Skip to content

feat(testutil): make utils t.Context() aware #17574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions testutil/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,37 @@ import (
// the channel is closed, the zero value of the channel type will be returned.
//
// Safety: Must only be called from the Go routine that created `t`.
func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (a A) {
t.Helper()
select {
case <-t.Context().Done():
t.Fatal("test timeout")
case <-ctx.Done():
t.Fatal("timeout")
var a A
return a
case a := <-c:
return a
t.Fatal("context timeout")
case a = <-c:
}
return a
}

// RequireReceive will receive a value from the chan and return it. If the
// context expires or the channel is closed before a value can be received,
// it will fail the test.
//
// Safety: Must only be called from the Go routine that created `t`.
func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (a A) {
t.Helper()
var ok bool
select {
case <-t.Context().Done():
t.Fatal("test timeout")
case <-ctx.Done():
t.Fatal("timeout")
var a A
return a
case a, ok := <-c:
t.Fatal("context timeout")
case a, ok = <-c:
if !ok {
t.Fatal("channel closed")
}
return a
}
return a
}

// RequireSend will send the given value over the chan and then return. If
Expand All @@ -49,8 +50,10 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
t.Helper()
select {
case <-t.Context().Done():
t.Fatal("test timeout")
case <-ctx.Done():
t.Fatal("timeout")
t.Fatal("context timeout")
case c <- a:
// OK!
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Context(t *testing.T, dur time.Duration) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), dur)
ctx, cancel := context.WithTimeout(t.Context(), dur)
t.Cleanup(cancel)
return ctx
}
3 changes: 3 additions & 0 deletions testutil/eventually.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func Eventually(ctx context.Context, t testing.TB, condition func(ctx context.Co
defer ticker.Stop()
for tick := ticker.C; ; {
select {
case <-t.Context().Done():
assert.NoError(t, t.Context().Err(), msg)
return false
case <-ctx.Done():
assert.NoError(t, ctx.Err(), msg)
return false
Expand Down
4 changes: 2 additions & 2 deletions testutil/eventually_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestEventually(t *testing.T) {
}()
return state > 2
}
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitShort)
defer cancel()
testutil.Eventually(ctx, t, condition, testutil.IntervalFast)
})
Expand All @@ -36,7 +36,7 @@ func TestEventually(t *testing.T) {
panicky := func() {
mockT := new(testing.T)
condition := func(_ context.Context) bool { return true }
testutil.Eventually(context.Background(), mockT, condition, testutil.IntervalFast)
testutil.Eventually(t.Context(), mockT, condition, testutil.IntervalFast)
}
assert.Panics(t, panicky)
})
Expand Down
2 changes: 2 additions & 0 deletions testutil/pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (tr *TerminalReader) ReadUntil(ctx context.Context, matcher func(line strin
if err != nil {
return err
}
case <-tr.t.Context().Done():
return tr.t.Context().Err()
case <-ctx.Done():
return ctx.Err()
}
Expand Down
Loading