diff --git a/lib/capybara/server.rb b/lib/capybara/server.rb index d1b4dfd0..9af7c4ea 100644 --- a/lib/capybara/server.rb +++ b/lib/capybara/server.rb @@ -28,6 +28,7 @@ module Capybara def initialize(app) @app = app + @server_thread = nil # supress warnings end def host @@ -43,6 +44,8 @@ module Capybara end def responsive? + return false if @server_thread && @server_thread.join(0) + res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') } 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 Capybara::Server.ports[@app.object_id] = @port - Thread.new do + @server_thread = Thread.new do Capybara.server.call(Identify.new(@app), @port) end - Timeout.timeout(60) { sleep(0.1) until responsive? } + Timeout.timeout(60) { @server_thread.join(0.1) until responsive? } end end rescue TimeoutError diff --git a/spec/server_spec.rb b/spec/server_spec.rb index 8a23c8d0..83f5d103 100644 --- a/spec/server_spec.rb +++ b/spec/server_spec.rb @@ -71,4 +71,21 @@ describe Capybara::Server do @server1a.port.should == @server1b.port @server2a.port.should == @server2b.port 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