class Teaspoon::Server

Attributes

host[RW]
port[RW]

Public Class Methods

new() click to toggle source
# File lib/teaspoon/server.rb, line 9
def initialize
  @host = Teaspoon.configuration.server_host || "127.0.0.1"
  @port = Teaspoon.configuration.server_port || find_available_port
end

Public Instance Methods

responsive?() click to toggle source
# File lib/teaspoon/server.rb, line 27
def responsive?
  TCPSocket.new(host, port).close
  true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  false
end
start() click to toggle source
# File lib/teaspoon/server.rb, line 14
def start
  return if responsive?

  thread = Thread.new do
    disable_logging
    server = Rack::Server.new(rack_options)
    server.start
  end
  wait_until_started(thread)
rescue => e
  raise Teaspoon::ServerError.new(desc: e.message)
end
url() click to toggle source
# File lib/teaspoon/server.rb, line 34
def url
  "http://#{host}:#{port}"
end

Protected Instance Methods

disable_logging() click to toggle source
# File lib/teaspoon/server.rb, line 46
def disable_logging
  return unless defined?(Thin)
  if Teaspoon.configuration.suppress_log
    Thin::Logging.silent = true
  else
    Thin::Logging.trace = false
  end
end
find_available_port() click to toggle source
# File lib/teaspoon/server.rb, line 68
def find_available_port
  server = TCPServer.new(host, 0)
  server.addr[1]
ensure
  server.close if server
end
rack_options() click to toggle source
# File lib/teaspoon/server.rb, line 55
def rack_options
  {
    app: Rails.application,
    Host: host,
    Port: port,
    environment: "test",
    AccessLog: [],
    Logger: Rails.logger,
    server: Teaspoon.configuration.server,
    Silent: true
  }
end
wait_until_started(thread) click to toggle source
# File lib/teaspoon/server.rb, line 40
def wait_until_started(thread)
  Timeout.timeout(Teaspoon.configuration.server_timeout.to_i) { thread.join(0.1) until responsive? }
rescue Timeout::Error
  raise Timeout::Error.new("consider increasing the timeout with `config.server_timeout`")
end