@@ -3,6 +3,9 @@ package testutil
3
3
import (
4
4
"context"
5
5
"testing"
6
+
7
+ "github.com/stretchr/testify/assert"
8
+ "github.com/stretchr/testify/require"
6
9
)
7
10
8
11
// 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 {
14
17
t .Helper ()
15
18
select {
16
19
case <- ctx .Done ():
17
- t . Fatal ( "timeout " )
20
+ assert . Fail ( t , "TryReceive: context expired " )
18
21
var a A
19
22
return a
20
23
case a := <- c :
@@ -31,12 +34,12 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
31
34
t .Helper ()
32
35
select {
33
36
case <- ctx .Done ():
34
- t . Fatal ( "timeout " )
37
+ require . Fail ( t , "RequireReceive: context expired " )
35
38
var a A
36
39
return a
37
40
case a , ok := <- c :
38
41
if ! ok {
39
- t . Fatal ( " channel closed" )
42
+ require . Fail ( t , "RequireReceive: channel closed" )
40
43
}
41
44
return a
42
45
}
@@ -50,7 +53,7 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
50
53
t .Helper ()
51
54
select {
52
55
case <- ctx .Done ():
53
- t . Fatal ( "timeout " )
56
+ require . Fail ( t , "RequireSend: context expired " )
54
57
case c <- a :
55
58
// OK!
56
59
}
@@ -68,7 +71,7 @@ func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bo
68
71
t .Helper ()
69
72
select {
70
73
case <- ctx .Done ():
71
- t . Error ( "timeout " )
74
+ assert . Fail ( t , "SoftTryReceive: context expired " )
72
75
var a A
73
76
return a , false
74
77
case a := <- c :
@@ -86,12 +89,12 @@ func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, boo
86
89
t .Helper ()
87
90
select {
88
91
case <- ctx .Done ():
89
- t . Error ( "timeout " )
92
+ assert . Fail ( t , "AssertReceive: context expired " )
90
93
var a A
91
94
return a , false
92
95
case a , ok := <- c :
93
96
if ! ok {
94
- t . Error ( " channel closed" )
97
+ assert . Fail ( t , "AssertReceive: channel closed" )
95
98
}
96
99
return a , ok
97
100
}
@@ -107,7 +110,7 @@ func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool
107
110
t .Helper ()
108
111
select {
109
112
case <- ctx .Done ():
110
- t . Error ( "timeout " )
113
+ assert . Fail ( t , "AssertSend: context expired " )
111
114
return false
112
115
case c <- a :
113
116
return true
0 commit comments