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

test: add a custom mock clock implementation #1154

Merged
merged 7 commits into from
Feb 14, 2024
Merged

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Feb 9, 2024

benbjohnson/clock has been archived and no longer maintained.
Removing that dependency is desirable.

This change adds a mock clock adapted from Zap's mock clock
(uber-go/zap#1349), but with operations specific to Fx's needs.

There are two areas that require more scrutiny
because of divergence from Zap's mock clock:

WithTimeout requires us to implement a custom context.Context
so that it can report context.DeadlineExceeded when it's time.
We cannot just use context.WithCancelCause here because the cause
for a context failure is considered different from ctx.Err,
so ctx.Err would still report context.Canceled for a timeout.

When testing that a sleep behaves as expected, there's a data race
between the Sleep and the Add that progresses time.
To resolve this, this change adds an AwaitScheduled method
that blocks until there are operations scheduled for the future.
This is done by using a sync.Cond.
This gives us a way to wait until the sleep is scheduled
before we progress time.

This change also had to update Zap to pick up the release with the
custom clock to drop the benbjohnson/clock dependency from the go.mod
completely.

Refs uber-go/zap#1349
Resolves #1135

"github.com/stretchr/testify/assert"
)

var _ Clock = clock.Clock(nil)

// Just a basic sanity check that everything is in order.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Instead of just the system clock sanity test, the test is now used for both, system clock and mock clock to verify basic outer expectations of the clocks.

@rabbbit
Copy link

rabbbit commented Feb 10, 2024 via email

@abhinav
Copy link
Collaborator Author

abhinav commented Feb 10, 2024

Looks like you don't intend to put the clock in an externally addressable location? :l

We do! @prashantv has a nicer version of the clock in progress for re-use between projects.

benbjohnson/clock has been archived and no longer maintained.
Removing that dependency is desirable.

This change adds a mock clock adapted from Zap's mock clock
(uber-go/zap#1349), but with operations specific to Fx's needs.

There are two areas that require more scrutiny
because of divergence from Zap's mock clock:

WithTimeout requires us to implement a custom `context.Context`
so that it can report context.DeadlineExceeded when it's time.
We cannot just use `context.WithCancelCause` here because the cause
for a context failure is considered different from `ctx.Err`,
so `ctx.Err` would still report `context.Canceled` for a timeout.

When testing that a sleep behaves as expected, there's a data race
between the `Sleep` and the `Add` that progresses time.
To resolve this, this change adds an `AwaitScheduled` method
that blocks until there are operations scheduled for the future.
This is done by using a `sync.Cond`.
This gives us a way to wait until the sleep is scheduled
before we progress time.

This change also had to update Zap to pick up the release with the
custom clock to drop the benbjohnson/clock dependency from the go.mod
completely.

Refs uber-go/zap#1349
Resolves uber-go#1135
Copy link

codecov bot commented Feb 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (fa4c995) 98.74% compared to head (7de231b) 98.78%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1154      +/-   ##
==========================================
+ Coverage   98.74%   98.78%   +0.04%     
==========================================
  Files          30       30              
  Lines        2950     3057     +107     
==========================================
+ Hits         2913     3020     +107     
  Misses         30       30              
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@abhinav
Copy link
Collaborator Author

abhinav commented Feb 12, 2024

image

unclear what's going on here; i don't have visibility into it

Copy link
Contributor

@JacobOaks JacobOaks left a comment

Choose a reason for hiding this comment

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

LGTM I think, just a couple questions/comments on your tests, and probably should wait for another maintainer to take a look too before merging.

internal/fxclock/clock.go Outdated Show resolved Hide resolved
internal/fxclock/clock_test.go Outdated Show resolved Hide resolved
internal/fxclock/clock_test.go Outdated Show resolved Hide resolved
internal/fxclock/clock_test.go Show resolved Hide resolved
abhinav and others added 4 commits February 13, 2024 13:19
Co-authored-by: Jacob Oaks <jacoboaks.8@gmail.com>
Co-authored-by: Jacob Oaks <jacoboaks.8@gmail.com>
The same check we have elsewhere with AwaitScheduled
is needed for the end-to-end Clock test.
internal/fxclock/clock.go Outdated Show resolved Hide resolved
internal/fxclock/clock.go Show resolved Hide resolved
@sywhang
Copy link
Contributor

sywhang commented Feb 14, 2024

quick first pass, will come back to this later tonight

@abhinav abhinav merged commit 793a644 into uber-go:master Feb 14, 2024
12 checks passed
@abhinav abhinav deleted the abg/clock branch February 14, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

https://github.com/benbjohnson/clock is EOL/archived.
4 participants