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

Added test utilities. #672

Merged
merged 2 commits into from
Jul 31, 2024

Conversation

cody-littley
Copy link
Contributor

@cody-littley cody-littley commented Jul 30, 2024

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

  • I've made sure the lint is passing in this PR.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, in that case, please comment that they are not relevant.
  • Testing Strategy
    • Unit tests
    • Integration tests
    • This PR is not tested :(

Signed-off-by: Cody Littley <[email protected]>
@cody-littley cody-littley requested a review from ian-shim July 30, 2024 14:28
@cody-littley cody-littley self-assigned this Jul 30, 2024
@cody-littley cody-littley requested a review from jianoaix July 30, 2024 15:29
// 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() {
Copy link
Contributor

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?

Copy link
Contributor Author

@cody-littley cody-littley Jul 30, 2024

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.

Copy link
Contributor Author

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.

// 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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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) {
Copy link
Contributor

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

Copy link
Contributor Author

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]>
@cody-littley cody-littley merged commit 9cc44dd into Layr-Labs:master Jul 31, 2024
6 checks passed
@cody-littley cody-littley deleted the test-utils-fragment branch July 31, 2024 16:33
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