-
Notifications
You must be signed in to change notification settings - Fork 59
/
context_test.go
243 lines (212 loc) · 6.38 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package clockwork
import (
"context"
"errors"
"reflect"
"testing"
"time"
)
func TestContextOps(t *testing.T) {
t.Parallel()
ctx := context.Background()
assertIsType(t, NewRealClock(), FromContext(ctx))
ctx = AddToContext(ctx, NewFakeClock())
assertIsType(t, NewFakeClock(), FromContext(ctx))
}
func assertIsType(t *testing.T, expectedType, object any) {
t.Helper()
if reflect.TypeOf(object) != reflect.TypeOf(expectedType) {
t.Fatalf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object))
}
}
func TestWithDeadlineDone(t *testing.T) {
t.Parallel()
cases := []struct {
name string
start, deadline time.Time
action func(cancelParent, cancelChild context.CancelFunc, clock *FakeClock)
want error
}{
{
name: "canceling parent cancels child",
start: time.Unix(10, 0),
deadline: time.Unix(20, 0),
action: func(cancelParent, _ context.CancelFunc, _ *FakeClock) {
cancelParent()
},
want: context.Canceled,
},
{
name: "canceling child cancels child",
start: time.Unix(10, 0),
deadline: time.Unix(20, 0),
action: func(_, cancelChild context.CancelFunc, _ *FakeClock) {
cancelChild()
},
want: context.Canceled,
},
{
name: "advancing past deadline cancels child",
start: time.Unix(10, 0),
deadline: time.Unix(20, 0),
action: func(_, _ context.CancelFunc, clock *FakeClock) {
clock.Advance(10 * time.Second)
},
want: context.DeadlineExceeded,
},
}
// Background context for catching timeouts.
base, cancelBase := context.WithTimeout(context.Background(), timeout)
defer cancelBase()
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
parent, cancelParent := context.WithCancel(base)
clock := NewFakeClockAt(tc.start)
child, cancelChild := WithDeadline(parent, clock, tc.deadline)
select {
case <-child.Done():
t.Fatal("WithDeadline context finished early.")
default:
}
tc.action(cancelParent, cancelChild, clock)
select {
case <-child.Done():
if got := child.Err(); !errors.Is(got, tc.want) {
t.Errorf("WithDeadline context returned %v, want %v", got, tc.want)
}
case <-base.Done():
t.Error("WithDeadline context was never canceled.")
}
})
}
}
func TestWithTimeoutDone(t *testing.T) {
t.Parallel()
cases := []struct {
name string
start time.Time
timeout time.Duration
action func(cancelParent, cancelChild context.CancelFunc, clock *FakeClock)
want error
}{
{
name: "canceling parent cancels child",
start: time.Unix(10, 0),
timeout: 10 * time.Second,
action: func(cancelParent, _ context.CancelFunc, _ *FakeClock) {
cancelParent()
},
want: context.Canceled,
},
{
name: "canceling child cancels child",
start: time.Unix(10, 0),
timeout: 10 * time.Second,
action: func(_, cancelChild context.CancelFunc, _ *FakeClock) {
cancelChild()
},
want: context.Canceled,
},
{
name: "advancing past deadline cancels child",
start: time.Unix(10, 0),
timeout: 10 * time.Second,
action: func(_, _ context.CancelFunc, clock *FakeClock) {
clock.Advance(10 * time.Second)
},
want: context.DeadlineExceeded,
},
}
// Background context for catching timeouts.
background, cancelBase := context.WithTimeout(context.Background(), timeout)
defer cancelBase()
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
parent, cancelParent := context.WithCancel(background)
clock := NewFakeClockAt(tc.start)
child, cancelChild := WithTimeout(parent, clock, tc.timeout)
select {
case <-child.Done():
t.Fatal("WithTimeout context finished early.")
default:
}
tc.action(cancelParent, cancelChild, clock)
select {
case <-child.Done():
if got := child.Err(); !errors.Is(got, tc.want) {
t.Errorf("WithTimeout context returned %v, want %v", got, tc.want)
}
case <-background.Done():
t.Error("WithTimeout context was never canceled.")
}
})
}
}
func TestParentCancellationIsRespected(t *testing.T) {
t.Parallel()
cases := []struct {
name string
contextFunc func(context.Context, *FakeClock) (context.Context, context.CancelFunc)
requireContextDeadlineExceeded bool
}{
{
name: "WithDeadline in the future",
contextFunc: func(ctx context.Context, fc *FakeClock) (context.Context, context.CancelFunc) {
return WithDeadline(ctx, fc, time.Now().Add(time.Hour))
},
// The FakeClock does not hit its deadline, so the error must be context.DeadlineExceeded.
requireContextDeadlineExceeded: true,
},
{
name: "WithDeadline in the past",
contextFunc: func(ctx context.Context, fc *FakeClock) (context.Context, context.CancelFunc) {
return WithDeadline(ctx, fc, time.Now().Add(-time.Hour))
},
},
{
name: "WithTimeout in the future",
contextFunc: func(ctx context.Context, fc *FakeClock) (context.Context, context.CancelFunc) {
return WithTimeout(ctx, fc, time.Hour)
},
// The FakeClock does not hit its deadline, so the error must be context.DeadlineExceeded.
requireContextDeadlineExceeded: true,
},
{
name: "WithTimeout immediately",
contextFunc: func(ctx context.Context, fc *FakeClock) (context.Context, context.CancelFunc) {
return WithTimeout(ctx, fc, 0)
},
},
{
name: "WithTimeout in the past",
contextFunc: func(ctx context.Context, fc *FakeClock) (context.Context, context.CancelFunc) {
return WithTimeout(ctx, fc, -time.Hour)
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
base, cancelBase := context.WithTimeout(context.Background(), timeout)
defer cancelBase()
// Parent context hits deadline effectively immediately.
parent, cancelParent := context.WithTimeout(base, time.Nanosecond)
defer cancelParent()
clock := NewFakeClockAt(time.Unix(10, 0))
child, cancelChild := tc.contextFunc(parent, clock)
defer cancelChild()
select {
case <-child.Done():
case <-base.Done():
t.Fatal("context did not respect parnet deadline")
}
if err := child.Err(); !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("errors.Is(Context.Err(), context.DeadlineExceeded) == falst, want true, error: %v", err)
}
if tc.requireContextDeadlineExceeded {
if err := child.Err(); errors.Is(err, ErrFakeClockDeadlineExceeded) {
t.Errorf("errors.Is(Context.Err(), ErrFakeClockDeadlineExceeded) == true, want false, error: %v", err)
}
}
})
}
}