Skip to content

feat: add custom time source injection for testing #4

@CybotTM

Description

@CybotTM

Summary

Allow users to inject a custom time.Now() function for testing and special use cases.

Background

Reference: robfig/cron PR robfig#549

Currently, the scheduler always uses time.Now(). This makes testing time-dependent behavior difficult without actually waiting.

Proposed Solution

Add a functional option:

func WithClock(clock func() time.Time) Option {
    return func(c *Cron) {
        c.clock = clock
    }
}

Considerations

  • WithLocation() covers most timezone use cases
  • Adds API complexity
  • Low demand from users
  • Reconsider if user demand increases

Use Cases

  • Unit testing scheduled job execution
  • Simulating time jumps for DST testing
  • Replaying historical schedules

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions