-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
628f799
commit de889e4
Showing
4 changed files
with
124 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package kadtest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func ReadItem[T any](t testing.TB, ctx context.Context, c <-chan T) T { | ||
t.Helper() | ||
|
||
select { | ||
case val, more := <-c: | ||
require.True(t, more, "channel closed unexpectedly") | ||
return val | ||
case <-ctx.Done(): | ||
t.Fatal("timeout reading item") | ||
return *new(T) | ||
} | ||
} | ||
|
||
// AssertClosed triggers a test failure if the given channel was not closed but | ||
// carried more values or a timeout occurs (given by the context). | ||
func AssertClosed[T any](t testing.TB, ctx context.Context, c <-chan T) { | ||
t.Helper() | ||
|
||
select { | ||
case _, more := <-c: | ||
assert.False(t, more) | ||
case <-ctx.Done(): | ||
t.Fatal("timeout closing channel") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters