1
0

ticker_test.go 822 B

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