|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestContext(t *testing.T) { |
| 11 | + t.Run("with cancel", func(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + var ( |
| 15 | + sig = make(chan struct{}) |
| 16 | + ctx = context.Context(lite{context.TODO(), sig}) |
| 17 | + ) |
| 18 | + if ctx.Err() != nil { |
| 19 | + t.Error("invalid state") |
| 20 | + } |
| 21 | + |
| 22 | + ctx, cancel := context.WithCancel(ctx) |
| 23 | + if ctx.Err() != nil { |
| 24 | + t.Error("invalid state") |
| 25 | + } |
| 26 | + |
| 27 | + verify(t, ctx, cancel, sig) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("with deadline", func(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + var ( |
| 34 | + sig = make(chan struct{}) |
| 35 | + ctx = context.Context(lite{context.TODO(), sig}) |
| 36 | + ) |
| 37 | + if ctx.Err() != nil { |
| 38 | + t.Error("invalid state") |
| 39 | + } |
| 40 | + |
| 41 | + ctx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Hour)) |
| 42 | + if ctx.Err() != nil { |
| 43 | + t.Error("invalid state") |
| 44 | + } |
| 45 | + |
| 46 | + verify(t, ctx, cancel, sig) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("with timeout", func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + var ( |
| 53 | + sig = make(chan struct{}) |
| 54 | + ctx = context.Context(lite{context.TODO(), sig}) |
| 55 | + ) |
| 56 | + if ctx.Err() != nil { |
| 57 | + t.Error("invalid state") |
| 58 | + } |
| 59 | + |
| 60 | + ctx, cancel := context.WithTimeout(ctx, time.Hour) |
| 61 | + if ctx.Err() != nil { |
| 62 | + t.Error("invalid state") |
| 63 | + } |
| 64 | + |
| 65 | + verify(t, ctx, cancel, sig) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("with value", func(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + var ( |
| 72 | + sig = make(chan struct{}) |
| 73 | + ctx = context.Context(lite{context.TODO(), sig}) |
| 74 | + ) |
| 75 | + if ctx.Err() != nil { |
| 76 | + t.Error("invalid state") |
| 77 | + } |
| 78 | + |
| 79 | + ctx = context.WithValue(ctx, key{}, "value") |
| 80 | + if expected, obtained := "value", ctx.Value(key{}); obtained != expected { |
| 81 | + t.Errorf("expected: %q, obtained: %q", expected, obtained) |
| 82 | + } |
| 83 | + |
| 84 | + close(sig) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +// helpers |
| 89 | + |
| 90 | +func stop(timer *time.Timer) { |
| 91 | + if !timer.Stop() { |
| 92 | + select { |
| 93 | + case <-timer.C: |
| 94 | + default: |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func verify(t *testing.T, ctx context.Context, cancel context.CancelFunc, sig chan struct{}) { |
| 100 | + t.Helper() |
| 101 | + |
| 102 | + timer := time.NewTimer(schedule) |
| 103 | + close(sig) |
| 104 | + select { |
| 105 | + case <-timer.C: |
| 106 | + t.Error("invalid state") |
| 107 | + case <-ctx.Done(): |
| 108 | + if ctx.Err() == nil { |
| 109 | + t.Error("invalid state") |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + stop(timer) |
| 114 | + cancel() |
| 115 | +} |
| 116 | + |
| 117 | +type key struct{} |
| 118 | + |
| 119 | +var schedule = 10 * time.Duration(runtime.NumCPU()) * time.Millisecond |
0 commit comments