Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
19 changes: 11 additions & 8 deletions testutil/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package testutil
import (
"context"
"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
Expand All @@ -14,7 +17,7 @@ func TryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
t.Helper()
select {
case <-ctx.Done():
t.Fatal("timeout")
require.Fail(t, "TryReceive: context expired")
var a A
return a
case a := <-c:
Expand All @@ -31,12 +34,12 @@ func RequireReceive[A any](ctx context.Context, t testing.TB, c <-chan A) A {
t.Helper()
select {
case <-ctx.Done():
t.Fatal("timeout")
require.Fail(t, "RequireReceive: context expired")
var a A
return a
case a, ok := <-c:
if !ok {
t.Fatal("channel closed")
require.Fail(t, "RequireReceive: channel closed")
}
return a
}
Expand All @@ -50,7 +53,7 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
t.Helper()
select {
case <-ctx.Done():
t.Fatal("timeout")
require.Fail(t, "RequireSend: context expired")
case c <- a:
// OK!
}
Expand All @@ -68,7 +71,7 @@ func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bo
t.Helper()
select {
case <-ctx.Done():
t.Error("timeout")
assert.Fail(t, "SoftTryReceive: context expired")
var a A
return a, false
case a := <-c:
Expand All @@ -86,12 +89,12 @@ func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, boo
t.Helper()
select {
case <-ctx.Done():
t.Error("timeout")
assert.Fail(t, "AssertReceive: context expired")
var a A
return a, false
case a, ok := <-c:
if !ok {
t.Error("channel closed")
assert.Fail(t, "AssertReceive: channel closed")
}
return a, ok
}
Expand All @@ -107,7 +110,7 @@ func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool
t.Helper()
select {
case <-ctx.Done():
t.Error("timeout")
assert.Fail(t, "AssertSend: context expired")
return false
case c <- a:
return true
Expand Down
Loading