Class: Smartest::Rails::TestServer
- Inherits:
-
Object
- Object
- Smartest::Rails::TestServer
- Defined in:
- lib/smartest/rails.rb
Constant Summary collapse
- DEFAULT_HOST =
"127.0.0.1"- 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: DEFAULT_HOST, 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: 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
#host ⇒ Object (readonly)
Returns the value of attribute host.
15 16 17 |
# File 'lib/smartest/rails.rb', line 15 def host @host end |
#port ⇒ Object (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_url ⇒ Object
49 50 51 |
# File 'lib/smartest/rails.rb', line 49 def base_url "http://#{host}:#{port}" end |
#start ⇒ Object
26 27 28 |
# File 'lib/smartest/rails.rb', line 26 def start @thread ||= @server.run end |
#stop ⇒ Object
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 |