-
Notifications
You must be signed in to change notification settings - Fork 193
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
Added test utilities. #672
Added test utilities. #672
Conversation
Signed-off-by: Cody Littley <[email protected]>
common/testutils/test_utils.go
Outdated
// InitializeRandom initializes the random number generator. Prints the seed so that the test can be rerun | ||
// deterministically. Replace a call to this method with a call to initializeRandomWithSeed to rerun a test | ||
// with a specific seed. | ||
func InitializeRandom() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we always use InitializeRandomWithSeed
and pass in random seed instead of having two separate methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've simplified this a little using varargs. You can now do InitializeRandom()
or InitializeRandom(1234)
.
This pattern is intentional. In general, you want to use a different random seed each time you test. But if you get a test failure, it's useful to go back and attempt a reproduction using the original seed that failed. In general, I wouldn't expect the caller to provide a seed unless they are attempting to debug a prior failed run.
If you are still uncomfortable with this method, let's discuss.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed with Ian offline, he is ok with this as it currently is.
common/testutils/test_utils.go
Outdated
// AssertEventuallyTrue asserts that a condition is true within a given duration. Repeatably checks the condition. | ||
func AssertEventuallyTrue(t *testing.T, condition func() bool, duration time.Duration, debugInfo ...any) { | ||
start := time.Now() | ||
for time.Since(start) < duration { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use a channel/a context/select statement to achieve the same thing instead of polling every millisecond?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've refactored this to use a select clause. Still learning best practices, please let me know if it needs more tweaks.
|
||
// ExecuteWithTimeout executes a function with a timeout. | ||
// Panics if the function does not complete within the given duration. | ||
func ExecuteWithTimeout(f func(), duration time.Duration, debugInfo ...any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe more idiomatic way of doing this is by using a context with a timeline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to use a context with a timeout.
Signed-off-by: Cody Littley <[email protected]>
Why are these changes needed?
This PR is code that I am splitting out of another PR that got too large: #666
Provides a small number of useful testing utility functions.
Checks