mirror of https://github.com/go-co-op/gocron.git
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
This commit is contained in:
parent
974802ab3b
commit
9cc3be7cff
|
|
@ -824,6 +824,11 @@ func (s *scheduler) RemoveJob(id uuid.UUID) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *scheduler) Start() {
|
func (s *scheduler) Start() {
|
||||||
|
if s.started.Load() {
|
||||||
|
s.logger.Warn("gocron: scheduler already started")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-s.shutdownCtx.Done():
|
case <-s.shutdownCtx.Done():
|
||||||
case s.startCh <- struct{}{}:
|
case s.startCh <- struct{}{}:
|
||||||
|
|
|
||||||
|
|
@ -578,6 +578,41 @@ func TestScheduler_Shutdown(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestScheduler_Start(t *testing.T) {
|
||||||
|
defer verifyNoGoroutineLeaks(t)
|
||||||
|
|
||||||
|
t.Run("calling start multiple times is a no-op", func(t *testing.T) {
|
||||||
|
s := newTestScheduler(t)
|
||||||
|
|
||||||
|
var counter int
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
_, err := s.NewJob(
|
||||||
|
DurationJob(
|
||||||
|
100*time.Millisecond,
|
||||||
|
),
|
||||||
|
NewTask(
|
||||||
|
func() {
|
||||||
|
mu.Lock()
|
||||||
|
counter++
|
||||||
|
mu.Unlock()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
s.Start()
|
||||||
|
s.Start()
|
||||||
|
s.Start()
|
||||||
|
|
||||||
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
|
||||||
|
require.NoError(t, s.Shutdown())
|
||||||
|
|
||||||
|
assert.Contains(t, []int{9, 10}, counter)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestScheduler_NewJob(t *testing.T) {
|
func TestScheduler_NewJob(t *testing.T) {
|
||||||
defer verifyNoGoroutineLeaks(t)
|
defer verifyNoGoroutineLeaks(t)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue