diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..25bcc1c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module b612.me/startimer + +go 1.20 diff --git a/time_test.go b/time_test.go index dd158a5..eb144d8 100644 --- a/time_test.go +++ b/time_test.go @@ -216,3 +216,25 @@ func TestPrepareCron2(t *testing.T) { fmt.Println(base) } } + +func TestRun(t *testing.T) { + now := time.Now().Add(time.Second * 2) + tmr := NewTimer(time.Now(), WithStaticDate(now), WithRunCountLimit(1)) + c := make(chan int) + tmr.AddTask(func() { + fmt.Println("hello world") + c <- 1 + }) + if err := tmr.Run(); err != nil { + t.Fatal(err) + } + fmt.Println(tmr.IsRunning()) + select { + case <-c: + fmt.Println(tmr.IsRunning()) + return + case <-time.After(time.Second * 10): + fmt.Println(tmr.IsRunning()) + t.FailNow() + } +} diff --git a/timer.go b/timer.go index 15b52f1..168458b 100644 --- a/timer.go +++ b/timer.go @@ -174,7 +174,7 @@ func (t *StarTimer) Run() error { t.stopCtx, t.stopFn = context.WithCancel(context.Background()) go func() { for { - if t.runCount+1 >= t.runLimit { + if t.runLimit > 0 && t.runCount >= t.runLimit { t.Stop() } now := time.Now() @@ -185,7 +185,7 @@ func (t *StarTimer) Run() error { case <-t.timer.C: t.runCount++ t.nextDate = t.parseNextDate(t.nextDate, false) - if t.nextDate.Before(now) { + if t.nextDate.Before(now) || t.runLimit > 0 && t.runCount >= t.runLimit { t.Stop() } for _, fn := range t.tasks {