From 12f11714c2c3fd1cfc98d30fdbb777260132b818 Mon Sep 17 00:00:00 2001 From: normal Date: Thu, 2 Aug 2018 06:09:38 +0000 Subject: test/socket/test_socket.rb (test_timestampns): retry send It looks like we need to retry test_timestampns in addition to test_timestamp; so share some code while we're at it. cf. http://ci.rvm.jp/results/trunk-test@frontier/1153126 [ruby-core:88104] [Bug #14898] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64157 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/socket/test_socket.rb | 59 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index 10549383c9..7f7ac90358 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -456,6 +456,33 @@ class TestSocket < Test::Unit::TestCase } end + def timestamp_retry_rw(s1, s2, t1, type) + 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 + assert_equal("a", msg) + assert(stamp.cmsg_is?(:SOCKET, type)) + w.close # stop th + n = th.value + n > 1 and + warn "UDP packet loss for #{type} over loopback, #{n} tries needed" + t2 = Time.now.strftime("%Y-%m-%d") + pat = Regexp.union([t1, t2].uniq) + assert_match(pat, stamp.inspect) + t = stamp.timestamp + assert_match(pat, t.strftime("%Y-%m-%d")) + stamp + end + end + def test_timestamp return if /linux|freebsd|netbsd|openbsd|solaris|darwin/ !~ RUBY_PLATFORM return if !defined?(Socket::AncillaryData) || !defined?(Socket::SO_TIMESTAMP) @@ -464,31 +491,10 @@ 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) - 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 + stamp = timestamp_retry_rw(s1, s2, t1, :TIMESTAMP) } } - t2 = Time.now.strftime("%Y-%m-%d") - pat = Regexp.union([t1, t2].uniq) - assert_match(pat, stamp.inspect) t = stamp.timestamp - assert_match(pat, t.strftime("%Y-%m-%d")) pat = /\.#{"%06d" % t.usec}/ assert_match(pat, stamp.inspect) end @@ -505,17 +511,10 @@ class TestSocket < Test::Unit::TestCase # SO_TIMESTAMPNS is available since Linux 2.6.22 return end - s2.send "a", 0, s1.local_address - msg, _, _, stamp = s1.recvmsg - assert_equal("a", msg) - assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMPNS)) + stamp = timestamp_retry_rw(s1, s2, t1, :TIMESTAMPNS) } } - t2 = Time.now.strftime("%Y-%m-%d") - pat = Regexp.union([t1, t2].uniq) - assert_match(pat, stamp.inspect) t = stamp.timestamp - assert_match(pat, t.strftime("%Y-%m-%d")) pat = /\.#{"%09d" % t.nsec}/ assert_match(pat, stamp.inspect) end -- cgit v1.2.3