Skip to content

Commit fa4063c

Browse files
committed
feat(testutil): make utils t.Context() aware
1 parent e0483e3 commit fa4063c

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

testutil/chan.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,37 @@ import (
1010
// the channel is closed, the zero value of the channel type will be returned.
1111
//
1212
// Safety: Must only be called from the Go routine that created `t`.
13-
func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
13+
func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (a A) {
1414
t.Helper()
1515
select {
16+
case <-t.Context().Done():
17+
t.Fatal("test timeout")
1618
case <-ctx.Done():
17-
t.Fatal("timeout")
18-
var a A
19-
return a
20-
case a := <-c:
21-
return a
19+
t.Fatal("context timeout")
20+
case a = <-c:
2221
}
22+
return a
2323
}
2424

2525
// RequireReceive will receive a value from the chan and return it. If the
2626
// context expires or the channel is closed before a value can be received,
2727
// it will fail the test.
2828
//
2929
// Safety: Must only be called from the Go routine that created `t`.
30-
func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
30+
func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (a A) {
3131
t.Helper()
32+
var ok bool
3233
select {
34+
case <-t.Context().Done():
35+
t.Fatal("test timeout")
3336
case <-ctx.Done():
34-
t.Fatal("timeout")
35-
var a A
36-
return a
37-
case a, ok := <-c:
37+
t.Fatal("context timeout")
38+
case a, ok = <-c:
3839
if !ok {
3940
t.Fatal("channel closed")
4041
}
41-
return a
4242
}
43+
return a
4344
}
4445

4546
// RequireSend will send the given value over the chan and then return. If
@@ -49,8 +50,10 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
4950
func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
5051
t.Helper()
5152
select {
53+
case <-t.Context().Done():
54+
t.Fatal("test timeout")
5255
case <-ctx.Done():
53-
t.Fatal("timeout")
56+
t.Fatal("context timeout")
5457
case c <- a:
5558
// OK!
5659
}

testutil/ctx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func Context(t *testing.T, dur time.Duration) context.Context {
10-
ctx, cancel := context.WithTimeout(context.Background(), dur)
10+
ctx, cancel := context.WithTimeout(t.Context(), dur)
1111
t.Cleanup(cancel)
1212
return ctx
1313
}

testutil/eventually.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func Eventually(ctx context.Context, t testing.TB, condition func(ctx context.Co
4141
defer ticker.Stop()
4242
for tick := ticker.C; ; {
4343
select {
44+
case <-t.Context().Done():
45+
assert.NoError(t, t.Context().Err(), msg)
46+
return false
4447
case <-ctx.Done():
4548
assert.NoError(t, ctx.Err(), msg)
4649
return false

testutil/eventually_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestEventually(t *testing.T) {
2525
}()
2626
return state > 2
2727
}
28-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
28+
ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitShort)
2929
defer cancel()
3030
testutil.Eventually(ctx, t, condition, testutil.IntervalFast)
3131
})
@@ -36,7 +36,7 @@ func TestEventually(t *testing.T) {
3636
panicky := func() {
3737
mockT := new(testing.T)
3838
condition := func(_ context.Context) bool { return true }
39-
testutil.Eventually(context.Background(), mockT, condition, testutil.IntervalFast)
39+
testutil.Eventually(t.Context(), mockT, condition, testutil.IntervalFast)
4040
}
4141
assert.Panics(t, panicky)
4242
})

testutil/pty.go

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (tr *TerminalReader) ReadUntil(ctx context.Context, matcher func(line strin
8383
if err != nil {
8484
return err
8585
}
86+
case <-tr.t.Context().Done():
87+
return tr.t.Context().Err()
8688
case <-ctx.Done():
8789
return ctx.Err()
8890
}

0 commit comments

Comments
 (0)