test(testutil): improve chan.go error visibility (#18406)

This commit is contained in:
Mathias Fredriksson
2025-06-17 17:39:31 +03:00
committed by GitHub
parent ebc769f328
commit b9ac16cb40
+11 -8
View File
@@ -3,6 +3,9 @@ package testutil
import ( import (
"context" "context"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
// TryReceive will attempt to receive a value from the chan and return it. If // 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 {
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("timeout") require.Fail(t, "TryReceive: context expired")
var a A var a A
return a return a
case a := <-c: case a := <-c:
@@ -31,12 +34,12 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("timeout") require.Fail(t, "RequireReceive: context expired")
var a A var a A
return a return a
case a, ok := <-c: case a, ok := <-c:
if !ok { if !ok {
t.Fatal("channel closed") require.Fail(t, "RequireReceive: channel closed")
} }
return a return a
} }
@@ -50,7 +53,7 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("timeout") require.Fail(t, "RequireSend: context expired")
case c <- a: case c <- a:
// OK! // OK!
} }
@@ -68,7 +71,7 @@ func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bo
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Error("timeout") assert.Fail(t, "SoftTryReceive: context expired")
var a A var a A
return a, false return a, false
case a := <-c: case a := <-c:
@@ -86,12 +89,12 @@ func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, boo
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Error("timeout") assert.Fail(t, "AssertReceive: context expired")
var a A var a A
return a, false return a, false
case a, ok := <-c: case a, ok := <-c:
if !ok { if !ok {
t.Error("channel closed") assert.Fail(t, "AssertReceive: channel closed")
} }
return a, ok return a, ok
} }
@@ -107,7 +110,7 @@ func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool
t.Helper() t.Helper()
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Error("timeout") assert.Fail(t, "AssertSend: context expired")
return false return false
case c <- a: case c <- a:
return true return true