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

testing: Avoid using context.Background #3949

Merged
merged 7 commits into from
Nov 5, 2020

Conversation

gauravgahlot
Copy link
Contributor

Fixes: #1878

@gauravgahlot
Copy link
Contributor Author

I have intentionally split the changes into commits based on the parent directory. This will make it easier for me to do any changes suggested during the review.

@dfawley Could you please add no release notes label and also take a look at the changes?

Copy link
Contributor

@easwars easwars left a comment

Choose a reason for hiding this comment

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

We don't want to change context.Background() with context.TODO(). What we want instead is what @dfawley mentioned in #1878 (comment).

Each test file (one per package) should define a defaultTestTimeout. And each test must create a local context with the appropriate timeout.

const defaultTestTimeout = 10 * time.Second

func TestXxx(t *testing.T) {
  ctx, cancel := context.WithTimeout(defaultTestTimeout)
  defer cancel()

  // Do stuff and pass `ctx` to functions which accept one.
}

And for places where we block on some event not happening, we should define a much shorter timeout (something of the order of 10ms), create a new context with that short deadline, and use that.

Hope this helps.

Thanks.

@gauravgahlot
Copy link
Contributor Author

Thanks for the explanation @easwars. I clearly misinterpreted @dfawley's comment.

I have made changes to the last commit. Please take a look and let me know if I'm doing it right this time.
If so, I will update the rest of the commits.

Thanks again. :)

Copy link
Contributor

@easwars easwars left a comment

Choose a reason for hiding this comment

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

I checked the last commit. The changes look mostly OK. I left some minor comments. Thank you so very much for doing this.

benchmark/primitives/context_test.go Outdated Show resolved Hide resolved
metadata/metadata_test.go Outdated Show resolved Hide resolved
stats/stats_test.go Outdated Show resolved Hide resolved
@dfawley dfawley changed the title Fixed: Avoid using context.Background in tests testing: Avoid using context.Background Oct 15, 2020
@gauravgahlot gauravgahlot force-pushed the context-fix branch 4 times, most recently from ac2ed64 to 9dd903c Compare October 30, 2020 05:54
Copy link
Contributor

@easwars easwars left a comment

Choose a reason for hiding this comment

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

Thank you so very much.

if err != nil {
t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
}
cancel()

ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use the ctx created in line 1272 here? And do a defer cancel()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right. I will do a fix and rebase. Thanks. :)

@gauravgahlot
Copy link
Contributor Author

Thank you so very much.

I'm super glad I could contribute. Please let me what I can pick next. 🚀

@easwars
Copy link
Contributor

easwars commented Nov 3, 2020

@gauravgahlot : Could you please take care of the test failure here: https://github.com/grpc/grpc-go/pull/3949/checks?check_run_id=1340435962. Thanks.

Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
@gauravgahlot gauravgahlot force-pushed the context-fix branch 2 times, most recently from 251441f to 4299949 Compare November 5, 2020 03:36
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
health/
metadata/
reflection/
stats/
@gauravgahlot
Copy link
Contributor Author

@easwars I have fixed the issue and it should be good to go now. 🚀

@easwars easwars merged commit d7a7a30 into grpc:master Nov 5, 2020
@gauravgahlot gauravgahlot deleted the context-fix branch November 5, 2020 17:29
davidkhala pushed a commit to Hyperledger-TWGC/grpc that referenced this pull request Dec 7, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid using context.Background in tests
3 participants