Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendContext fix #300

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ func (i Item) SendBlocking(ch chan<- Item) {
// It returns a boolean to indicate whether the item was sent.
func (i Item) SendContext(ctx context.Context, ch chan<- Item) bool {
select {
case <-ctx.Done():
case <-ctx.Done(): // Context's done channel has the highest priority
return false
case ch <- i:
return true
default:
select {
case <-ctx.Done():
return false
case ch <- i:
return true
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func Test_Item_SendContext_True(t *testing.T) {
assert.True(t, Of(5).SendContext(ctx, ch))
}

func Test_Item_SendContext_False(t *testing.T) {
defer goleak.VerifyNone(t)
ch := make(chan Item, 1)
defer close(ch)
ctx, cancel := context.WithCancel(context.Background())
cancel()
assert.False(t, Of(5).SendContext(ctx, ch))
}

func Test_Item_SendNonBlocking(t *testing.T) {
defer goleak.VerifyNone(t)
ch := make(chan Item, 1)
Expand Down