Class: Calliper::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/calliper/server.rb

Overview

NOTE: Borrows code from the fantastic Teaspoon gem:

http://github.com/modeset/teaspoon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, options = {}) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
# File 'lib/calliper/server.rb', line 10

def initialize(application, options = {})
  @application = application
  @port = options[:port] || Config.port || find_available_port
  @logger = options[:logger]
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



8
9
10
# File 'lib/calliper/server.rb', line 8

def application
  @application
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/calliper/server.rb', line 8

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/calliper/server.rb', line 8

def port
  @port
end

Instance Method Details

#rack_optionsObject



39
40
41
42
43
44
45
46
47
# File 'lib/calliper/server.rb', line 39

def rack_options
  {
    app: application,
    environment: 'test',
    Port: port,
    AccessLog: [],
    Logger: logger
  }
end

#responsive?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/calliper/server.rb', line 31

def responsive?
  return false if @thread && @thread.join(0)
  TCPSocket.new('127.0.0.1', port).close
  return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  return false
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/calliper/server.rb', line 16

def start
  @thread = Thread.new do
    server = Rack::Server.new(rack_options)
    server.start
  end

  Timeout.timeout(60) do
    @thread.join(0.1) until responsive?
  end
rescue Timeout::Error
  raise "Server failed to start within 60 seconds."
rescue => e
  raise "Cannot start server: #{e.message}"
end

#urlObject



49
50
51
# File 'lib/calliper/server.rb', line 49

def url
  "http://127.0.0.1:#{port}"
end