Skip to content

Commit b9ac16c

Browse files
authored
test(testutil): improve chan.go error visibility (#18406)
1 parent ebc769f commit b9ac16c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

testutil/chan.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package testutil
33
import (
44
"context"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
69
)
710

811
// TryReceive will attempt to receive a value from the chan and return it. If
@@ -14,7 +17,7 @@ func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
1417
t.Helper()
1518
select {
1619
case <-ctx.Done():
17-
t.Fatal("timeout")
20+
require.Fail(t, "TryReceive: context expired")
1821
var a A
1922
return a
2023
case a := <-c:
@@ -31,12 +34,12 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
3134
t.Helper()
3235
select {
3336
case <-ctx.Done():
34-
t.Fatal("timeout")
37+
require.Fail(t, "RequireReceive: context expired")
3538
var a A
3639
return a
3740
case a, ok := <-c:
3841
if !ok {
39-
t.Fatal("channel closed")
42+
require.Fail(t, "RequireReceive: channel closed")
4043
}
4144
return a
4245
}
@@ -50,7 +53,7 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
5053
t.Helper()
5154
select {
5255
case <-ctx.Done():
53-
t.Fatal("timeout")
56+
require.Fail(t, "RequireSend: context expired")
5457
case c <- a:
5558
// OK!
5659
}
@@ -68,7 +71,7 @@ func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bo
6871
t.Helper()
6972
select {
7073
case <-ctx.Done():
71-
t.Error("timeout")
74+
assert.Fail(t, "SoftTryReceive: context expired")
7275
var a A
7376
return a, false
7477
case a := <-c:
@@ -86,12 +89,12 @@ func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, boo
8689
t.Helper()
8790
select {
8891
case <-ctx.Done():
89-
t.Error("timeout")
92+
assert.Fail(t, "AssertReceive: context expired")
9093
var a A
9194
return a, false
9295
case a, ok := <-c:
9396
if !ok {
94-
t.Error("channel closed")
97+
assert.Fail(t, "AssertReceive: channel closed")
9598
}
9699
return a, ok
97100
}
@@ -107,7 +110,7 @@ func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool
107110
t.Helper()
108111
select {
109112
case <-ctx.Done():
110-
t.Error("timeout")
113+
assert.Fail(t, "AssertSend: context expired")
111114
return false
112115
case c <- a:
113116
return true

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy