2009-12-09 15:39:03 +00:00
|
|
|
require 'uri'
|
2009-11-05 16:35:45 +00:00
|
|
|
require 'net/http'
|
2009-11-05 16:39:57 +00:00
|
|
|
require 'rack'
|
2009-11-05 16:35:45 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
module Capybara
|
|
|
|
class Server
|
2015-07-06 23:19:47 +00:00
|
|
|
class Counter
|
|
|
|
attr_reader :value
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@value = 0
|
|
|
|
@mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment
|
|
|
|
@mutex.synchronize { @value += 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement
|
|
|
|
@mutex.synchronize { @value -= 1 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-13 12:57:43 +00:00
|
|
|
class Middleware
|
|
|
|
attr_accessor :error
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2015-07-06 23:19:47 +00:00
|
|
|
@counter = Counter.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def pending_requests?
|
|
|
|
@counter.value > 0
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def call(env)
|
|
|
|
if env["PATH_INFO"] == "/__identify__"
|
2011-04-26 10:41:43 +00:00
|
|
|
[200, {}, [@app.object_id.to_s]]
|
2010-07-09 18:31:58 +00:00
|
|
|
else
|
2015-07-06 23:19:47 +00:00
|
|
|
@counter.increment
|
2012-07-13 12:57:43 +00:00
|
|
|
begin
|
|
|
|
@app.call(env)
|
2015-02-26 21:40:59 +00:00
|
|
|
rescue *Capybara.server_errors => e
|
2012-07-13 12:57:43 +00:00
|
|
|
@error = e unless @error
|
|
|
|
raise e
|
2015-07-06 23:19:47 +00:00
|
|
|
ensure
|
|
|
|
@counter.decrement
|
2012-07-13 12:57:43 +00:00
|
|
|
end
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-14 20:54:36 +00:00
|
|
|
class << self
|
|
|
|
def ports
|
|
|
|
@ports ||= {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-11 21:26:37 +00:00
|
|
|
attr_reader :app, :port, :host
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2013-09-11 21:26:37 +00:00
|
|
|
def initialize(app, port=Capybara.server_port, host=Capybara.server_host)
|
2010-07-09 18:31:58 +00:00
|
|
|
@app = app
|
2012-07-13 12:57:43 +00:00
|
|
|
@middleware = Middleware.new(@app)
|
2014-03-31 22:10:27 +00:00
|
|
|
@server_thread = nil # suppress warnings
|
2013-09-11 21:26:37 +00:00
|
|
|
@host, @port = host, port
|
2012-07-13 13:19:20 +00:00
|
|
|
@port ||= Capybara::Server.ports[@app.object_id]
|
|
|
|
@port ||= find_available_port
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-11-04 22:00:05 +00:00
|
|
|
|
2012-07-13 12:57:43 +00:00
|
|
|
def reset_error!
|
|
|
|
@middleware.error = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def error
|
|
|
|
@middleware.error
|
|
|
|
end
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def responsive?
|
2013-03-17 23:35:48 +00:00
|
|
|
return false if @server_thread && @server_thread.join(0)
|
2012-03-06 22:42:47 +00:00
|
|
|
|
2010-07-14 20:54:36 +00:00
|
|
|
res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
|
|
|
|
|
|
|
|
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
|
|
|
|
return res.body == @app.object_id.to_s
|
|
|
|
end
|
2013-03-16 18:59:02 +00:00
|
|
|
rescue SystemCallError
|
2010-07-14 20:54:36 +00:00
|
|
|
return false
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-12-13 13:33:14 +00:00
|
|
|
|
2015-07-06 23:19:47 +00:00
|
|
|
def wait_for_pending_requests
|
|
|
|
Timeout.timeout(60) { sleep(0.01) while @middleware.pending_requests? }
|
|
|
|
rescue Timeout::Error
|
|
|
|
raise "Requests did not finish in 60 seconds"
|
|
|
|
end
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def boot
|
2012-07-13 13:19:20 +00:00
|
|
|
unless responsive?
|
|
|
|
Capybara::Server.ports[@app.object_id] = @port
|
2010-10-08 15:30:08 +00:00
|
|
|
|
2012-07-13 13:19:20 +00:00
|
|
|
@server_thread = Thread.new do
|
|
|
|
Capybara.server.call(@middleware, @port)
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2012-07-13 13:19:20 +00:00
|
|
|
|
2013-03-17 23:35:48 +00:00
|
|
|
Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2012-12-26 13:50:45 +00:00
|
|
|
rescue Timeout::Error
|
2011-10-19 20:26:58 +00:00
|
|
|
raise "Rack application timed out during boot"
|
2010-07-14 20:54:36 +00:00
|
|
|
else
|
|
|
|
self
|
2009-11-04 22:00:05 +00:00
|
|
|
end
|
2009-11-07 14:35:47 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
private
|
2010-01-17 16:40:26 +00:00
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
def find_available_port
|
2010-07-14 20:54:36 +00:00
|
|
|
server = TCPServer.new('127.0.0.1', 0)
|
|
|
|
server.addr[1]
|
|
|
|
ensure
|
|
|
|
server.close if server
|
2010-07-09 08:05:30 +00:00
|
|
|
end
|
|
|
|
|
2010-07-09 18:31:58 +00:00
|
|
|
end
|
2009-11-07 14:35:47 +00:00
|
|
|
end
|