Skip to content

Conversation

@cloudfly
Copy link
Contributor

Add a new interface Timer replace time.Timer for scheduling the job.

So that the user can create custom timer instead of using standard time.Timer,mainly used for testing。

eg.

import (
	"github.com/benbjohnson/clock"
        "github.com/robfig/cron/v3"
)

func main() {
    mockClock := clock.NewMock()
    cronEngine = cron.New(cron.WithTimerFunc(NewMockTimerFunc(mockClock)))
}

func NewMockTimerFunc(mock *clock.Mock) func(time.Duration) cron.Timer {
	return func(d time.Duration) cron.Timer {
		return &mockTimer{
			timer: mock.Timer(d),
		}
	}
}

type mockTimer struct {
	timer *clock.Timer
}

func (t *mockTimer) C() <-chan time.Time {
	return t.timer.C
}

func (t *mockTimer) Stop() bool {
	return t.timer.Stop()
}

dsh2dsh added a commit to dsh2dsh/cron that referenced this pull request Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant