samba/pkg/service/timer_test.go
2025-06-04 09:51:39 +08:00

52 lines
897 B
Go

package service
import (
"samba/pkg/ksync"
"testing"
"time"
)
func runTimerEvent(tm *Timer) {
ksync.GoSafe(func() {
for {
select {
case t, ok := <-tm.chTimer:
if ok && t != nil && t.cb != nil {
t.cb()
}
}
}
}, nil)
}
func TestTimer(t *testing.T) {
timer := NewTimer()
runTimerEvent(timer)
id := timer.NewTimer(1*time.Second, func() {
t.Log("this is timer1")
})
t.Log("new timer:", id)
id = timer.NewTimer(2*time.Second, func() {
t.Log("this is timer2")
})
t.Log("new timer:", id)
id = timer.NewTimer(3*time.Second, func() {
t.Log("this is timer3")
})
t.Log("new timer:", id)
id = timer.NewTimer(1*time.Second, func() {
t.Log("this is timer4")
})
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")
}