Add support for using mock clock in cron#473
Open
sarsharma wants to merge 4 commits intorobfig:masterfrom
Open
Add support for using mock clock in cron#473sarsharma wants to merge 4 commits intorobfig:masterfrom
sarsharma wants to merge 4 commits intorobfig:masterfrom
Conversation
Signed-off-by: Sarthak Sharma <sartsharma@microsoft.com>
Signed-off-by: Sarthak Sharma <sartsharma@microsoft.com>
Signed-off-by: Sarthak Sharma <sartsharma@microsoft.com>
Signed-off-by: Sarthak Sharma <sartsharma@microsoft.com>
|
Solution available in maintained fork This feature has been implemented in netresearch/go-cron, a maintained fork of this library. Implementation: Full mock clock support with // Create a fake clock initialized to a specific time
fakeClock := cron.NewFakeClock(time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC))
c := cron.New(cron.WithClock(fakeClock))
// Add jobs as usual
c.AddFunc("@every 1h", func() {
fmt.Println("Triggered!")
})
c.Start()
// Wait for scheduler to register timer
fakeClock.BlockUntil(1)
// Advance time deterministically - no real waiting!
fakeClock.Advance(time.Hour) // Triggers the job immediately
// Check timer count for assertions
count := fakeClock.TimerCount()Features:
How to migrate: // Change import from:
import "github.com/robfig/cron/v3"
// To:
import cron "github.com/netresearch/go-cron"The API is 100% backward compatible. See the migration guide for details. Related: netresearch#4, netresearch#34 |
This file contains hidden or 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
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 PR uses https://github.com/benbjohnson/clock to mock the time.
Adding support for using mock clock with cron will be helpful to write test cases and verify cron behaviour.
Similar to this PR #327 but uses a more up to date mock clock
PR also contains fix for flaky unit tests on windows and minor linting fixes