master v0.0.1
兔子 2 years ago
parent 5eb59878cb
commit 0cb637b84c

@ -0,0 +1,3 @@
module b612.me/startimer
go 1.20

@ -216,3 +216,25 @@ func TestPrepareCron2(t *testing.T) {
fmt.Println(base) 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()
}
}

@ -174,7 +174,7 @@ func (t *StarTimer) Run() error {
t.stopCtx, t.stopFn = context.WithCancel(context.Background()) t.stopCtx, t.stopFn = context.WithCancel(context.Background())
go func() { go func() {
for { for {
if t.runCount+1 >= t.runLimit { if t.runLimit > 0 && t.runCount >= t.runLimit {
t.Stop() t.Stop()
} }
now := time.Now() now := time.Now()
@ -185,7 +185,7 @@ func (t *StarTimer) Run() error {
case <-t.timer.C: case <-t.timer.C:
t.runCount++ t.runCount++
t.nextDate = t.parseNextDate(t.nextDate, false) 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() t.Stop()
} }
for _, fn := range t.tasks { for _, fn := range t.tasks {

Loading…
Cancel
Save