Class: Smartest::Rails::TestServer

Inherits:
Object
  • Object
show all
Defined in:
lib/smartest/rails.rb

Constant Summary collapse

DEFAULT_READY_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, host: nil, port: nil) ⇒ TestServer

Returns a new instance of TestServer.



16
17
18
19
20
21
22
23
# File 'lib/smartest/rails.rb', line 16

def initialize(app:, host: nil, port: nil)
  @app = app
  @host = host || "127.0.0.1"
  @requested_port = port ? port.to_i : 0
  @server = Puma::Server.new(@app)
  @port = bind_tcp_listener
  @thread = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/smartest/rails.rb', line 14

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/smartest/rails.rb', line 14

def port
  @port
end

Instance Method Details

#base_urlObject



48
49
50
# File 'lib/smartest/rails.rb', line 48

def base_url
  "http://#{host}:#{port}"
end

#startObject



25
26
27
# File 'lib/smartest/rails.rb', line 25

def start
  @thread ||= @server.run
end

#stopObject



29
30
31
# File 'lib/smartest/rails.rb', line 29

def stop
  @server.stop
end

#wait_for_ready(timeout: DEFAULT_READY_TIMEOUT) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/smartest/rails.rb', line 33

def wait_for_ready(timeout: DEFAULT_READY_TIMEOUT)
  Timeout.timeout(timeout) do
    sleep 0.05 until responsive?
  end
rescue Timeout::Error
  raise "Rails test server did not become ready at #{base_url} within #{timeout} seconds"
end

#wait_for_stopped(timeout: DEFAULT_READY_TIMEOUT) ⇒ Object



41
42
43
44
45
46
# File 'lib/smartest/rails.rb', line 41

def wait_for_stopped(timeout: DEFAULT_READY_TIMEOUT)
  return unless @thread
  return if @thread.join(timeout)

  raise "Rails test server did not stop within #{timeout} seconds"
end