-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
362 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package cron | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-co-op/gocron/v2" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/duplicatechecker" | ||
"github.com/mayswind/ezbookkeeping/pkg/settings" | ||
) | ||
|
||
func TestCronJobSchedulerContainerRegisterIntervalJob(t *testing.T) { | ||
var err error | ||
|
||
container := &CronJobSchedulerContainer{ | ||
allJobsMap: make(map[string]*CronJob), | ||
allGocronJobsMap: make(map[string]gocron.Job), | ||
} | ||
|
||
container.scheduler, err = gocron.NewScheduler( | ||
gocron.WithLocation(time.Local), | ||
gocron.WithLogger(NewGocronLoggerAdapter()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
actualValue := false | ||
job := &CronJob{ | ||
Name: "TestRegisterIntervalJob", | ||
Description: "The test cron job", | ||
Period: CronJobIntervalPeriod{ | ||
Interval: 1 * time.Second, | ||
}, | ||
Run: func() error { | ||
actualValue = true | ||
return nil | ||
}, | ||
} | ||
|
||
container.registerIntervalJob(job) | ||
container.scheduler.Start() | ||
|
||
assert.Equal(t, 1, len(container.GetAllJobs())) | ||
assert.Equal(t, job, container.GetAllJobs()[0]) | ||
|
||
time.Sleep(2 * time.Second) | ||
assert.True(t, actualValue) | ||
|
||
err = container.scheduler.Shutdown() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestCronJobSchedulerContainerSyncRunJobNow(t *testing.T) { | ||
var err error | ||
|
||
container := &CronJobSchedulerContainer{ | ||
allJobsMap: make(map[string]*CronJob), | ||
allGocronJobsMap: make(map[string]gocron.Job), | ||
} | ||
|
||
container.scheduler, err = gocron.NewScheduler( | ||
gocron.WithLocation(time.Local), | ||
gocron.WithLogger(NewGocronLoggerAdapter()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
actualValue := false | ||
job := &CronJob{ | ||
Name: "TestSyncRunJob", | ||
Description: "The test cron job", | ||
Period: CronJobIntervalPeriod{ | ||
Interval: 24 * time.Hour, | ||
}, | ||
Run: func() error { | ||
actualValue = true | ||
return nil | ||
}, | ||
} | ||
|
||
container.registerIntervalJob(job) | ||
|
||
err = container.SyncRunJobNow("TestSyncRunJob") | ||
assert.Nil(t, err) | ||
assert.True(t, actualValue) | ||
} | ||
|
||
func TestCronJobSchedulerContainerRepeatRun(t *testing.T) { | ||
var err error | ||
|
||
checker, _ := duplicatechecker.NewInMemoryDuplicateChecker(&settings.Config{ | ||
DuplicateSubmissionsIntervalDuration: 60 * time.Second, | ||
InMemoryDuplicateCheckerCleanupIntervalDuration: 60 * time.Second, | ||
}) | ||
|
||
duplicatechecker.Container.Current = checker | ||
|
||
container := &CronJobSchedulerContainer{ | ||
allJobsMap: make(map[string]*CronJob), | ||
allGocronJobsMap: make(map[string]gocron.Job), | ||
} | ||
|
||
container.scheduler, err = gocron.NewScheduler( | ||
gocron.WithLocation(time.Local), | ||
gocron.WithLogger(NewGocronLoggerAdapter()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
var runCount atomic.Uint32 | ||
runTime := time.Now().Add(time.Second) | ||
job := &CronJob{ | ||
Name: "TestRepeatRunJob", | ||
Description: "The test cron job", | ||
Period: CronJobFixedTimePeriod{ | ||
Time: runTime, | ||
}, | ||
Run: func() error { | ||
runCount.Add(1) | ||
return nil | ||
}, | ||
} | ||
|
||
container.registerIntervalJob(job) | ||
container.registerIntervalJob(job) | ||
container.registerIntervalJob(job) | ||
container.registerIntervalJob(job) | ||
container.registerIntervalJob(job) | ||
container.scheduler.Start() | ||
|
||
time.Sleep(10 * time.Second) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, uint32(1), runCount.Load()) | ||
|
||
err = container.scheduler.Shutdown() | ||
assert.Nil(t, err) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cron | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-co-op/gocron/v2" | ||
) | ||
|
||
// CronJobPeriod represents the cron job period | ||
type CronJobPeriod interface { | ||
GetInterval() time.Duration | ||
ToJobDefinition() gocron.JobDefinition | ||
} | ||
|
||
// CronJobIntervalPeriod represents the period of execution at intervals | ||
type CronJobIntervalPeriod struct { | ||
Interval time.Duration | ||
} | ||
|
||
// CronJobFixedHourPeriod represents the period of execution at fixed hour | ||
type CronJobFixedHourPeriod struct { | ||
Hour uint32 | ||
} | ||
|
||
// CronJobFixedTimePeriod represents the period of execution at fixed time | ||
type CronJobFixedTimePeriod struct { | ||
Time time.Time | ||
} | ||
|
||
// GetInterval returns the interval time of the period of CronJobIntervalPeriod | ||
func (p CronJobIntervalPeriod) GetInterval() time.Duration { | ||
return p.Interval | ||
} | ||
|
||
// ToJobDefinition returns the gocron job definition of the period of CronJobIntervalPeriod | ||
func (p CronJobIntervalPeriod) ToJobDefinition() gocron.JobDefinition { | ||
return gocron.DurationJob(p.Interval) | ||
} | ||
|
||
// GetInterval returns the interval time of the period of CronJobFixedHourPeriod | ||
func (p CronJobFixedHourPeriod) GetInterval() time.Duration { | ||
return 24 * time.Hour | ||
} | ||
|
||
// ToJobDefinition returns the gocron job definition of the period of CronJobFixedHourPeriod | ||
func (p CronJobFixedHourPeriod) ToJobDefinition() gocron.JobDefinition { | ||
return gocron.DailyJob( | ||
1, | ||
gocron.NewAtTimes( | ||
gocron.NewAtTime(uint(p.Hour), 0, 0), | ||
), | ||
) | ||
} | ||
|
||
// GetInterval returns the interval time of the period of CronJobFixedTimePeriod | ||
func (p CronJobFixedTimePeriod) GetInterval() time.Duration { | ||
return 0 | ||
} | ||
|
||
// ToJobDefinition returns the gocron job definition of the period of CronJobFixedTimePeriod | ||
func (p CronJobFixedTimePeriod) ToJobDefinition() gocron.JobDefinition { | ||
return gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(p.Time)) | ||
} |
Oops, something went wrong.