Skip to content

Commit

Permalink
fix random interval where lastRun ends up in the past (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler authored Apr 13, 2023
1 parent 6cd5439 commit a86ec0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
13 changes: 6 additions & 7 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *Scheduler) scheduleNextRun(job *Job) (bool, nextRun) {
}
}
} else {
lastRun = job.LastRun()
lastRun = job.NextRun()
}

if !job.shouldRun() {
Expand Down Expand Up @@ -602,18 +602,17 @@ func (s *Scheduler) runContinuous(job *Job) {
} else {
s.run(job)
}

nextRun := next.dateTime.Sub(s.now())
if nextRun < 0 {
time.Sleep(absDuration(nextRun))
nr := next.dateTime.Sub(s.now())
if nr < 0 {
time.Sleep(absDuration(nr))
shouldRun, next := s.scheduleNextRun(job)
if !shouldRun {
return
}
nextRun = next.dateTime.Sub(s.now())
nr = next.dateTime.Sub(s.now())
}

job.setTimer(s.timer(nextRun, func() {
job.setTimer(s.timer(nr, func() {
if !next.dateTime.IsZero() {
for {
n := s.now().UnixNano() - next.dateTime.UnixNano()
Expand Down
28 changes: 16 additions & 12 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestScheduler_EveryRandom(t *testing.T) {
s := NewScheduler(time.UTC)
semaphore := make(chan bool)

j, err := s.EveryRandom(1, 2).Seconds().Do(func() {
j, err := s.EveryRandom(50, 100).Milliseconds().Do(func() {
semaphore <- true
})
require.NoError(t, err)
Expand All @@ -93,14 +93,14 @@ func TestScheduler_EveryRandom(t *testing.T) {
var counter int

now := time.Now()
for time.Now().Before(now.Add(2 * time.Second)) {
for time.Now().Before(now.Add(1 * time.Second)) {
if <-semaphore {
counter++
}
}
s.Stop()
assert.LessOrEqual(t, counter, 3)
assert.GreaterOrEqual(t, counter, 1)
assert.LessOrEqual(t, counter, 20)
assert.GreaterOrEqual(t, counter, 10)
}

func TestScheduler_Every(t *testing.T) {
Expand Down Expand Up @@ -1773,8 +1773,8 @@ func TestScheduler_Update(t *testing.T) {

counterMutex.RLock()
defer counterMutex.RUnlock()
assert.GreaterOrEqual(t, counter, 5)
assert.LessOrEqual(t, counter, 6)
assert.GreaterOrEqual(t, counter, 2)
assert.LessOrEqual(t, counter, 3)
})

t.Run("happy singleton mode", func(t *testing.T) {
Expand Down Expand Up @@ -1805,8 +1805,8 @@ func TestScheduler_Update(t *testing.T) {

counterMutex.RLock()
defer counterMutex.RUnlock()
assert.GreaterOrEqual(t, counter, 4)
assert.LessOrEqual(t, counter, 5)
assert.GreaterOrEqual(t, counter, 2)
assert.LessOrEqual(t, counter, 3)
})

t.Run("update called without job call", func(t *testing.T) {
Expand All @@ -1824,12 +1824,16 @@ func TestScheduler_Update(t *testing.T) {
var counterMutex sync.RWMutex
counter := 0

j, err := s.Every(1).Day().Do(func() { counterMutex.Lock(); defer counterMutex.Unlock(); counter++ })
j, err := s.Every(1).Day().Do(func() {
counterMutex.Lock()
defer counterMutex.Unlock()
counter++
})
require.NoError(t, err)

s.StartAsync()

time.Sleep(300 * time.Millisecond)

_, err = s.Job(j).Every("500ms").Update()
require.NoError(t, err)

Expand All @@ -1848,7 +1852,8 @@ func TestScheduler_Update(t *testing.T) {

counterMutex.RLock()
defer counterMutex.RUnlock()
assert.Equal(t, 4, counter)
assert.GreaterOrEqual(t, counter, 3)
assert.LessOrEqual(t, counter, 4)
})

// Verifies https://github.com/go-co-op/gocron/issues/424
Expand All @@ -1869,7 +1874,6 @@ func TestScheduler_Update(t *testing.T) {
expectedNext := last.Add(newInterval)

assert.Equal(t, expectedNext, actualNext)

})
}

Expand Down

0 comments on commit a86ec0c

Please sign in to comment.