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

Conversation

wdhongtw
Copy link
Contributor

@wdhongtw wdhongtw commented Jul 1, 2022

When using Subscription.Fetch() with nats.Context(ctx) option,
if context timeout receiving any message, Fetch will return error.

There are two possible value of error here, nats.ErrTimeout and context.DeadlineExceeded, based on some timing in the process/gorouting. If context timeout before subscription object trying to fetch message, nats.ErrTimeout is returned. Otherwise, context.DeadlineExceeded is returned.

This PR make the error value of fetch timeout predicable, if Fetch is called with nats.Context(ctx), timeout always return
context.DeadlineExceeded; if called with nats.MaxWait(timeout), timeout always return nats.ErrTimeout.

@wdhongtw wdhongtw marked this pull request as draft July 1, 2022 07:25
@coveralls
Copy link

coveralls commented Jul 1, 2022

Coverage Status

Coverage increased (+0.02%) to 83.975% when pulling 37edc5e on wdhongtw:fix-fetch-context-timeout into d29a40a on nats-io:main.

@wdhongtw wdhongtw marked this pull request as ready for review July 1, 2022 16:42
js_test.go Outdated
t.Fatalf("Expect ErrTimeout, got err=%#v", err)
}
})
t.Run("Context timeout should return context error", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@wallyqs wallyqs Jul 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, I see now that this test is the one that has the coverage for the issue. In the one I mentioned the context has not yet timed out but here it has.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

js_test.go Outdated
@@ -939,6 +941,21 @@ func TestJetStreamExpiredPullRequests(t *testing.T) {
t.Fatalf("Expected error and wait for 250ms, got err=%v and dur=%v", err, dur)
}
}

t.Run("MaxWait timeout should return nats error", func(t *testing.T) {
Copy link
Member

@wallyqs wallyqs Jul 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move these test blocks to be as part of this other test? https://github.com/nats-io/nats.go/blob/main/test/js_test.go#L6869

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :D

Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Just one request to move the tests to be under test/js_test.go under the TestJetStreamPullSubscribeFetchContext tests.

Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Follow TDD principle, write test watch it fails.
If user provides context object and context timeout,
always return the timeout error from context.
@wdhongtw
Copy link
Contributor Author

wdhongtw commented Jul 2, 2022

I accidentally leaved an empty line in js_test.go, so I force push the branch again. :D

Copy link
Collaborator

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@wallyqs wallyqs merged commit 8a4b9f4 into nats-io:main Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants