diff options
author | Sam Balana <sbalana@google.com> | 2021-06-04 15:17:04 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-06-04 15:19:53 -0700 |
commit | a2c88252c84839550bef1543ed5cc904340a8a3e (patch) | |
tree | c57adb55d4ddc2151668f498c07ab83bedefa782 /test | |
parent | 240629524905024c7564d009cbc47c7b44064219 (diff) |
Allow sniffer receive timeout durations less than one usec
Fixes the erronously signaled fatal error when the sniffer receive timeout
duration is less than one usec. This was caused by the converstion from float64
to int64; the integer conversion truncated the floating point to 0, which
signaled the fatal error.
PiperOrigin-RevId: 377600179
Diffstat (limited to 'test')
-rw-r--r-- | test/packetimpact/testbench/rawsockets.go | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/test/packetimpact/testbench/rawsockets.go b/test/packetimpact/testbench/rawsockets.go index feeb0888a..6d95c033d 100644 --- a/test/packetimpact/testbench/rawsockets.go +++ b/test/packetimpact/testbench/rawsockets.go @@ -17,7 +17,6 @@ package testbench import ( "encoding/binary" "fmt" - "math" "net" "testing" "time" @@ -81,19 +80,20 @@ func (s *Sniffer) Recv(t *testing.T, timeout time.Duration) []byte { deadline := time.Now().Add(timeout) for { - timeout = deadline.Sub(time.Now()) + timeout = time.Until(deadline) if timeout <= 0 { return nil } - whole, frac := math.Modf(timeout.Seconds()) - tv := unix.Timeval{ - Sec: int64(whole), - Usec: int64(frac * float64(time.Second/time.Microsecond)), + usec := timeout.Microseconds() + if usec == 0 { + // Timeout is less than a microsecond; set usec to 1 to avoid + // blocking indefinitely. + usec = 1 } - // The following should never happen, but having this guard here is better - // than blocking indefinitely in the future. - if tv.Sec == 0 && tv.Usec == 0 { - t.Fatal("setting SO_RCVTIMEO to 0 means blocking indefinitely") + const microsInOne = 1e6 + tv := unix.Timeval{ + Sec: usec / microsInOne, + Usec: usec % microsInOne, } if err := unix.SetsockoptTimeval(s.fd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv); err != nil { t.Fatalf("can't setsockopt SO_RCVTIMEO: %s", err) |