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

Consistent error value of context timeout on subscription Fetch() interface. #1011

Merged
merged 2 commits into from Jul 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions js.go
Expand Up @@ -2574,9 +2574,9 @@ func (sub *Subscription) Fetch(batch int, opts ...PullOpt) ([]*Msg, error) {
// Check if context not done already before making the request.
select {
case <-ctx.Done():
if ctx.Err() == context.Canceled {
if o.ctx != nil { // Timeout or Cancel triggered by context object option
err = ctx.Err()
} else {
} else { // Timeout triggered by timeout option
err = ErrTimeout
}
default:
Expand Down
16 changes: 16 additions & 0 deletions test/js_test.go
Expand Up @@ -6867,6 +6867,22 @@ func testJetStreamFetchContext(t *testing.T, srvs ...*jsServer) {
t.Errorf("Expected %d pending messages, got: %d", pending, total)
}
})

t.Run("MaxWait timeout should return nats error", func(t *testing.T) {
_, err := sub.Fetch(1, nats.MaxWait(1*time.Nanosecond))
if !errors.Is(err, nats.ErrTimeout) {
t.Fatalf("Expect ErrTimeout, got err=%#v", err)
}
})

t.Run("Context timeout should return context error", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
_, err := sub.Fetch(1, nats.Context(ctx))
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expect context.DeadlineExceeded, got err=%#v", err)
}
})
}

func TestJetStreamSubscribeContextCancel(t *testing.T) {
Expand Down