1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Add code to catch server errors at server boot time, and during responsiveness checks

This commit is contained in:
James Tucker 2012-03-06 14:42:47 -08:00
parent bb7f4b1d68
commit ab62b27ae1
2 changed files with 22 additions and 2 deletions

View file

@ -28,6 +28,7 @@ module Capybara
def initialize(app) def initialize(app)
@app = app @app = app
@server_thread = nil # supress warnings
end end
def host def host
@ -43,6 +44,8 @@ module Capybara
end end
def responsive? def responsive?
return false if @server_thread && @server_thread.join(0)
res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') } res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection) if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
@ -60,11 +63,11 @@ module Capybara
@port = Capybara.server_port || find_available_port @port = Capybara.server_port || find_available_port
Capybara::Server.ports[@app.object_id] = @port Capybara::Server.ports[@app.object_id] = @port
Thread.new do @server_thread = Thread.new do
Capybara.server.call(Identify.new(@app), @port) Capybara.server.call(Identify.new(@app), @port)
end end
Timeout.timeout(60) { sleep(0.1) until responsive? } Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
end end
end end
rescue TimeoutError rescue TimeoutError

View file

@ -71,4 +71,21 @@ describe Capybara::Server do
@server1a.port.should == @server1b.port @server1a.port.should == @server1b.port
@server2a.port.should == @server2b.port @server2a.port.should == @server2b.port
end end
it "should raise server errors when the server errors before the timeout" do
begin
Capybara.server do
sleep 0.1
raise 'kaboom'
end
proc do
Capybara::Server.new(proc {|e|}).boot
end.should raise_error(RuntimeError, 'kaboom')
ensure
# TODO refactor out the defaults so it's reliant on unset state instead of
# a one-time call in capybara.rb
Capybara.server {|app, port| Capybara.run_default_server(app, port)}
end
end
end end