mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Simplify fix and test
This commit is contained in:
parent
bb75591306
commit
4cd9d8790b
2 changed files with 31 additions and 27 deletions
|
@ -105,23 +105,20 @@ module Capybara
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_available_port(host)
|
def find_available_port(host)
|
||||||
port = 0
|
server = TCPServer.new(host, 0)
|
||||||
while port.zero?
|
|
||||||
begin
|
|
||||||
server = TCPServer.new(host, port)
|
|
||||||
port = server.addr[1]
|
port = server.addr[1]
|
||||||
ensure
|
server.close
|
||||||
server&.close
|
|
||||||
end
|
# Workaround issue where some platforms (mac, ???) when passed a host
|
||||||
begin
|
# of '0.0.0.0' will return a port that is only available on one of the
|
||||||
|
# ip addresses that resolves to, but the next binding to that port requires
|
||||||
|
# that port to be available on all ips
|
||||||
server = TCPServer.new(host, port)
|
server = TCPServer.new(host, port)
|
||||||
|
port
|
||||||
rescue Errno::EADDRINUSE
|
rescue Errno::EADDRINUSE
|
||||||
port = 0
|
retry
|
||||||
ensure
|
ensure
|
||||||
server&.close
|
server&.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
port
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -74,19 +74,26 @@ RSpec.describe Capybara::Server do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_port(host, port)
|
it 'should handle that getting available ports fails randomly' do
|
||||||
server = TCPServer.new(host, port)
|
begin
|
||||||
|
# Use a port to force a EADDRINUSE error to be generated
|
||||||
|
server = TCPServer.new('0.0.0.0', 0)
|
||||||
|
server_port = server.addr[1]
|
||||||
|
d_server = instance_double('TCPServer', addr: [nil, server_port, nil, nil], close: nil)
|
||||||
|
call_count = 0
|
||||||
|
allow(TCPServer).to receive(:new).and_wrap_original do |m, *args|
|
||||||
|
begin
|
||||||
|
call_count.zero? ? d_server : m.call(*args)
|
||||||
|
ensure
|
||||||
|
call_count += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
port = described_class.new(Object.new, host: '0.0.0.0').port
|
||||||
|
expect(port).not_to eq(server_port)
|
||||||
ensure
|
ensure
|
||||||
server&.close
|
server&.close
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should handle that getting available ports fails randomly' do
|
|
||||||
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
|
|
||||||
end.not_to raise_error
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should return its #base_url' do
|
it 'should return its #base_url' do
|
||||||
|
|
Loading…
Reference in a new issue