Skip to content

Commit

Permalink
Avoid test failures on hosts that only support IPv4 (ruby#12213)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shioimm authored Dec 2, 2024
1 parent c6b8a52 commit 8f57204
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions test/socket/test_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ def test_initialize_v6_hostname_resolved_earlier
return if RUBY_PLATFORM =~ /mswin|mingw|cygwin/

begin
# Verify that "localhost" can be resolved to an IPv6 address
Socket.getaddrinfo("localhost", 0, Socket::AF_INET6)
server = TCPServer.new("::1", 0)
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
exit
rescue Socket::ResolutionError, Errno::EADDRNOTAVAIL # IPv6 is not supported
return
end

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

begin
# Verify that "localhost" can be resolved to an IPv6 address
Socket.getaddrinfo("localhost", 0, Socket::AF_INET6)
server = TCPServer.new("::1", 0)
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
exit
rescue Socket::ResolutionError, Errno::EADDRNOTAVAIL # IPv6 is not supported
return
end

port = server.addr[1]
Expand Down Expand Up @@ -290,7 +294,7 @@ def test_initialize_resolv_timeout_with_connection_failure
begin
server = TCPServer.new("::1", 0)
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
exit
return
end

port = server.connect_address.ip_port
Expand All @@ -314,8 +318,9 @@ def test_initialize_with_hostname_resolution_failure_after_connection_failure
begin
server = TCPServer.new("::1", 0)
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
exit
return
end

port = server.connect_address.ip_port
server.close

Expand Down Expand Up @@ -353,7 +358,7 @@ def test_initialize_v6_connected_socket_with_v6_address
begin
server = TCPServer.new("::1", 0)
rescue Errno::EADDRNOTAVAIL # IPv6 is not supported
exit
return
end

server_thread = Thread.new { server.accept }
Expand Down

0 comments on commit 8f57204

Please sign in to comment.