-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Replace benbjohnson/clock with custom MockClock #1349
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report
@@ Coverage Diff @@
## master #1349 +/- ##
==========================================
+ Coverage 98.38% 98.40% +0.02%
==========================================
Files 52 52
Lines 3403 3456 +53
==========================================
+ Hits 3348 3401 +53
Misses 46 46
Partials 9 9
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
abhinav
force-pushed
the
clock
branch
2 times, most recently
from
September 8, 2023 15:32
43b64d5
to
da84dd7
Compare
This drops the dependency on benbjohnson/clock in favor of a simple hand-rolled mock clock implementation. The core of the functionality of MockClock is provided by the following: - waiters: a min-heap of functions waiting to be executed - runAt: schedules a function to be executed when time progresses - Add: moves time forward, running all functions in range The rest of the time functionality can be built upon these pieces. Note that nothing happens until Add is called. There are no goroutines running additional work in the background. Resolves uber-go#1331
abhinav
changed the title
Drop frozen dependency benbjohnson/clock
Replace benbjohnson/clock with custom MockClock
Sep 8, 2023
prashantv
reviewed
Sep 8, 2023
Instead of a `PopLTE` twice, just use a `for { ... }` and break inside.
Avoids memory leaks because the waiter holds a function reference.
Use a slice of waiters, sort it each `Add` call. Simpler, less efficient implementation.
prashantv
approved these changes
Sep 8, 2023
abhinav
added a commit
to abhinav/fx
that referenced
this pull request
Feb 7, 2024
Refs uber-go/zap#1349 Resolves uber-go#1135
abhinav
added a commit
to abhinav/fx
that referenced
this pull request
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 uber-go#1135
abhinav
added a commit
to abhinav/fx
that referenced
this pull request
Feb 12, 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 uber-go#1135
abhinav
added a commit
to uber-go/fx
that referenced
this pull request
Feb 14, 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 --------- Co-authored-by: Jacob Oaks <jacoboaks.8@gmail.com> Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This drops the dependency on benbjohnson/clock as it's no longer maintained.
It replaces it with a hand-rolled mock clock implementation.
The core of the functionality of MockClock is provided by the following:
The rest of the necessary functionality is built on top of these.
This is a pretty simple implementation.
We schedule work, but nothing happens until Add is called.
There are no goroutines running additional work in the background.
Resolves #1331