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

124 lines
2.7 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
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
2010-07-09 14:31:58 -04:00
def initialize(app)
@app = app
@counter = Counter.new
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 *Capybara.server_errors => e
@error = e unless @error
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
def initialize(app, port=Capybara.server_port, host=Capybara.server_host)
2010-07-09 14:31:58 -04:00
@app = app
@middleware = Middleware.new(@app)
2014-03-31 18:10:27 -04:00
@server_thread = nil # suppress warnings
@host, @port = host, port
@port ||= Capybara::Server.ports[Capybara.reuse_server ? @app.object_id : @middleware.object_id]
@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!
@middleware.error = nil
end
def error
@middleware.error
end
2010-07-09 14:31:58 -04:00
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)
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
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 14:31:58 -04:00
def boot
unless responsive?
Capybara::Server.ports[Capybara.reuse_server ? @app.object_id : @middleware.object_id] = @port
@server_thread = Thread.new do
2016-02-16 16:37:17 -05:00
Capybara.server.call(@middleware, @port, @host)
2010-07-09 14:31:58 -04:00
end
Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
2010-07-09 14:31:58 -04:00
end
rescue Timeout::Error
2011-10-19 16:26:58 -04:00
raise "Rack application timed out during boot"
else
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
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