Skip to content

Commit

Permalink
SendContext: channel has the highest priority
Browse files Browse the repository at this point in the history
  • Loading branch information
pashaosipyants authored Mar 24, 2021
1 parent 9bae729 commit bb49cbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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

0 comments on commit bb49cbf

Please sign in to comment.