This commit is contained in:
Fangliding
2026-11-23 02:29:56 +08:00
parent 3ff9cec713
commit 8d6509bdcd
6 changed files with 21 additions and 32 deletions
+3 -7
View File
@@ -58,15 +58,11 @@ func (t *Periodic) Start() error {
return nil
}
t.running = true
t.timer = time.AfterFunc(t.Interval, func() {
t.checkedExecute()
})
t.access.Unlock()
if err := t.checkedExecute(); err != nil {
t.access.Lock()
t.running = false
t.access.Unlock()
return err
}
return nil
}
+6 -6
View File
@@ -20,17 +20,17 @@ func TestPeriodicTaskStop(t *testing.T) {
common.Must(task.Start())
time.Sleep(time.Second * 5)
common.Must(task.Close())
if value != 3 {
t.Fatal("expected 3, but got ", value)
if value != 2 {
t.Fatal("expected 2, but got ", value)
}
time.Sleep(time.Second * 4)
if value != 3 {
t.Fatal("expected 3, but got ", value)
if value != 2 {
t.Fatal("expected 2, but got ", value)
}
common.Must(task.Start())
time.Sleep(time.Second * 3)
if value != 5 {
t.Fatal("Expected 5, but ", value)
if value != 3 {
t.Fatal("Expected 3, but ", value)
}
common.Must(task.Close())
}