spec/port_explorer_spec.rb in rest-assured-3.0.1 vs spec/port_explorer_spec.rb in rest-assured-4.0.0

- old
+ new

@@ -6,22 +6,33 @@ it 'finds free tcp port' do free_port = PortExplorer.free_port expect { Net::HTTP.get('127.0.0.1', '/', free_port) }.to raise_error(Errno::ECONNREFUSED) end - it 'knows if port is in use' do - port = PortExplorer.free_port + context 'port is taken' do + let(:port) { PortExplorer.free_port } + server = nil - Thread.new do - TCPServer.open('127.0.0.1', port) do |serv| - s = serv.accept - s.puts 'Hello from test' - s.close - end + before :each do + server = TCPServer.new port end - sleep 0.5 - expect(PortExplorer.port_free?(port)).to eq(false) + after :each do + server.close + end + + it 'knows if port is in use' do + Thread.new do + loop do + Thread.start(server.accept) do |client| + client.puts "Hello!" + client.close + end + end + end + + expect(PortExplorer.port_free?(port)).to eq(false) + end end it 'knows that port is free' do port = PortExplorer.free_port