fox/timer/timer_test.go
2025-05-25 20:02:15 +08:00

52 lines
946 B
Go

package timer
import (
"github.com/fox/fox/ksync"
"testing"
"time"
)
func runTimerEvent(tm *Timer) {
ksync.GoSafe(func() {
for {
select {
case t, ok := <-tm.chTimer.Reader():
if ok && t != nil && t.Func != nil {
t.Func()
}
}
}
}, nil)
}
func TestTimer(t *testing.T) {
timer := NewTimer()
runTimerEvent(timer)
id := timer.NewTimer(1*time.Second, func() {
t.Log("this is timer1")
}, false)
t.Log("new timer:", id)
id = timer.NewTimer(2*time.Second, func() {
t.Log("this is timer2")
}, false)
t.Log("new timer:", id)
id = timer.NewTimer(3*time.Second, func() {
t.Log("this is timer3")
}, false)
t.Log("new timer:", id)
id = timer.NewTimer(1*time.Second, func() {
t.Log("this is timer4")
}, false)
t.Log("new timer:", id)
timer.CancelTimer(id)
t.Log("cancel timer:", id)
// fmt.Println("rrr id:", id)
time.Sleep(5 * time.Second)
t.Log("rest timer:", timer.timers.Count(), " count")
}