Skip to content

fix: Move timeout ctx closer to use in tests, increase timeout #3109

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

Merged
merged 1 commit into from
Jul 22, 2022

Conversation

mafredri
Copy link
Member

Some contexts were moved closer to use so that test setup doesn't affect
timeout. And timeout was increased for some others to avoid flakyness
due to slow test runners.

This CI run ran into context deadline exceeded:
https://github.com/coder/coder/runs/7456533733?check_suite_focus=true

Some contexts were moved closer to use so that test setup doesn't affect
timeout. And timeout was increased for some others to avoid flakyness
due to slow test runners.
@mafredri mafredri requested a review from a team as a code owner July 22, 2022 10:03
@mafredri mafredri self-assigned this Jul 22, 2022
@mafredri mafredri requested a review from a team July 22, 2022 10:03
@@ -65,6 +63,9 @@ func TestProvisionerJobLogs_Unit(t *testing.T) {
{ID: uuid.New(), JobID: jobID, Stage: "Stage3"},
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Copy link
Member

Choose a reason for hiding this comment

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

Why defer here over t.Cleanup?

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel tests should prefer to read like idiomatic Go code, t.Cleanup is useful for helper functions but has no benefit over defer.

It could also lead to logic bugs where there are defers and t.Cleanup mixed, because the order of execution would differ.

defer close1()
t.Cleanup(close2)
defer close3()

This would execute in the following order: close3, close1, close2 likely leading to a logic bug.

@@ -48,7 +48,7 @@ func TestCaching(t *testing.T) {
defer srv.Close()

// Create a context
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
Copy link
Member

Choose a reason for hiding this comment

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

(Idea, out-of-scope): should we have a "standard" set of timeouts we use for all unit tests?
e.g. "short" -> 1 second, "medium" -> 5 seconds, "long", 10 seconds?

Copy link
Member Author

Choose a reason for hiding this comment

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

I really like that idea! It's all over the place now and doesn't allow us to easily adjust for slow/fast platforms.

@mafredri mafredri removed the request for review from a team July 22, 2022 14:41
@mafredri mafredri merged commit ef7d357 into main Jul 22, 2022
@mafredri mafredri deleted the mafredri/fix-test-ctx-timeouts branch July 22, 2022 14:42
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.

2 participants