Class: Smartest::Rails::TestServer

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

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1"
DEFAULT_READY_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TestServer.



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

def initialize(app:, host: DEFAULT_HOST, port: nil)
  @app = app
  @host = host
  @requested_port = port || ENV["SMARTEST_RAILS_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.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#base_urlObject



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

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

#startObject



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

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

#stopObject



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

def stop
  @server.stop
end

#wait_for_ready(timeout: DEFAULT_READY_TIMEOUT) ⇒ Object



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

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



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

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