diff options
Diffstat (limited to 'test/socket')
| -rw-r--r-- | test/socket/test_addrinfo.rb | 6 | ||||
| -rw-r--r-- | test/socket/test_socket.rb | 39 | ||||
| -rw-r--r-- | test/socket/test_tcp.rb | 26 | ||||
| -rw-r--r-- | test/socket/test_unix.rb | 21 |
4 files changed, 78 insertions, 14 deletions
diff --git a/test/socket/test_addrinfo.rb b/test/socket/test_addrinfo.rb index c61764d76d..0c9529090e 100644 --- a/test/socket/test_addrinfo.rb +++ b/test/socket/test_addrinfo.rb @@ -360,6 +360,12 @@ class TestSocketAddrinfo < Test::Unit::TestCase assert_raise(Socket::ResolutionError) { Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("::1", 80) } end + def test_ractor_shareable + assert_ractor(<<~'RUBY', require: 'socket', timeout: 60) + Ractor.make_shareable Addrinfo.new "\x10\x02\x14\xE9\xE0\x00\x00\xFB\x00\x00\x00\x00\x00\x00\x00\x00".b + RUBY + end + def random_port # IANA suggests dynamic port for 49152 to 65535 # http://www.iana.org/assignments/port-numbers diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index 165990dd64..c42527f370 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -173,8 +173,11 @@ class TestSocket < Test::Unit::TestCase def errors_addrinuse errs = [Errno::EADDRINUSE] - # MinGW fails with "Errno::EACCES: Permission denied - bind(2) for 0.0.0.0:49721" - errs << Errno::EACCES if /mingw/ =~ RUBY_PLATFORM + # Windows can fail with "Errno::EACCES: Permission denied - bind(2) for 0.0.0.0:49721" + # or "Test::Unit::ProxyError: Permission denied - bind(2) for 0.0.0.0:55333" + if /mswin|mingw/ =~ RUBY_PLATFORM + errs += [Errno::EACCES, Test::Unit::ProxyError] + end errs end @@ -906,7 +909,7 @@ class TestSocket < Test::Unit::TestCase Addrinfo.define_singleton_method(:getaddrinfo) { |*_| sleep } - assert_raise(Errno::ETIMEDOUT) do + assert_raise(IO::TimeoutError) do Socket.tcp("localhost", port, resolv_timeout: 0.01) end ensure @@ -931,12 +934,38 @@ class TestSocket < Test::Unit::TestCase server.close - assert_raise(Errno::ETIMEDOUT) do + assert_raise(IO::TimeoutError) do Socket.tcp("localhost", port, resolv_timeout: 0.01) end RUBY end + def test_tcp_socket_open_timeout + opts = %w[-rsocket -W1] + assert_separately opts, <<~RUBY + Addrinfo.define_singleton_method(:getaddrinfo) do |_, _, family, *_| + if family == Socket::AF_INET6 + sleep + else + [Addrinfo.tcp("127.0.0.1", 12345)] + end + end + + assert_raise(IO::TimeoutError) do + Socket.tcp("localhost", 12345, open_timeout: 0.01) + end + RUBY + end + + def test_tcp_socket_open_timeout_with_other_timeouts + opts = %w[-rsocket -W1] + assert_separately opts, <<~RUBY + assert_raise(ArgumentError) do + Socket.tcp("localhost", 12345, open_timeout: 0.01, resolv_timout: 0.01) + end + RUBY + end + def test_tcp_socket_one_hostname_resolution_succeeded_at_least opts = %w[-rsocket -W1] assert_separately opts, <<~RUBY @@ -982,7 +1011,7 @@ class TestSocket < Test::Unit::TestCase Addrinfo.define_singleton_method(:getaddrinfo) do |_, _, family, *_| case family when Socket::AF_INET6 then raise SocketError - when Socket::AF_INET then sleep(0.001); raise SocketError, "Last hostname resolution error" + when Socket::AF_INET then sleep(0.01); raise SocketError, "Last hostname resolution error" end end diff --git a/test/socket/test_tcp.rb b/test/socket/test_tcp.rb index be6d59b31e..d689ab2376 100644 --- a/test/socket/test_tcp.rb +++ b/test/socket/test_tcp.rb @@ -73,6 +73,30 @@ class TestSocket_TCPSocket < Test::Unit::TestCase end end + def test_tcp_initialize_open_timeout + return if RUBY_PLATFORM =~ /mswin|mingw|cygwin/ + + server = TCPServer.new("127.0.0.1", 0) + port = server.connect_address.ip_port + server.close + + assert_raise(IO::TimeoutError) do + TCPSocket.new( + "localhost", + port, + open_timeout: 0.01, + fast_fallback: true, + test_mode_settings: { delay: { ipv4: 1000 } } + ) + end + end + + def test_initialize_open_timeout_with_other_timeouts + assert_raise(ArgumentError) do + TCPSocket.new("localhost", 12345, open_timeout: 0.01, resolv_timeout: 0.01) + end + end + def test_initialize_connect_timeout assert_raise(IO::TimeoutError, Errno::ENETUNREACH, Errno::EACCES) do TCPSocket.new("192.0.2.1", 80, connect_timeout: 0) @@ -297,7 +321,7 @@ class TestSocket_TCPSocket < Test::Unit::TestCase port = server.connect_address.ip_port server.close - assert_raise(Errno::ETIMEDOUT) do + assert_raise(IO::TimeoutError) do TCPSocket.new( "localhost", port, diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index 3e7d85befc..e239e3935b 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -146,6 +146,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase end def test_fd_passing_race_condition + omit 'randomly crashes on macOS' if RUBY_PLATFORM =~ /darwin/ r1, w = IO.pipe s1, s2 = UNIXSocket.pair s1.nonblock = s2.nonblock = true @@ -292,14 +293,18 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase File.unlink path if path && File.socket?(path) end - def test_open_nul_byte - tmpfile = Tempfile.new("s") - path = tmpfile.path - tmpfile.close(true) - assert_raise(ArgumentError) {UNIXServer.open(path+"\0")} - assert_raise(ArgumentError) {UNIXSocket.open(path+"\0")} - ensure - File.unlink path if path && File.socket?(path) + def test_open_argument + assert_raise(TypeError) {UNIXServer.new(nil)} + assert_raise(TypeError) {UNIXServer.new(1)} + Tempfile.create("s") do |s| + path = s.path + s.close + File.unlink(path) + assert_raise(ArgumentError) {UNIXServer.open(path+"\0")} + assert_raise(ArgumentError) {UNIXSocket.open(path+"\0")} + arg = Struct.new(:to_path).new(path) + assert_equal(path, UNIXServer.open(arg) { |server| server.path }) + end end def test_addr |
