diff options
| author | Yusuke Endoh <mame@ruby-lang.org> | 2024-10-10 14:14:56 +0900 |
|---|---|---|
| committer | Yusuke Endoh <mame@ruby-lang.org> | 2024-10-10 14:44:51 +0900 |
| commit | 15bb571730c4c19def9c4e1d59c9546056d9bd98 (patch) | |
| tree | 876fee98777ad188adad71be5cceec847e9c50bc | |
| parent | 63f94e12fb371fdba41a8233e56f752a1a579c5e (diff) | |
test/resolv/test_dns.rb: Keep `UDPSocket`s 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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/11863
| -rw-r--r-- | test/resolv/test_dns.rb | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/test/resolv/test_dns.rb b/test/resolv/test_dns.rb index 743511c8fb..fb5842f048 100644 --- a/test/resolv/test_dns.rb +++ b/test/resolv/test_dns.rb @@ -68,35 +68,29 @@ class TestResolvDNS < Test::Unit::TestCase if port == 0 # Automatic port; we might need to retry until we find a port which is free on both UDP _and_ TCP. retries_remaining = 10 - t = nil - u = nil + ts = [] + us = [] begin begin - u = UDPSocket.new - u.bind(host, 0) - _, udp_port, _, _ = u.addr - t = TCPServer.new(host, udp_port) - t.listen(1) + us << UDPSocket.new + us.last.bind(host, 0) + _, udp_port, _, _ = us.last.addr + ts << TCPServer.new(host, udp_port) + ts.last.listen(1) rescue Errno::EADDRINUSE, Errno::EACCES # ADDRINUSE is what should get thrown if we try and bind a port which is already bound on UNIXen, # but windows can sometimes throw EACCESS. # See: https://stackoverflow.com/questions/48478869/cannot-bind-to-some-ports-due-to-permission-denied retries_remaining -= 1 - if retries_remaining > 0 - t&.close - t = nil - u&.close - u = nil - retry - end - omit "Could not find a free port after 10 retries" + retry if retries_remaining > 0 + raise end # If we get to this point, we have a valid t & u socket - yield u, t + yield us.last, ts.last ensure - t&.close - u&.close + ts.each { _1.close } + us.each { _1.close } end else # Explicitly specified port, don't retry the bind. |
