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

Use TCPServer to find available port for server

This protects against the race conditions which inevitably occur
when booting multiple capybara servers (such as running specjour).
TCPServer.new() will set recently opened sockets to SOCK_WAIT status to
ensure that a certain amount of time elapses before they are reused.
This commit is contained in:
Bernerd Schaefer 2010-07-06 16:03:32 -05:00
parent ccced29b47
commit a8a4efc379

View file

@ -83,8 +83,10 @@ class Capybara::Server
private
def find_available_port
@port = 9887
@port += 1 while is_port_open?(@port) and not is_running_on_port?(@port)
server = TCPServer.new('127.0.0.1', 0)
@port = server.addr[1]
ensure
server.close if server
end
def is_running_on_port?(tested_port)
@ -97,18 +99,4 @@ private
return false
end
def is_port_open?(tested_port)
Timeout::timeout(1) do
begin
s = TCPSocket.new(host, tested_port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
rescue Timeout::Error
return false
end
end