Skip to content

Commit

Permalink
fix: chanResponseEmitter cancel being ineffective
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv authored and guseggert committed Sep 9, 2021
1 parent d89651f commit 87b5c50
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 8 additions & 0 deletions chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (r *chanResponse) Next() (interface{}, error) {
ctx = context.Background()
}

if err := ctx.Err(); err != nil {
return nil, err
}

select {
case v, ok := <-r.ch:
if !ok {
Expand Down Expand Up @@ -145,6 +149,10 @@ func (re *chanResponseEmitter) Emit(v interface{}) error {

ctx := re.req.Context

if err := ctx.Err(); err != nil {
return err
}

select {
case re.ch <- v:
if _, ok := v.(Single); ok {
Expand Down
3 changes: 1 addition & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ func TestCancel(t *testing.T) {
}

re, res := NewChanResponsePair(req)
cancel()

go func() {
err := re.Emit("abc")
Expand All @@ -328,8 +329,6 @@ func TestCancel(t *testing.T) {
close(wait)
}()

cancel()

_, err = res.Next()
if err != context.Canceled {
t.Errorf("res: expected context.Canceled but got %v", err)
Expand Down

0 comments on commit 87b5c50

Please sign in to comment.