github.com/sandwich-go/boost@v1.3.29/xtime/dispatcher_test.go (about) 1 package xtime 2 3 import ( 4 "testing" 5 "time" 6 7 . "github.com/smartystreets/goconvey/convey" 8 ) 9 10 func TestTimerDispatcher(t *testing.T) { 11 12 Convey("First, create a TestTimerDispatcher", t, func() { 13 dispatcher := NewDispatcher(10) 14 a := 0 15 dispatcher.AfterFunc(time.Second, func() { 16 a++ 17 t.Log("AfterFunc") 18 }) 19 timer := dispatcher.AfterFuncInDomain(time.Second, func() { 20 a++ 21 t.Log("AfterFuncInDomain") 22 }, "goconvey") 23 timer.t.Reset(0) 24 dispatcher.RemoveAllTimerInDomain(DefaultTimerDomain) 25 time.Sleep(500 * time.Millisecond) 26 dispatcher.Close() 27 dispatcher.Close() 28 So(a, ShouldEqual, 1) 29 30 }) 31 } 32 33 func TestTimerResetDispatcher(t *testing.T) { 34 Convey("normal timer", t, func() { 35 a := 0 36 var tm *time.Timer 37 tm = time.AfterFunc(1*time.Second, func() { 38 a++ 39 tm.Reset(1 * time.Second) 40 }) 41 time.Sleep(2500 * time.Millisecond) 42 tm.Stop() 43 So(a, ShouldEqual, 2) 44 }) 45 Convey("test reset AfterFunc", t, func() { 46 dp := NewDispatcher(10) 47 go func() { 48 for { 49 select { 50 case tn := <-dp.TimerNotify(): 51 if tn != nil { 52 tn.Cb() 53 } 54 } 55 } 56 }() 57 a := 0 58 var tt *DanglingTimer 59 tt = dp.AfterFuncWithOwnershipTransferInDomain(time.Second, func() { 60 a++ 61 t.Log("reset AfterFunc") 62 tt.Reset(time.Second) 63 }, "reset") 64 time.Sleep(3500 * time.Millisecond) 65 So(a, ShouldEqual, 3) 66 }) 67 }