@@ -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+
581616func TestScheduler_NewJob (t * testing.T ) {
582617 defer verifyNoGoroutineLeaks (t )
583618 tests := []struct {
@@ -2876,3 +2911,34 @@ func TestScheduler_WithMonitor(t *testing.T) {
28762911 })
28772912 }
28782913}
2914+
2915+ func TestScheduler_WithStartAtDateTimePast (t * testing.T ) {
2916+ defer verifyNoGoroutineLeaks (t )
2917+
2918+ // Monday
2919+ testTime := time .Date (2024 , time .January , 1 , 9 , 0 , 0 , 0 , time .UTC )
2920+
2921+ fakeClock := clockwork .NewFakeClockAt (testTime )
2922+
2923+ s := newTestScheduler (t , WithClock (fakeClock ))
2924+ j , err := s .NewJob (
2925+ WeeklyJob (2 , NewWeekdays (time .Sunday ), NewAtTimes (NewAtTime (10 , 0 , 0 ))),
2926+ NewTask (func () {}),
2927+ WithStartAt (
2928+ // The start time is in the past (Dec 30, 2023 9am) which is a Saturday
2929+ WithStartDateTimePast (testTime .Add (- time .Hour * 24 * 2 )),
2930+ ),
2931+ )
2932+ require .NoError (t , err )
2933+
2934+ s .Start ()
2935+
2936+ nextRun , err := j .NextRun ()
2937+ require .NoError (t , err )
2938+
2939+ require .NoError (t , s .Shutdown ())
2940+
2941+ // Because the start time was in the past - we expect it to schedule 2 intervals ahead, pasing the first available Sunday
2942+ // which was in the past Dec 31, 2023, so the next is Jan 7, 2024
2943+ assert .Equal (t , time .Date (2024 , time .January , 7 , 10 , 0 , 0 , 0 , time .UTC ), nextRun )
2944+ }
0 commit comments