Class: Smartest::Rails::TestServer
- Inherits:
-
Object
- Object
- Smartest::Rails::TestServer
- Defined in:
- lib/smartest/rails.rb
Constant Summary collapse
- DEFAULT_READY_TIMEOUT =
10
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #base_url ⇒ Object
-
#initialize(app:, host: nil, port: nil) ⇒ TestServer
constructor
A new instance of TestServer.
- #start ⇒ Object
- #stop ⇒ Object
- #wait_for_ready(timeout: DEFAULT_READY_TIMEOUT) ⇒ Object
- #wait_for_stopped(timeout: DEFAULT_READY_TIMEOUT) ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/smartest/rails.rb', line 14 def host @host end |
#port ⇒ Object (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_url ⇒ Object
48 49 50 |
# File 'lib/smartest/rails.rb', line 48 def base_url "http://#{host}:#{port}" end |
#start ⇒ Object
25 26 27 |
# File 'lib/smartest/rails.rb', line 25 def start @thread ||= @server.run end |
#stop ⇒ Object
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 |