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

144 lines
3.1 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -05:00
require 'uri'
2009-11-05 11:35:45 -05:00
require 'net/http'
2009-11-05 11:39:57 -05:00
require 'rack'
2009-11-05 11:35:45 -05:00
2010-07-09 14:31:58 -04:00
module Capybara
class Server
class Middleware
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
attr_accessor :error
def initialize(app, server_errors)
2010-07-09 14:31:58 -04:00
@app = app
@counter = Counter.new
@server_errors = server_errors
end
def pending_requests?
@counter.value > 0
2010-07-09 14:31:58 -04:00
end
2010-07-09 14:31:58 -04:00
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, [@app.object_id.to_s]]
2010-07-09 14:31:58 -04:00
else
@counter.increment
begin
@app.call(env)
rescue *@server_errors => e
2018-01-08 15:23:54 -05:00
@error ||= e
raise e
ensure
@counter.decrement
end
2010-07-09 14:31:58 -04:00
end
end
end
class << self
def ports
@ports ||= {}
end
end
attr_reader :app, :port, :host
2018-01-09 17:05:50 -05:00
def initialize(app, port = Capybara.server_port, host = Capybara.server_host, server_errors = Capybara.server_errors)
2010-07-09 14:31:58 -04:00
@app = app
2014-03-31 18:10:27 -04:00
@server_thread = nil # suppress warnings
@host, @port, @server_errors = host, port, server_errors
2016-07-25 13:04:50 -04:00
@port ||= Capybara::Server.ports[port_key]
@port ||= find_available_port(host)
2010-07-09 14:31:58 -04:00
end
2009-11-04 17:00:05 -05:00
def reset_error!
2016-07-25 15:12:59 -04:00
middleware.error = nil
end
def error
2016-07-25 15:12:59 -04:00
middleware.error
end
2010-07-09 14:31:58 -04:00
def responsive?
return false if @server_thread && @server_thread.join(0)
2016-07-25 15:12:59 -04:00
res = Net::HTTP.start(host, port) { |http| http.get('/__identify__') }
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
2016-07-25 15:12:59 -04:00
return res.body == app.object_id.to_s
end
rescue SystemCallError
return false
2010-07-09 14:31:58 -04:00
end
def wait_for_pending_requests
start_time = Capybara::Helpers.monotonic_time
while pending_requests?
if (Capybara::Helpers.monotonic_time - start_time) > 60
raise "Requests did not finish in 60 seconds"
end
sleep 0.01
end
end
2010-07-09 14:31:58 -04:00
def boot
unless responsive?
2016-07-25 15:12:59 -04:00
Capybara::Server.ports[port_key] = port
@server_thread = Thread.new do
2016-07-25 15:12:59 -04:00
Capybara.server.call(middleware, port, host)
2010-07-09 14:31:58 -04:00
end
start_time = Capybara::Helpers.monotonic_time
until responsive?
if (Capybara::Helpers.monotonic_time - start_time) > 60
raise "Rack application timed out during boot"
end
@server_thread.join(0.1)
end
2010-07-09 14:31:58 -04:00
end
self
2009-11-04 17:00:05 -05:00
end
2009-11-07 09:35:47 -05:00
2010-07-09 14:31:58 -04:00
private
2016-07-25 15:12:59 -04:00
def middleware
@middleware ||= Middleware.new(app, @server_errors)
2016-07-25 15:12:59 -04:00
end
2016-07-25 13:04:50 -04:00
def port_key
2016-07-25 15:12:59 -04:00
Capybara.reuse_server ? app.object_id : middleware.object_id
2016-07-25 13:04:50 -04:00
end
def pending_requests?
2016-07-25 15:12:59 -04:00
middleware.pending_requests?
2016-07-25 13:04:50 -04:00
end
def find_available_port(host)
server = TCPServer.new(host, 0)
server.addr[1]
ensure
server.close if server
end
2010-07-09 14:31:58 -04:00
end
2009-11-07 09:35:47 -05:00
end