@@ -3190,3 +3190,76 @@ func TestScheduler_JobSchedule(t *testing.T) {
31903190 })
31913191 }
31923192}
3193+
3194+ func TestScheduler_WithStopDateTime_JobRemovedAfterStopTime (t * testing.T ) {
3195+ defer verifyNoGoroutineLeaks (t )
3196+
3197+ t .Run ("job is removed from scheduler after stop time elapses" , func (t * testing.T ) {
3198+ monitor := newTestSchedulerMonitor ()
3199+ s := newTestScheduler (t , WithSchedulerMonitor (monitor ))
3200+
3201+ _ , err := s .NewJob (
3202+ DurationJob (50 * time .Millisecond ),
3203+ NewTask (func () {}),
3204+ WithStopAt (WithStopDateTime (time .Now ().Add (200 * time .Millisecond ))),
3205+ WithStartAt (WithStartImmediately ()),
3206+ )
3207+ require .NoError (t , err )
3208+
3209+ s .Start ()
3210+
3211+ require .Eventually (t , func () bool {
3212+ return len (s .Jobs ()) == 0
3213+ }, time .Second , 10 * time .Millisecond , "job should be removed after stop time" )
3214+
3215+ assert .GreaterOrEqual (t , monitor .getJobUnregCount (), int64 (1 ), "monitor should receive JobUnregistered notification" )
3216+
3217+ require .NoError (t , s .Shutdown ())
3218+ })
3219+
3220+ t .Run ("job added before start is removed on start when stop time already elapsed" , func (t * testing.T ) {
3221+ monitor := newTestSchedulerMonitor ()
3222+ s := newTestScheduler (t , WithSchedulerMonitor (monitor ))
3223+
3224+ _ , err := s .NewJob (
3225+ DurationJob (time .Hour ),
3226+ NewTask (func () {}),
3227+ WithStopAt (WithStopDateTime (time .Now ().Add (100 * time .Millisecond ))),
3228+ )
3229+ require .NoError (t , err )
3230+
3231+ // wait until stop time has passed, then start the scheduler
3232+ time .Sleep (150 * time .Millisecond )
3233+ s .Start ()
3234+
3235+ require .Eventually (t , func () bool {
3236+ return len (s .Jobs ()) == 0
3237+ }, time .Second , 10 * time .Millisecond , "job should be removed when scheduler starts after stop time" )
3238+
3239+ require .NoError (t , s .Shutdown ())
3240+ })
3241+
3242+ t .Run ("RemoveJob on already auto-removed job returns ErrJobNotFound" , func (t * testing.T ) {
3243+ s := newTestScheduler (t )
3244+
3245+ j , err := s .NewJob (
3246+ DurationJob (50 * time .Millisecond ),
3247+ NewTask (func () {}),
3248+ WithStopAt (WithStopDateTime (time .Now ().Add (150 * time .Millisecond ))),
3249+ WithStartAt (WithStartImmediately ()),
3250+ )
3251+ require .NoError (t , err )
3252+
3253+ s .Start ()
3254+
3255+ require .Eventually (t , func () bool {
3256+ return len (s .Jobs ()) == 0
3257+ }, time .Second , 10 * time .Millisecond , "job should be auto-removed after stop time" )
3258+
3259+ // Explicitly removing an already auto-removed job should return ErrJobNotFound
3260+ err = s .RemoveJob (j .ID ())
3261+ assert .ErrorIs (t , err , ErrJobNotFound )
3262+
3263+ require .NoError (t , s .Shutdown ())
3264+ })
3265+ }
0 commit comments