| 1 | //go:build linux || darwin |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "golang.org/x/sys/unix" |
| 10 | ) |
| 11 | |
| 12 | func TestPollRetriesEINTR(t *testing.T) { |
| 13 | calls := 0 |
| 14 | poll := func([]unix.PollFd, int) (int, error) { |
| 15 | calls++ |
| 16 | if calls <= 2 { |
| 17 | return 0, unix.EINTR |
| 18 | } |
| 19 | return 1, nil |
| 20 | } |
| 21 | n, e := pollRetry(nil, 0, poll) |
| 22 | if n != 1 || e != nil || calls != 3 { |
| 23 | t.Fatalf("interrupted poll not retried: n=%d err=%v calls=%d", n, e, calls) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestPollRetryBoundsZeroTimeout(t *testing.T) { |
| 28 | calls := 0 |
| 29 | poll := func([]unix.PollFd, int) (int, error) { |
| 30 | calls++ |
| 31 | if calls > 10*zeroTimeoutTries { |
| 32 | return 0, nil // without a cap, stop here so the assertion reports it |
| 33 | } |
| 34 | return 0, unix.EINTR |
| 35 | } |
| 36 | n, e := pollRetry(nil, 0, poll) |
| 37 | if n != 0 || e != unix.EINTR || calls != zeroTimeoutTries { |
| 38 | t.Fatalf("endless interruption not bounded: n=%d err=%v calls=%d", n, e, calls) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestPollRetryKeepsDeadline(t *testing.T) { |
| 43 | poll := func([]unix.PollFd, int) (int, error) { |
| 44 | time.Sleep(30 * time.Millisecond) |
| 45 | return 0, unix.EINTR |
| 46 | } |
| 47 | start := time.Now() |
| 48 | n, e := pollRetry(nil, 100*time.Millisecond, poll) |
| 49 | took := time.Since(start) |
| 50 | if n != 0 || e != nil || took > 200*time.Millisecond { |
| 51 | t.Fatalf("retries outlived the timeout: n=%d err=%v after %v", n, e, took) |
| 52 | } |
| 53 | } |
| 54 | |