clock_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clock
  14. import (
  15. "testing"
  16. "time"
  17. )
  18. func TestFakeClock(t *testing.T) {
  19. startTime := time.Now()
  20. tc := NewFakeClock(startTime)
  21. tc.Step(time.Second)
  22. now := tc.Now()
  23. if now.Sub(startTime) != time.Second {
  24. t.Errorf("input: %s now=%s gap=%s expected=%s", startTime, now, now.Sub(startTime), time.Second)
  25. }
  26. tt := tc.Now()
  27. tc.SetTime(tt.Add(time.Hour))
  28. if tc.Now().Sub(tt) != time.Hour {
  29. t.Errorf("input: %s now=%s gap=%s expected=%s", tt, tc.Now(), tc.Now().Sub(tt), time.Hour)
  30. }
  31. }
  32. func TestFakeClockSleep(t *testing.T) {
  33. startTime := time.Now()
  34. tc := NewFakeClock(startTime)
  35. tc.Sleep(time.Duration(1) * time.Hour)
  36. now := tc.Now()
  37. if now.Sub(startTime) != time.Hour {
  38. t.Errorf("Fake sleep failed, expected time to advance by one hour, instead, its %v", now.Sub(startTime))
  39. }
  40. }
  41. func TestFakeAfter(t *testing.T) {
  42. tc := NewFakeClock(time.Now())
  43. if tc.HasWaiters() {
  44. t.Errorf("unexpected waiter?")
  45. }
  46. oneSec := tc.After(time.Second)
  47. if !tc.HasWaiters() {
  48. t.Errorf("unexpected lack of waiter?")
  49. }
  50. oneOhOneSec := tc.After(time.Second + time.Millisecond)
  51. twoSec := tc.After(2 * time.Second)
  52. select {
  53. case <-oneSec:
  54. t.Errorf("unexpected channel read")
  55. case <-oneOhOneSec:
  56. t.Errorf("unexpected channel read")
  57. case <-twoSec:
  58. t.Errorf("unexpected channel read")
  59. default:
  60. }
  61. tc.Step(999 * time.Millisecond)
  62. select {
  63. case <-oneSec:
  64. t.Errorf("unexpected channel read")
  65. case <-oneOhOneSec:
  66. t.Errorf("unexpected channel read")
  67. case <-twoSec:
  68. t.Errorf("unexpected channel read")
  69. default:
  70. }
  71. tc.Step(time.Millisecond)
  72. select {
  73. case <-oneSec:
  74. // Expected!
  75. case <-oneOhOneSec:
  76. t.Errorf("unexpected channel read")
  77. case <-twoSec:
  78. t.Errorf("unexpected channel read")
  79. default:
  80. t.Errorf("unexpected non-channel read")
  81. }
  82. tc.Step(time.Millisecond)
  83. select {
  84. case <-oneSec:
  85. // should not double-trigger!
  86. t.Errorf("unexpected channel read")
  87. case <-oneOhOneSec:
  88. // Expected!
  89. case <-twoSec:
  90. t.Errorf("unexpected channel read")
  91. default:
  92. t.Errorf("unexpected non-channel read")
  93. }
  94. }
  95. func TestFakeTick(t *testing.T) {
  96. tc := NewFakeClock(time.Now())
  97. if tc.HasWaiters() {
  98. t.Errorf("unexpected waiter?")
  99. }
  100. oneSec := tc.Tick(time.Second)
  101. if !tc.HasWaiters() {
  102. t.Errorf("unexpected lack of waiter?")
  103. }
  104. oneOhOneSec := tc.Tick(time.Second + time.Millisecond)
  105. twoSec := tc.Tick(2 * time.Second)
  106. select {
  107. case <-oneSec:
  108. t.Errorf("unexpected channel read")
  109. case <-oneOhOneSec:
  110. t.Errorf("unexpected channel read")
  111. case <-twoSec:
  112. t.Errorf("unexpected channel read")
  113. default:
  114. }
  115. tc.Step(999 * time.Millisecond) // t=.999
  116. select {
  117. case <-oneSec:
  118. t.Errorf("unexpected channel read")
  119. case <-oneOhOneSec:
  120. t.Errorf("unexpected channel read")
  121. case <-twoSec:
  122. t.Errorf("unexpected channel read")
  123. default:
  124. }
  125. tc.Step(time.Millisecond) // t=1.000
  126. select {
  127. case <-oneSec:
  128. // Expected!
  129. case <-oneOhOneSec:
  130. t.Errorf("unexpected channel read")
  131. case <-twoSec:
  132. t.Errorf("unexpected channel read")
  133. default:
  134. t.Errorf("unexpected non-channel read")
  135. }
  136. tc.Step(time.Millisecond) // t=1.001
  137. select {
  138. case <-oneSec:
  139. // should not double-trigger!
  140. t.Errorf("unexpected channel read")
  141. case <-oneOhOneSec:
  142. // Expected!
  143. case <-twoSec:
  144. t.Errorf("unexpected channel read")
  145. default:
  146. t.Errorf("unexpected non-channel read")
  147. }
  148. tc.Step(time.Second) // t=2.001
  149. tc.Step(time.Second) // t=3.001
  150. tc.Step(time.Second) // t=4.001
  151. tc.Step(time.Second) // t=5.001
  152. // The one second ticker should not accumulate ticks
  153. accumulatedTicks := 0
  154. drained := false
  155. for !drained {
  156. select {
  157. case <-oneSec:
  158. accumulatedTicks++
  159. default:
  160. drained = true
  161. }
  162. }
  163. if accumulatedTicks != 1 {
  164. t.Errorf("unexpected number of accumulated ticks: %d", accumulatedTicks)
  165. }
  166. }