fix: send on closed channel

This commit is contained in:
Noah Hsu
2022-09-14 15:13:02 +08:00
parent 08a001fbd1
commit 9e5ef974a7
3 changed files with 15 additions and 15 deletions

View File

@ -30,6 +30,10 @@ func (c *Cron) Do(f func()) {
}
func (c *Cron) Stop() {
c.ch <- struct{}{}
close(c.ch)
select {
case _, _ = <-c.ch:
default:
c.ch <- struct{}{}
close(c.ch)
}
}

View File

@ -10,6 +10,7 @@ func TestCron(t *testing.T) {
c.Do(func() {
t.Logf("cron log")
})
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
c.Stop()
c.Stop()
}