github.com/sandwich-go/boost@v1.3.29/xtime/tick_test.go (about) 1 package xtime 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 ) 8 9 func TestTick(t *testing.T) { 10 d := NewDispatcher(0, WithTickDuration(time.Millisecond*5)) 11 count := 0 12 stop := make(chan struct{}) 13 d.TickFunc("123", func(_ context.Context) { 14 count += 1 15 if count >= 2 { 16 close(stop) 17 } 18 }) 19 d.Start() 20 select { 21 case <-stop: 22 t.Log("trigger tick twice") 23 case <-time.After(time.Millisecond * 12): 24 t.Fatal("ticker not work") 25 } 26 } 27 28 func TestTickExternalHost(t *testing.T) { 29 d := NewDispatcher(0, WithTickDuration(time.Millisecond*5), WithTickHostingMode(false)) 30 count := 0 31 stop := make(chan struct{}) 32 d.TickFunc("123", func(_ context.Context) { 33 count += 1 34 if count >= 2 { 35 close(stop) 36 } 37 }) 38 d.Start() 39 td := d.(TickerDispatcher) 40 41 for { 42 select { 43 case <-td.TickerC(): 44 td.TriggerTickFuncs(context.Background()) 45 case <-stop: 46 t.Log("trigger tick twice") 47 return 48 case <-time.After(time.Millisecond * 12): 49 t.Fatal("ticker not work") 50 } 51 } 52 53 }