Skip to content

chore: add testutil.Eventually and friends #3389

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 7 commits into from
Aug 5, 2022
Merged

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Aug 5, 2022

This PR adds a testutil function aimed to replace require.Eventually.
Should be useful in conjunction with #3381

Before:

require.Eventually(t, func() bool { ... }, testutil.WaitShort, testutil.IntervalFast)

After:

require.True(t, testutil.EventuallyShort(t, func(ctx context.Context) bool { ... }))

// or the full incantation if you need more control
ctx, cancel := context.WithTimeout(ctx.Background(), testutil.WaitLong)
require.True(t, testutil.Eventually(t, ctx, func(ctx context.Context) bool { ... }, testutil.IntervalSlow))

@johnstcn johnstcn requested review from mafredri and a team August 5, 2022 11:47
@johnstcn johnstcn changed the title feat: add testutil.Eventually feat: add testutil.Eventually and friends Aug 5, 2022
@johnstcn johnstcn changed the title feat: add testutil.Eventually and friends chore: add testutil.Eventually and friends Aug 5, 2022
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Had some comments/suggestions but nice work!

@johnstcn johnstcn requested a review from mafredri August 5, 2022 13:38
@johnstcn johnstcn self-assigned this Aug 5, 2022
// the case in order to avoid potentially waiting forever.
//
// condition is not run in a goroutine; use the provided
// context argument for cancellation if required.
Copy link
Member

Choose a reason for hiding this comment

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

Great docs ❤️!

Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
case <-ctx.Done():
assert.NoError(t, ctx.Err(), "Eventually timed out")
return false
case <-tick:
Copy link
Member

Choose a reason for hiding this comment

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

My head must've been background processing this PR review, just realized there's a race here between ticks and ctx.Done(), we should add an additional check for ctx.Done() after receiving the tick, otherwise we may run the condition as many additional times as the go runtime decides to prioritize the tick.

@johnstcn johnstcn merged commit 01fe5e6 into main Aug 5, 2022
@johnstcn johnstcn deleted the cj/testutil-eventually branch August 5, 2022 15:34
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