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

Replace benbjohnson/clock with custom MockClock #1349

Merged
merged 6 commits into from
Sep 9, 2023

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Sep 8, 2023

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:

  • waiter: a function 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 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

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Merging #1349 (74f27a8) into master (2b35963) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            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              
Files Changed Coverage Δ
internal/ztest/clock.go 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

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 abhinav changed the title Drop frozen dependency benbjohnson/clock Replace benbjohnson/clock with custom MockClock Sep 8, 2023
internal/ztest/clock.go Show resolved Hide resolved
internal/ztest/clock.go Outdated Show resolved Hide resolved
internal/ztest/clock.go Show resolved Hide resolved
internal/ztest/clock.go Outdated Show resolved Hide resolved
internal/ztest/clock.go Outdated Show resolved Hide resolved
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.
internal/ztest/clock.go Show resolved Hide resolved
@abhinav abhinav merged commit 82c728b into uber-go:master Sep 9, 2023
5 checks passed
@abhinav abhinav deleted the clock branch September 9, 2023 15:55
abhinav added a commit to abhinav/fx that referenced this pull request Feb 7, 2024
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
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Use of github.com/benbjohnson/clock which is no longer maintained
2 participants