Skip to content

chore: add clock pkg for testing time #13461

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

Merged
merged 1 commit into from
Jun 5, 2024
Merged

Conversation

spikecurtis
Copy link
Contributor

@spikecurtis spikecurtis commented Jun 4, 2024

Adds a package for testing time/timer/ticker functions. Implementation is limited to NewTimer and NewContextTicker, but will eventually be expanded to all time functions from the standard library as well as context.WithTimeout(), context.WithDeadline().

Replaces benbjohnson/clock for the pubsub watchdog, as a proof of concept.

Eventually, as we expand functionality, we will replace most time-related functions with this library for testing.

Copy link
Contributor Author

spikecurtis commented Jun 4, 2024

@spikecurtis spikecurtis marked this pull request as ready for review June 4, 2024 07:38
@spikecurtis spikecurtis force-pushed the spike/clock-testing-watchdog branch 2 times, most recently from e6c30e4 to 8caaed8 Compare June 4, 2024 08:08
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Nice work! I'm happy to see how the modified test cases in pubsub nicely demonstrates the utility of the trap concept. I didn't re-read the RFC but this implementation largely matches my recollection of the approach.

Considering this is likely going to be its own library in the future, I'd love to see some func Example...s that demonstrate the functionality and use-cases. Some in-package tests would be nice too.

clock/clock.go Outdated
// NewContextTicker is a convenience function that calls f on the interval d until either the
// given context expires or f returns an error. Callers may call Wait() on the returned Waiter
// to wait until this happens and obtain the errror.
NewContextTicker(ctx context.Context, d time.Duration, f func() error, tags ...string) Waiter
Copy link
Member

Choose a reason for hiding this comment

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

How would you feel about using functional options instead of ...string? An alternative to having this convenience function would be to accept the following signature:

NewTicker(d time.Duration, f func() error, opts ...Option) and you'd pass it clock.WithContext(ctx) or `clock.Tags("tag1", "tag2") as options.

I think this could result in a slightly cleaner and more flexible API, as well as the added verbosity/explicitness can help with code reads (i.e. "what are these strings?").

In this scenario, I imagine the return signature of NewTicker could include all three methods Stop, Reset and Wait.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Firstly, maybe "ContextTicker" isn't the best name. It's not a ticker that optionally takes a context, it's a ticker calls the function f on the ticks, rather than just sending the ticks to a channel. Sort of like AfterFunc is a special timer that calls a function. "TickerFunc"?

Secondly, the idea is to include tags on every call in the clock interface, and all the timer/ticker calls as well. With the exception of this ContextTicker/TickerFunc, the idea is to verbatim replicate the functionality of the time package, not enhance it. So, I literally cannot think of another option we'd want other than tags, and even if I could, it's unlikely to apply to every call, so adding it to a generic Option interface would muddy the waters. I think variability is actually a downside in this case, and we should keep it simple.

Lastly, I'm not a fan of the verbosity of clock.Tags("tag1"). The signature says tags ...string, so I feel like that's clear enough once you jump to the definition.

@johnstcn
Copy link
Member

johnstcn commented Jun 4, 2024

I really like how this is looking so far! 👍 Just a few suggestions on my end.

@spikecurtis spikecurtis force-pushed the spike/clock-testing-watchdog branch 2 times, most recently from 5da53ab to e243711 Compare June 4, 2024 12:37
@spikecurtis spikecurtis force-pushed the spike/clock-testing-watchdog branch from e243711 to 63c9374 Compare June 5, 2024 06:14
@spikecurtis spikecurtis requested review from johnstcn and mafredri June 5, 2024 06:21
@spikecurtis spikecurtis merged commit 42324b3 into main Jun 5, 2024
27 checks passed
@spikecurtis spikecurtis deleted the spike/clock-testing-watchdog branch June 5, 2024 09:55
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants