clockwork_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package clockwork
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestFakeClockAfter(t *testing.T) {
  8. fc := &fakeClock{}
  9. zero := fc.After(0)
  10. select {
  11. case <-zero:
  12. default:
  13. t.Errorf("zero did not return!")
  14. }
  15. one := fc.After(1)
  16. two := fc.After(2)
  17. six := fc.After(6)
  18. ten := fc.After(10)
  19. fc.Advance(1)
  20. select {
  21. case <-one:
  22. default:
  23. t.Errorf("one did not return!")
  24. }
  25. select {
  26. case <-two:
  27. t.Errorf("two returned prematurely!")
  28. case <-six:
  29. t.Errorf("six returned prematurely!")
  30. case <-ten:
  31. t.Errorf("ten returned prematurely!")
  32. default:
  33. }
  34. fc.Advance(1)
  35. select {
  36. case <-two:
  37. default:
  38. t.Errorf("two did not return!")
  39. }
  40. select {
  41. case <-six:
  42. t.Errorf("six returned prematurely!")
  43. case <-ten:
  44. t.Errorf("ten returned prematurely!")
  45. default:
  46. }
  47. fc.Advance(1)
  48. select {
  49. case <-six:
  50. t.Errorf("six returned prematurely!")
  51. case <-ten:
  52. t.Errorf("ten returned prematurely!")
  53. default:
  54. }
  55. fc.Advance(3)
  56. select {
  57. case <-six:
  58. default:
  59. t.Errorf("six did not return!")
  60. }
  61. select {
  62. case <-ten:
  63. t.Errorf("ten returned prematurely!")
  64. default:
  65. }
  66. fc.Advance(100)
  67. select {
  68. case <-ten:
  69. default:
  70. t.Errorf("ten did not return!")
  71. }
  72. }
  73. func TestNotifyBlockers(t *testing.T) {
  74. b1 := &blocker{1, make(chan struct{})}
  75. b2 := &blocker{2, make(chan struct{})}
  76. b3 := &blocker{5, make(chan struct{})}
  77. b4 := &blocker{10, make(chan struct{})}
  78. b5 := &blocker{10, make(chan struct{})}
  79. bs := []*blocker{b1, b2, b3, b4, b5}
  80. bs1 := notifyBlockers(bs, 2)
  81. if n := len(bs1); n != 4 {
  82. t.Fatalf("got %d blockers, want %d", n, 4)
  83. }
  84. select {
  85. case <-b2.ch:
  86. case <-time.After(time.Second):
  87. t.Fatalf("timed out waiting for channel close!")
  88. }
  89. bs2 := notifyBlockers(bs1, 10)
  90. if n := len(bs2); n != 2 {
  91. t.Fatalf("got %d blockers, want %d", n, 2)
  92. }
  93. select {
  94. case <-b4.ch:
  95. case <-time.After(time.Second):
  96. t.Fatalf("timed out waiting for channel close!")
  97. }
  98. select {
  99. case <-b5.ch:
  100. case <-time.After(time.Second):
  101. t.Fatalf("timed out waiting for channel close!")
  102. }
  103. }
  104. func TestNewFakeClock(t *testing.T) {
  105. fc := NewFakeClock()
  106. now := fc.Now()
  107. if now.IsZero() {
  108. t.Fatalf("fakeClock.Now() fulfills IsZero")
  109. }
  110. now2 := fc.Now()
  111. if !reflect.DeepEqual(now, now2) {
  112. t.Fatalf("fakeClock.Now() returned different value: want=%#v got=%#v", now, now2)
  113. }
  114. }
  115. func TestNewFakeClockAt(t *testing.T) {
  116. t1 := time.Date(1999, time.February, 3, 4, 5, 6, 7, time.UTC)
  117. fc := NewFakeClockAt(t1)
  118. now := fc.Now()
  119. if !reflect.DeepEqual(now, t1) {
  120. t.Fatalf("fakeClock.Now() returned unexpected non-initialised value: want=%#v, got %#v", t1, now)
  121. }
  122. }