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

make time unit test more reliable #3

Merged
merged 1 commit into from
Nov 10, 2024
Merged

Conversation

edyoung
Copy link
Collaborator

@edyoung edyoung commented Nov 10, 2024

When running the unit tests on a Windows machine, I see the failure

--- FAIL: TestClock (0.01s)
time_utils_test.go:15:
Error Trace: C:/onebranch/fair/pkg/utils/time_utils_test.go:15
Error: Should be true
Test: TestClock

I believe this is because t1 and t2 happen closer together than the resolution of the clock so they evaluate as the same time, rather than one being later than the other.

This change allows t2 to be equal or greater than t1.

I didn't observe a failure in the second assert, but for consistency it seems reasonable to allow the delta to be exactly 10ms.

Copy link
Owner

@satmihir satmihir left a comment

Choose a reason for hiding this comment

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

Thank you so much for fixing this!

@satmihir satmihir merged commit 50d8989 into satmihir:main Nov 10, 2024
1 check passed
@@ -12,11 +12,11 @@ func TestClock(t *testing.T) {
t1 := time.Now()
t2 := clk.Now()

assert.True(t, t2.After(t1))
assert.True(t, t2.Compare(t1) >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's worth trying

Suggested change
assert.True(t, t2.Compare(t1) >= 0)
assert.GreaterOrEqual(t, t2, t1)

https://pkg.go.dev/github.com/stretchr/testify/assert#GreaterOrEqualf

As you can see here, testify supports time.Time

https://github.com/stretchr/testify/blob/v1.9.0/assert%2Fassertion_compare.go#L317-L329

The output will be way better in case of error.

You can simulate it by inverting t1 and t2 temporarily and locally

@@ -12,11 +12,11 @@ func TestClock(t *testing.T) {
t1 := time.Now()
t2 := clk.Now()

assert.True(t, t2.After(t1))
assert.True(t, t2.Compare(t1) >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please note another way to write it could have been simpler

Suggested change
assert.True(t, t2.Compare(t1) >= 0)
assert.False(t, t2.Before(t1))

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.

3 participants