MCPcopy
hub / github.com/robfig/cron / TestStopAndWait

Function TestStopAndWait

cron_test.go:558–672  ·  cron_test.go::TestStopAndWait
(t *testing.T)

Source from the content-addressed store, hash-verified

556}
557
558func TestStopAndWait(t *testing.T) {
559 t.Run("nothing running, returns immediately", func(t *testing.T) {
560 cron := newWithSeconds()
561 cron.Start()
562 ctx := cron.Stop()
563 select {
564 case <-ctx.Done():
565 case <-time.After(time.Millisecond):
566 t.Error("context was not done immediately")
567 }
568 })
569
570 t.Run("repeated calls to Stop", func(t *testing.T) {
571 cron := newWithSeconds()
572 cron.Start()
573 _ = cron.Stop()
574 time.Sleep(time.Millisecond)
575 ctx := cron.Stop()
576 select {
577 case <-ctx.Done():
578 case <-time.After(time.Millisecond):
579 t.Error("context was not done immediately")
580 }
581 })
582
583 t.Run("a couple fast jobs added, still returns immediately", func(t *testing.T) {
584 cron := newWithSeconds()
585 cron.AddFunc("* * * * * *", func() {})
586 cron.Start()
587 cron.AddFunc("* * * * * *", func() {})
588 cron.AddFunc("* * * * * *", func() {})
589 cron.AddFunc("* * * * * *", func() {})
590 time.Sleep(time.Second)
591 ctx := cron.Stop()
592 select {
593 case <-ctx.Done():
594 case <-time.After(time.Millisecond):
595 t.Error("context was not done immediately")
596 }
597 })
598
599 t.Run("a couple fast jobs and a slow job added, waits for slow job", func(t *testing.T) {
600 cron := newWithSeconds()
601 cron.AddFunc("* * * * * *", func() {})
602 cron.Start()
603 cron.AddFunc("* * * * * *", func() { time.Sleep(2 * time.Second) })
604 cron.AddFunc("* * * * * *", func() {})
605 time.Sleep(time.Second)
606
607 ctx := cron.Stop()
608
609 // Verify that it is not done for at least 750ms
610 select {
611 case <-ctx.Done():
612 t.Error("context was done too quickly immediately")
613 case <-time.After(750 * time.Millisecond):
614 // expected, because the job sleeping for 1 second is still running
615 }

Callers

nothing calls this directly

Calls 7

newWithSecondsFunction · 0.85
StartMethod · 0.80
StopMethod · 0.80
DoneMethod · 0.80
AddFuncMethod · 0.80
RunMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected