From eb78bedab73136cf78df1db6fb77b130ca39255f Mon Sep 17 00:00:00 2001 From: normal Date: Sat, 7 Jul 2018 05:34:03 +0000 Subject: [PATCH] test/socket/test_socket.rb (test_timestamp): retry send I theorize there can be UDP packet loss even over loopback if the kernel is under memory pressure. Retry sending periodically until recvmsg succeeds. i[ruby-core:87842] [Bug #14898] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63872 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/socket/test_socket.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index 126f789492..10549383c9 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -464,10 +464,24 @@ class TestSocket < Test::Unit::TestCase Addrinfo.udp("127.0.0.1", 0).bind {|s1| Addrinfo.udp("127.0.0.1", 0).bind {|s2| s1.setsockopt(:SOCKET, :TIMESTAMP, true) - s2.send "a", 0, s1.local_address - msg, _, _, stamp = s1.recvmsg - assert_equal("a", msg) - assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMP)) + IO.pipe do |r,w| + # UDP may not be reliable, keep sending until recvmsg returns: + th = Thread.new do + n = 0 + begin + s2.send("a", 0, s1.local_address) + n += 1 + end while IO.select([r], nil, nil, 0.1).nil? + n + end + + msg, _, _, stamp = s1.recvmsg + w.close # stop th + assert_equal("a", msg) + assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMP)) + n = th.value + warn "UDP packet loss over loopback, #{n} tries needed" if n > 1 + end } } t2 = Time.now.strftime("%Y-%m-%d")