| 1 |
package deadline |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
func TestDeadline(t *testing.T) { |
| 12 |
ctx, cancel := context.WithCancel(context.Background()) |
| 13 |
defer cancel() |
| 14 |
|
| 15 |
t.Run("Deadline", func(t *testing.T) { |
| 16 |
now := time.Now() |
| 17 |
|
| 18 |
ctx0, cancel0 := context.WithDeadline(ctx, now.Add(40*time.Millisecond)) |
| 19 |
defer cancel0() |
| 20 |
ctx1, cancel1 := context.WithDeadline(ctx, now.Add(60*time.Millisecond)) |
| 21 |
defer cancel1() |
| 22 |
d := New() |
| 23 |
d.Set(now.Add(50 * time.Millisecond)) |
| 24 |
|
| 25 |
ch := make(chan byte) |
| 26 |
go sendOnDone(ctx, ctx0.Done(), ch, 0) |
| 27 |
go sendOnDone(ctx, ctx1.Done(), ch, 1) |
| 28 |
go sendOnDone(ctx, d.Done(), ch, 2) |
| 29 |
|
| 30 |
calls := collectCh(ch, 3, 100*time.Millisecond) |
| 31 |
expectedCalls := []byte{0, 2, 1} |
| 32 |
if !bytes.Equal(calls, expectedCalls) { |
| 33 |
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls) |
| 34 |
} |
| 35 |
}) |
| 36 |
|
| 37 |
t.Run("DeadlineExtend", func(t *testing.T) { |
| 38 |
now := time.Now() |
| 39 |
|
| 40 |
ctx0, cancel0 := context.WithDeadline(ctx, now.Add(40*time.Millisecond)) |
| 41 |
defer cancel0() |
| 42 |
ctx1, cancel1 := context.WithDeadline(ctx, now.Add(60*time.Millisecond)) |
| 43 |
defer cancel1() |
| 44 |
d := New() |
| 45 |
d.Set(now.Add(50 * time.Millisecond)) |
| 46 |
d.Set(now.Add(70 * time.Millisecond)) |
| 47 |
|
| 48 |
ch := make(chan byte) |
| 49 |
go sendOnDone(ctx, ctx0.Done(), ch, 0) |
| 50 |
go sendOnDone(ctx, ctx1.Done(), ch, 1) |
| 51 |
go sendOnDone(ctx, d.Done(), ch, 2) |
| 52 |
|
| 53 |
calls := collectCh(ch, 3, 100*time.Millisecond) |
| 54 |
expectedCalls := []byte{0, 1, 2} |
| 55 |
if !bytes.Equal(calls, expectedCalls) { |
| 56 |
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls) |
| 57 |
} |
| 58 |
}) |
| 59 |
|
| 60 |
t.Run("DeadlinePretend", func(t *testing.T) { |
| 61 |
now := time.Now() |
| 62 |
|
| 63 |
ctx0, cancel0 := context.WithDeadline(ctx, now.Add(40*time.Millisecond)) |
| 64 |
defer cancel0() |
| 65 |
ctx1, cancel1 := context.WithDeadline(ctx, now.Add(60*time.Millisecond)) |
| 66 |
defer cancel1() |
| 67 |
d := New() |
| 68 |
d.Set(now.Add(50 * time.Millisecond)) |
| 69 |
d.Set(now.Add(30 * time.Millisecond)) |
| 70 |
|
| 71 |
ch := make(chan byte) |
| 72 |
go sendOnDone(ctx, ctx0.Done(), ch, 0) |
| 73 |
go sendOnDone(ctx, ctx1.Done(), ch, 1) |
| 74 |
go sendOnDone(ctx, d.Done(), ch, 2) |
| 75 |
|
| 76 |
calls := collectCh(ch, 3, 100*time.Millisecond) |
| 77 |
expectedCalls := []byte{2, 0, 1} |
| 78 |
if !bytes.Equal(calls, expectedCalls) { |
| 79 |
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls) |
| 80 |
} |
| 81 |
}) |
| 82 |
|
| 83 |
t.Run("DeadlineCancel", func(t *testing.T) { |
| 84 |
now := time.Now() |
| 85 |
|
| 86 |
ctx0, cancel0 := context.WithDeadline(ctx, now.Add(40*time.Millisecond)) |
| 87 |
defer cancel0() |
| 88 |
d := New() |
| 89 |
d.Set(now.Add(50 * time.Millisecond)) |
| 90 |
d.Set(time.Time{}) |
| 91 |
|
| 92 |
ch := make(chan byte) |
| 93 |
go sendOnDone(ctx, ctx0.Done(), ch, 0) |
| 94 |
go sendOnDone(ctx, d.Done(), ch, 1) |
| 95 |
|
| 96 |
calls := collectCh(ch, 2, 60*time.Millisecond) |
| 97 |
expectedCalls := []byte{0} |
| 98 |
if !bytes.Equal(calls, expectedCalls) { |
| 99 |
t.Errorf("Wrong order of deadline signal, expected: %v, got: %v", expectedCalls, calls) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func sendOnDone(ctx context.Context, done <-chan struct{}, dest chan byte, val byte) { |
| 105 |
select { |
| 106 |
case <-done: |
| 107 |
case <-ctx.Done(): |
| 108 |
return |
| 109 |
} |
| 110 |
dest <- val |
| 111 |
} |
| 112 |
|
| 113 |
func collectCh(ch <-chan byte, n int, timeout time.Duration) []byte { |
| 114 |
a := time.After(timeout) |
| 115 |
var calls []byte |
| 116 |
for len(calls) < n { |
| 117 |
select { |
| 118 |
case call := <-ch: |
| 119 |
calls = append(calls, call) |
| 120 |
case <-a: |
| 121 |
return calls |
| 122 |
} |
| 123 |
} |
| 124 |
return calls |
| 125 |
} |
| 126 |
|
| 127 |
func TestContext(t *testing.T) { |
| 128 |
t.Run("Cancel", func(t *testing.T) { |
| 129 |
d := New() |
| 130 |
select { |
| 131 |
case <-d.Done(): |
| 132 |
t.Fatal("Deadline unexpectedly done") |
| 133 |
case <-time.After(50 * time.Millisecond): |
| 134 |
} |
| 135 |
if err := d.Err(); err != nil { |
| 136 |
t.Errorf("Wrong Err(), expected: nil, got: %v", err) |
| 137 |
} |
| 138 |
d.Set(time.Unix(0, 1)) |
| 139 |
select { |
| 140 |
case <-d.Done(): |
| 141 |
case <-time.After(50 * time.Millisecond): |
| 142 |
t.Fatal("Timeout") |
| 143 |
} |
| 144 |
if err := d.Err(); !errors.Is(err, context.DeadlineExceeded) { |
| 145 |
t.Errorf("Wrong Err(), expected: %v, got: %v", context.DeadlineExceeded, err) |
| 146 |
} |
| 147 |
}) |
| 148 |
t.Run("Deadline", func(t *testing.T) { |
| 149 |
d := New() |
| 150 |
t0, expired0 := d.Deadline() |
| 151 |
if !t0.IsZero() { |
| 152 |
t.Errorf("Initial Deadline is expected to be 0, got %v", t0) |
| 153 |
} |
| 154 |
if expired0 { |
| 155 |
t.Error("Deadline is not expected to be expired at initial state") |
| 156 |
} |
| 157 |
|
| 158 |
dl := time.Unix(12345, 0) |
| 159 |
d.Set(dl) |
| 160 |
|
| 161 |
t1, expired1 := d.Deadline() |
| 162 |
if !t1.Equal(dl) { |
| 163 |
t.Errorf("Initial Deadline is expected to be %v, got %v", dl, t1) |
| 164 |
} |
| 165 |
if !expired1 { |
| 166 |
t.Error("Deadline is expected to be expired") |
| 167 |
} |
| 168 |
}) |
| 169 |
} |