Skip to content

Commit 8f57204

Browse files
authored
Avoid test failures on hosts that only support IPv4 (#12213)
To verify the behavior of HEv2, some tests were prepared. But unexpected failures occur in certain environments. This happens in environments where "localhost" resolves only to an IPv4 address during tests that verify connections to IPv6. For example, the following situation can occur: - The server process is bound to ::1. - The client socket always resolves "localhost" to 127.0.0.1 and attempts to connect to 127.0.0.1. - Since no server is bound to 127.0.0.1, an ECONNREFUSED error is raised. In such situations, the behavior of `TCPSocket.new` remains unchanged from before the introduction of HEv2. (The failures occur because tests explicitly binding to ::1 were added to verify HEv2 behavior.) This change ensures that the affected tests are skipped in environments of this kind.
1 parent c6b8a52 commit 8f57204

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

test/socket/test_tcp.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ def test_initialize_v6_hostname_resolved_earlier
146146
return if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
147147

148148
begin
149+
# Verify that "localhost" can be resolved to an IPv6 address
150+
Socket.getaddrinfo("localhost", 0, Socket::AF_INET6)
149151
server = TCPServer.new("::1", 0)
150-
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
151-
exit
152+
rescue Socket::ResolutionError, Errno::EADDRNOTAVAIL # IPv6 is not supported
153+
return
152154
end
153155

154156
server_thread = Thread.new { server.accept }
@@ -192,9 +194,11 @@ def test_initialize_v6_hostname_resolved_in_resolution_delay
192194
return if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
193195

194196
begin
197+
# Verify that "localhost" can be resolved to an IPv6 address
198+
Socket.getaddrinfo("localhost", 0, Socket::AF_INET6)
195199
server = TCPServer.new("::1", 0)
196-
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
197-
exit
200+
rescue Socket::ResolutionError, Errno::EADDRNOTAVAIL # IPv6 is not supported
201+
return
198202
end
199203

200204
port = server.addr[1]
@@ -290,7 +294,7 @@ def test_initialize_resolv_timeout_with_connection_failure
290294
begin
291295
server = TCPServer.new("::1", 0)
292296
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
293-
exit
297+
return
294298
end
295299

296300
port = server.connect_address.ip_port
@@ -314,8 +318,9 @@ def test_initialize_with_hostname_resolution_failure_after_connection_failure
314318
begin
315319
server = TCPServer.new("::1", 0)
316320
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
317-
exit
321+
return
318322
end
323+
319324
port = server.connect_address.ip_port
320325
server.close
321326

@@ -353,7 +358,7 @@ def test_initialize_v6_connected_socket_with_v6_address
353358
begin
354359
server = TCPServer.new("::1", 0)
355360
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
356-
exit
361+
return
357362
end
358363

359364
server_thread = Thread.new { server.accept }

0 commit comments

Comments
 (0)