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

Fallback when the port isn't actually available

This code will test the randomly assigned port to make sure it can be
used before returning it. It finds another port if there is a problem.
This commit is contained in:
Lukas Oberhuber 2019-09-07 12:06:31 +01:00
parent 17717bc261
commit bb75591306
2 changed files with 22 additions and 9 deletions

View file

@ -105,10 +105,23 @@ module Capybara
end
def find_available_port(host)
server = TCPServer.new(host, 0)
server.addr[1]
ensure
server&.close
port = 0
while port.zero?
begin
server = TCPServer.new(host, port)
port = server.addr[1]
ensure
server&.close
end
begin
server = TCPServer.new(host, port)
rescue Errno::EADDRINUSE
port = 0
ensure
server&.close
end
end
port
end
end
end

View file

@ -81,12 +81,12 @@ RSpec.describe Capybara::Server do
end
it 'should handle that getting available ports fails randomly' do
expect {
100000.times do |count|
port = Capybara::Server.new(Object.new).send(:find_available_port, "0.0.0.0")
use_port("0.0.0.0", port)
expect do
100000.times do
port = described_class.new(Object.new).send(:find_available_port, '0.0.0.0')
use_port('0.0.0.0', port)
end
}.not_to raise_error
end.not_to raise_error
end
it 'should return its #base_url' do