ticker_test.go 678 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }