Skip to content

Commit

Permalink
feat(entry): remove valid() method (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
janrnc authored Nov 11, 2023
1 parent 13f4603 commit abf1acd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
17 changes: 10 additions & 7 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"sync"
"time"
"fmt"
)

// Cron keeps track of any number of entries, invoking the associated func as
Expand Down Expand Up @@ -39,7 +40,7 @@ type Schedule interface {
}

// ID identifies an entry within a Cron instance
type ID int
type ID uint

// Entry consists of a schedule and the func to execute on that schedule.
type Entry struct {
Expand All @@ -60,9 +61,6 @@ type Entry struct {
job func()
}

// Valid returns true if this is not the zero entry.
func (e Entry) Valid() bool { return e.ID != 0 }

// byTime is a wrapper for sorting the entry array by time
// (with zero time at the end).
type byTime []*Entry
Expand Down Expand Up @@ -128,14 +126,19 @@ func (c *Cron) Add(spec string, cmd func()) (ID, error) {
if err != nil {
return 0, err
}
return c.Schedule(schedule, cmd), nil
return c.Schedule(schedule, cmd)
}

// Schedule adds a job to the Cron to be run on the given schedule.
// The job is wrapped with the configured Chain.
func (c *Cron) Schedule(schedule Schedule, cmd func()) ID {
func (c *Cron) Schedule(schedule Schedule, cmd func()) (ID, error) {
c.runningMu.Lock()
defer c.runningMu.Unlock()

if c.next == 0 {
return 0, fmt.Errorf("run out of available ids")
}

entry := &Entry{
ID: c.next,
Schedule: schedule,
Expand All @@ -147,7 +150,7 @@ func (c *Cron) Schedule(schedule Schedule, cmd func()) ID {
} else {
c.add <- entry
}
return entry.ID
return entry.ID, nil
}

// Entries returns a snapshot of the cron entries.
Expand Down
54 changes: 46 additions & 8 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,18 @@ func TestRunningMultipleSchedules(t *testing.T) {
if err != nil {
t.Error("non-nil error")
}
cron.Schedule(Every(time.Minute), func() {})
cron.Schedule(Every(time.Second), func() { wg.Done() })
cron.Schedule(Every(time.Hour), func() {})
_, err = cron.Schedule(Every(time.Minute), func() {})
if err != nil {
t.Error("non-nil error")
}
_, err = cron.Schedule(Every(time.Second), func() { wg.Done() })
if err != nil {
t.Error("non-nil error")
}
_, err = cron.Schedule(Every(time.Hour), func() {})
if err != nil {
t.Error("non-nil error")
}

cron.Start()
defer cron.Stop()
Expand Down Expand Up @@ -508,8 +517,14 @@ func TestJob(t *testing.T) {
if err != nil {
t.Error("non-nil error")
}
job4 := cron.Schedule(Every(5*time.Second+5*time.Nanosecond), newTestJob(wg, "job4").job())
job5 := cron.Schedule(Every(5*time.Minute), newTestJob(wg, "job5").job())
job4, err := cron.Schedule(Every(5*time.Second+5*time.Nanosecond), newTestJob(wg, "job4").job())
if err != nil {
t.Error("non-nil error")
}
job5, err := cron.Schedule(Every(5*time.Minute), newTestJob(wg, "job5").job())
if err != nil {
t.Error("non-nil error")
}

cron.Start()
defer cron.Stop()
Expand Down Expand Up @@ -551,8 +566,11 @@ func TestScheduleAfterRemoval(t *testing.T) {
var mu sync.Mutex

cron := newWithSeconds()
hourJob := cron.Schedule(Every(time.Hour), func() {})
cron.Schedule(Every(time.Second), func() {
hourJob, err := cron.Schedule(Every(time.Hour), func() {})
if err != nil {
t.Error("non-nil error")
}
_, err = cron.Schedule(Every(time.Second), func() {
mu.Lock()
defer mu.Unlock()
switch calls {
Expand All @@ -570,6 +588,9 @@ func TestScheduleAfterRemoval(t *testing.T) {
panic("unexpected 3rd call")
}
})
if err != nil {
t.Error("non-nil error")
}

cron.Start()
defer cron.Stop()
Expand Down Expand Up @@ -599,7 +620,10 @@ func TestJobWithZeroTimeDoesNotRun(t *testing.T) {
if err != nil {
t.Error("non-nil error")
}
cron.Schedule(new(ZeroSchedule), func() { t.Error("expected zero task will not run") })
_, err = cron.Schedule(new(ZeroSchedule), func() { t.Error("expected zero task will not run") })
if err != nil {
t.Error("non-nil error")
}
cron.Start()
defer cron.Stop()
<-time.After(OneSecond)
Expand Down Expand Up @@ -761,6 +785,20 @@ func TestMultiThreadedStartAndStop(t *testing.T) {
cron.Stop()
}

func TestRunningOutOfIDs(t *testing.T) {
cron := New()
cron.next = ID(^uint(0))

_, err := cron.Add("* * * * *", func() {})
if err != nil {
t.Error("non-nil error")
}
_, err = cron.Add("* * * * *", func() {})
if err == nil {
t.Error("expected error, got nil")
}
}

func wait(wg *sync.WaitGroup) chan bool {
ch := make(chan bool)
go func() {
Expand Down

0 comments on commit abf1acd

Please sign in to comment.