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

Remove arbitrary sleeps from tests #87

Merged
merged 1 commit into from
Oct 27, 2021
Merged

Remove arbitrary sleeps from tests #87

merged 1 commit into from
Oct 27, 2021

Conversation

iand
Copy link
Contributor

@iand iand commented Oct 26, 2021

Also speeds up tests locally since most propagation happens in much less than 1 second

}
}

func waitUntil(ctx context.Context, condition func(context.Context) (bool, error), interval time.Duration) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use require.Eventually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's so simple and idiomatic that it hardly seems worth another dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Plus the context already controls the duration of the test so we don't have to guess a max duration to wait for.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's so simple and idiomatic that it hardly seems worth another dependency

We use require in almost every other libp2p package, so I'd much prefer to use it here as well.

Plus the context already controls the duration of the test so we don't have to guess a max duration to wait for.

As far as I can see, the context here doesn't actually do anything, as it's only canceled in defer. Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's cancelled by the test runner, controlled by go test -timeout so it can be adjusted externally.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's cancelled by the test runner, controlled by go test -timeout so it can be adjusted externally.

Is it? I've never heard of that feature. I wrote a test case:

func TestCancelation(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	<-ctx.Done()
	fmt.Println("test canceled", ctx.Err())
}

and ran with go test -timeout 1s. The test is just blowing up, and the context is not canceled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I misremembered, you need to use t.Deadline() and pass it to context.WithDeadline

@iand
Copy link
Contributor Author

iand commented Oct 26, 2021

Updated to use require.Eventually. We lose the ability to abort the test early on a non-expected error and there is an arbitrary 30 second cutoff but it makes the test more deterministic than the arbitrary sleeps.

@iand
Copy link
Contributor Author

iand commented Oct 26, 2021

Hmm. Seems my guess of 30s timeout is too low. Would much prefer not to have to make these kinds of guesses.

@iand
Copy link
Contributor Author

iand commented Oct 26, 2021

Increased to 5 minutes

@iand
Copy link
Contributor Author

iand commented Oct 26, 2021

Still failing. Using require.Eventually doesn't help fix these flakes.

@iand
Copy link
Contributor Author

iand commented Oct 26, 2021

Still seems flaky compared with the original change.

@marten-seemann
Copy link
Contributor

Let's use the original change then? Sorry for the confusion.

@iand
Copy link
Contributor Author

iand commented Oct 27, 2021

Rebased back to the original. It's not clear to me why require.Eventually performs so poorly when with this version all tests pass within 2 minutes.

@iand
Copy link
Contributor Author

iand commented Oct 27, 2021

@marten-seemann

@iand iand merged commit 55f7405 into master Oct 27, 2021
@iand iand deleted the chore/fix-tests branch October 27, 2021 13:05
@aschmahmann aschmahmann mentioned this pull request Dec 1, 2021
80 tasks
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