github.com/lingyao2333/mo-zero@v1.4.1/core/timex/ticker_test.go (about) 1 package timex 2 3 import ( 4 "sync/atomic" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestRealTickerDoTick(t *testing.T) { 12 ticker := NewTicker(time.Millisecond * 10) 13 defer ticker.Stop() 14 var count int 15 for range ticker.Chan() { 16 count++ 17 if count > 5 { 18 break 19 } 20 } 21 } 22 23 func TestFakeTicker(t *testing.T) { 24 const total = 5 25 ticker := NewFakeTicker() 26 defer ticker.Stop() 27 28 var count int32 29 go func() { 30 for range ticker.Chan() { 31 if atomic.AddInt32(&count, 1) == total { 32 ticker.Done() 33 } 34 } 35 }() 36 37 for i := 0; i < 5; i++ { 38 ticker.Tick() 39 } 40 41 assert.Nil(t, ticker.Wait(time.Second)) 42 assert.Equal(t, int32(total), atomic.LoadInt32(&count)) 43 } 44 45 func TestFakeTickerTimeout(t *testing.T) { 46 ticker := NewFakeTicker() 47 defer ticker.Stop() 48 49 assert.NotNil(t, ticker.Wait(time.Millisecond)) 50 }