Replies: 4 comments 13 replies
-
Take a look at https://pkg.go.dev/github.com/go-co-op/gocron#example-Scheduler.CustomTime It was added for this purpose to allow testing with your own logic for time. |
Beta Was this translation helpful? Give feedback.
-
I spend some time today digging more, and found a new package for mocking time ("go.chromium.org/luci/common/clock/testclock") With this new gist, I can have a fake clock that runs faster than wall clock, and it works to some extent. See the test result here: Basically I schedule a job running every 2sec, and inside the box was the mock clock (which runs 60x faster): each trigger reports 2 min inside, the test stops after 10 triggering. However, ideally I want a slight different idea: I want the scheduler to schedule the events inside the box (following the mock clock). Do you think your scheduler package can somehow achieve this? Thanks |
Beta Was this translation helpful? Give feedback.
-
This example is not working anymore. Only one line is printed, which means the job is only executed once.
At the time of writing, testing the scheduler for periodical jobs is quite impracticable, as there's no working example anywhere. |
Beta Was this translation helpful? Give feedback.
-
Mocking of time isn’t great in v1 of gocron. I’m planning to have a better design in v2 that supports actually mocking and manipulating time. I’m planning to use https://github.com/jonboulle/clockwork, but open to suggestions if there is something better. |
Beta Was this translation helpful? Give feedback.
-
Hey, maintainer:
I'm trying to write a scheduler with gocron, and it seems to be serving my needs just fine in production. However, I want to do some testing to ensure my own scheduling logic makes sense and ideally I would like to try mock time: actually the ability to mock time is one of the primary reason I picked gocron in the first place.
I tried to use
github.com/benbjohnson/clock
to mock time, with this gist. Essentially I want to test with the mock time, a scheduler with a periodic job of 2sec, can run (at least) 10 times during a mock period of 30sec.Ideally I would like the time progression to be invisible by the application code, so that we can play god in the time manipulation.
However, even the underlying
clock
library seems to do its job, the testing code (the gist) doesn't work as expected. It would be great if you can:gocron.TimeWrapper
: currently there is no working example.Thanks.
Kevin.
Beta Was this translation helpful? Give feedback.
All reactions