|
| 1 | +package contextutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + shortDuration = 1 * time.Millisecond // a reasonable duration to block in a test |
| 11 | + veryLongDuration = 1000 * time.Hour // an arbitrary upper bound on the test's running time |
| 12 | +) |
| 13 | + |
| 14 | +func TestAfterFuncCalledAfterCancel(t *testing.T) { |
| 15 | + ctx, cancel := context.WithCancel(context.Background()) |
| 16 | + donec := make(chan struct{}) |
| 17 | + stop := afterFunc(ctx, func() { |
| 18 | + close(donec) |
| 19 | + }) |
| 20 | + select { |
| 21 | + case <-donec: |
| 22 | + t.Fatalf("AfterFunc called before context is done") |
| 23 | + case <-time.After(shortDuration): |
| 24 | + } |
| 25 | + cancel() |
| 26 | + select { |
| 27 | + case <-donec: |
| 28 | + case <-time.After(veryLongDuration): |
| 29 | + t.Fatalf("AfterFunc not called after context is canceled") |
| 30 | + } |
| 31 | + if stop() { |
| 32 | + t.Fatalf("stop() = true, want false") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestAfterFuncCalledAfterTimeout(t *testing.T) { |
| 37 | + ctx, cancel := context.WithTimeout(context.Background(), shortDuration) |
| 38 | + defer cancel() |
| 39 | + donec := make(chan struct{}) |
| 40 | + afterFunc(ctx, func() { |
| 41 | + close(donec) |
| 42 | + }) |
| 43 | + select { |
| 44 | + case <-donec: |
| 45 | + case <-time.After(veryLongDuration): |
| 46 | + t.Fatalf("AfterFunc not called after context is canceled") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestAfterFuncCalledImmediately(t *testing.T) { |
| 51 | + ctx, cancel := context.WithCancel(context.Background()) |
| 52 | + cancel() |
| 53 | + donec := make(chan struct{}) |
| 54 | + afterFunc(ctx, func() { |
| 55 | + close(donec) |
| 56 | + }) |
| 57 | + select { |
| 58 | + case <-donec: |
| 59 | + case <-time.After(veryLongDuration): |
| 60 | + t.Fatalf("AfterFunc not called for already-canceled context") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestAfterFuncNotCalledAfterStop(t *testing.T) { |
| 65 | + ctx, cancel := context.WithCancel(context.Background()) |
| 66 | + donec := make(chan struct{}) |
| 67 | + stop := afterFunc(ctx, func() { |
| 68 | + close(donec) |
| 69 | + }) |
| 70 | + if !stop() { |
| 71 | + t.Fatalf("stop() = false, want true") |
| 72 | + } |
| 73 | + cancel() |
| 74 | + select { |
| 75 | + case <-donec: |
| 76 | + t.Fatalf("AfterFunc called for already-canceled context") |
| 77 | + case <-time.After(shortDuration): |
| 78 | + } |
| 79 | + if stop() { |
| 80 | + t.Fatalf("stop() = true, want false") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// This test verifies that canceling a context does not block waiting for AfterFuncs to finish. |
| 85 | +func TestAfterFuncCalledAsynchronously(t *testing.T) { |
| 86 | + ctx, cancel := context.WithCancel(context.Background()) |
| 87 | + donec := make(chan struct{}) |
| 88 | + stop := afterFunc(ctx, func() { |
| 89 | + // The channel send blocks until donec is read from. |
| 90 | + donec <- struct{}{} |
| 91 | + }) |
| 92 | + defer stop() |
| 93 | + cancel() |
| 94 | + // After cancel returns, read from donec and unblock the AfterFunc. |
| 95 | + select { |
| 96 | + case <-donec: |
| 97 | + case <-time.After(veryLongDuration): |
| 98 | + t.Fatalf("AfterFunc not called after context is canceled") |
| 99 | + } |
| 100 | +} |
0 commit comments