Skip to content

Commit

Permalink
reconciler: provide a job factory func
Browse files Browse the repository at this point in the history
This allows us to use cron expressions with seconds in the tests, thus
allowing the test run to finish a lot quicker.

Signed-off-by: Miguel Duarte Barroso <[email protected]>
  • Loading branch information
maiqueb committed Dec 22, 2023
1 parent 58ba24c commit 40ea8de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
24 changes: 22 additions & 2 deletions pkg/reconciler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,36 @@ type ConfigWatcher struct {
job gocron.Job
scheduler gocron.Scheduler
handlerFunc func()
jobFactoryFunc func(string) gocron.JobDefinition
watcher *fsnotify.Watcher
}

func NewConfigWatcher(configPath string, scheduler gocron.Scheduler, configWatcher *fsnotify.Watcher, handlerFunc func()) (*ConfigWatcher, error) {
return newConfigWatcher(
configPath,
scheduler,
configWatcher,
func(schedule string) gocron.JobDefinition {
return gocron.CronJob(schedule, false)
},
handlerFunc,
)
}

func newConfigWatcher(
configPath string,
scheduler gocron.Scheduler,
configWatcher *fsnotify.Watcher,
cronJobFactoryFunc func(string) gocron.JobDefinition,
handlerFunc func(),
) (*ConfigWatcher, error) {
schedule, err := determineCronExpression(configPath)
if err != nil {
return nil, err
}

job, err := scheduler.NewJob(
gocron.CronJob(schedule, false),
cronJobFactoryFunc(schedule),
gocron.NewTask(handlerFunc),
)
if err != nil {
Expand All @@ -46,6 +65,7 @@ func NewConfigWatcher(configPath string, scheduler gocron.Scheduler, configWatch
scheduler: scheduler,
watcher: configWatcher,
handlerFunc: handlerFunc,
jobFactoryFunc: cronJobFactoryFunc,
}, nil
}

Expand Down Expand Up @@ -100,7 +120,7 @@ func (c *ConfigWatcher) syncConfig(relevantEventPredicate func(event fsnotify.Ev
}
updatedJob, err := c.scheduler.Update(
c.job.ID(),
gocron.CronJob(updatedSchedule, false),
c.jobFactoryFunc(updatedSchedule),
gocron.NewTask(c.handlerFunc),
)
if err != nil {
Expand Down
26 changes: 19 additions & 7 deletions pkg/reconciler/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ var _ = Describe("Reconciler configuration watcher", func() {
configDir, err = os.MkdirTemp("", "config")
Expect(err).NotTo(HaveOccurred())
const (
initialCron = "0/1 2 3 * *"
dummyFileName = "DUMMY"
initialCronWithSeconds = "0/1 2 3 * * *"
dummyFileName = "DUMMY"
)
dummyConfig, err = os.Create(filepath.Join(configDir, filepath.Base(dummyFileName)))
Expect(err).NotTo(HaveOccurred())

Expect(dummyConfig.Write([]byte(initialCron))).To(Equal(len(initialCron)))
Expect(dummyConfig.Write([]byte(initialCronWithSeconds))).To(Equal(len(initialCronWithSeconds)))
scheduler, err := gocron.NewScheduler()
Expect(err).NotTo(HaveOccurred())
watcher, err = fsnotify.NewWatcher()
Expect(err).NotTo(HaveOccurred())
config, err = NewConfigWatcher(
config, err = newConfigWatcherForTests(
dummyConfig.Name(),
scheduler,
watcher,
Expand All @@ -59,15 +59,27 @@ var _ = Describe("Reconciler configuration watcher", func() {
})

When("the cron job expression is updated in the file-system", func() {
const updatedCron = "0/1 * * * *"
const updatedCronWithSeconds = "0/1 * * * * *"

BeforeEach(func() {
Expect(dummyConfig.WriteAt([]byte(updatedCron), 0)).To(Equal(len(updatedCron)))
Expect(dummyConfig.WriteAt([]byte(updatedCronWithSeconds), 0)).To(Equal(len(updatedCronWithSeconds)))
})

It("the current schedule is updated, and the handler function executed", func() {
Eventually(func() string { return config.currentSchedule }).Should(Equal(updatedCron))
Eventually(func() string { return config.currentSchedule }).Should(Equal(updatedCronWithSeconds))
Eventually(mailbox).WithTimeout(time.Minute).Should(Receive())
})
})
})

func newConfigWatcherForTests(configPath string, scheduler gocron.Scheduler, configWatcher *fsnotify.Watcher, handlerFunc func()) (*ConfigWatcher, error) {
return newConfigWatcher(
configPath,
scheduler,
configWatcher,
func(schedule string) gocron.JobDefinition {
return gocron.CronJob(schedule, true)
},
handlerFunc,
)
}

0 comments on commit 40ea8de

Please sign in to comment.