Skip to content

Commit 9cc3be7

Browse files
authored
fix: calling start multiple times should no-op (#901)
* fix: calling start multiple times should no-op * give test more time after start * add a mutex
1 parent 974802a commit 9cc3be7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

scheduler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,11 @@ func (s *scheduler) RemoveJob(id uuid.UUID) error {
824824
}
825825

826826
func (s *scheduler) Start() {
827+
if s.started.Load() {
828+
s.logger.Warn("gocron: scheduler already started")
829+
return
830+
}
831+
827832
select {
828833
case <-s.shutdownCtx.Done():
829834
case s.startCh <- struct{}{}:

scheduler_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,41 @@ func TestScheduler_Shutdown(t *testing.T) {
578578
})
579579
}
580580

581+
func TestScheduler_Start(t *testing.T) {
582+
defer verifyNoGoroutineLeaks(t)
583+
584+
t.Run("calling start multiple times is a no-op", func(t *testing.T) {
585+
s := newTestScheduler(t)
586+
587+
var counter int
588+
var mu sync.Mutex
589+
590+
_, err := s.NewJob(
591+
DurationJob(
592+
100*time.Millisecond,
593+
),
594+
NewTask(
595+
func() {
596+
mu.Lock()
597+
counter++
598+
mu.Unlock()
599+
},
600+
),
601+
)
602+
require.NoError(t, err)
603+
604+
s.Start()
605+
s.Start()
606+
s.Start()
607+
608+
time.Sleep(1000 * time.Millisecond)
609+
610+
require.NoError(t, s.Shutdown())
611+
612+
assert.Contains(t, []int{9, 10}, counter)
613+
})
614+
}
615+
581616
func TestScheduler_NewJob(t *testing.T) {
582617
defer verifyNoGoroutineLeaks(t)
583618
tests := []struct {

0 commit comments

Comments
 (0)