Skip to content

Commit 9bb57e7

Browse files
mamehsbt
authored andcommitted
test/resolv/test_dns.rb: Keep UDPSockets until a free port is found
Follow up to 589f1978d8c368b8eccf34453463ad46a58d36da I suspect `UDPSocket.new` grabs the same port number because they are closed at each trial.
1 parent dfa9a12 commit 9bb57e7

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

test/resolv/test_dns.rb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,29 @@ def with_udp_and_tcp(host, port)
6868
if port == 0
6969
# Automatic port; we might need to retry until we find a port which is free on both UDP _and_ TCP.
7070
retries_remaining = 10
71-
t = nil
72-
u = nil
71+
ts = []
72+
us = []
7373
begin
7474
begin
75-
u = UDPSocket.new
76-
u.bind(host, 0)
77-
_, udp_port, _, _ = u.addr
78-
t = TCPServer.new(host, udp_port)
79-
t.listen(1)
75+
us << UDPSocket.new
76+
us.last.bind(host, 0)
77+
_, udp_port, _, _ = us.last.addr
78+
ts << TCPServer.new(host, udp_port)
79+
ts.last.listen(1)
8080
rescue Errno::EADDRINUSE, Errno::EACCES
8181
# ADDRINUSE is what should get thrown if we try and bind a port which is already bound on UNIXen,
8282
# but windows can sometimes throw EACCESS.
8383
# See: https://stackoverflow.com/questions/48478869/cannot-bind-to-some-ports-due-to-permission-denied
8484
retries_remaining -= 1
85-
if retries_remaining > 0
86-
t&.close
87-
t = nil
88-
u&.close
89-
u = nil
90-
retry
91-
end
92-
omit "Could not find a free port after 10 retries"
85+
retry if retries_remaining > 0
86+
raise
9387
end
9488

9589
# If we get to this point, we have a valid t & u socket
96-
yield u, t
90+
yield us.last, ts.last
9791
ensure
98-
t&.close
99-
u&.close
92+
ts.each { _1.close }
93+
us.each { _1.close }
10094
end
10195
else
10296
# Explicitly specified port, don't retry the bind.

0 commit comments

Comments
 (0)