Module: HttpTest::Server
Constant Summary collapse
- PORT =
HttpTest::PORT
Instance Method Summary collapse
- #available!(timeout = 0.1) ⇒ Object
- #available?(timeout = 0.1) ⇒ Boolean
- #start!(command) ⇒ Object
- #started? ⇒ Boolean
- #wait_for_port(timeout = 10) ⇒ Object
Instance Method Details
#available!(timeout = 0.1) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/http-test/server.rb', line 9 def available!(timeout = 0.1) s = nil Timeout.timeout(timeout) do STDERR.print "." s = TCPSocket.new("127.0.0.1", PORT) end STDERR.puts "[http-test] test server became available on http://127.0.0.1:#{PORT}" ensure s&.close end |
#available?(timeout = 0.1) ⇒ Boolean
21 22 23 24 25 26 |
# File 'lib/http-test/server.rb', line 21 def available?(timeout = 0.1) available!(timeout) true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error false end |
#start!(command) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/http-test/server.rb', line 41 def start!(command) return if started? pid = fork do # Signal.trap("HUP") { STDERR.puts "Exiting web server"; exit } # # ... do some work ... ENV["RACK_ENV"] = "test" ENV["PORT"] = PORT.to_s STDERR.puts "[http-test] Trying to start test server via '#{command}'" exec command end at_exit do Process.kill("TERM", pid) Process.wait end wait_for_port @started = true end |
#started? ⇒ Boolean
37 38 39 |
# File 'lib/http-test/server.rb', line 37 def started? @started ? true : false end |
#wait_for_port(timeout = 10) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/http-test/server.rb', line 28 def wait_for_port(timeout = 10) (timeout / 0.1).to_i.times do return true if available?(PORT) sleep(0.1) end available! end |