| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package testing |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"k8s.io/utils/clock" |
| 24 |
) |
| 25 |
|
| 26 |
type SettablePassiveClock interface { |
| 27 |
clock.PassiveClock |
| 28 |
SetTime(time.Time) |
| 29 |
} |
| 30 |
|
| 31 |
func exercisePassiveClock(t *testing.T, pc SettablePassiveClock) { |
| 32 |
t1 := time.Now() |
| 33 |
t2 := t1.Add(time.Hour) |
| 34 |
pc.SetTime(t1) |
| 35 |
tx := pc.Now() |
| 36 |
if tx != t1 { |
| 37 |
t.Errorf("SetTime(%#+v); Now() => %#+v", t1, tx) |
| 38 |
} |
| 39 |
dx := pc.Since(t1) |
| 40 |
if dx != 0 { |
| 41 |
t.Errorf("Since() => %v", dx) |
| 42 |
} |
| 43 |
pc.SetTime(t2) |
| 44 |
dx = pc.Since(t1) |
| 45 |
if dx != time.Hour { |
| 46 |
t.Errorf("Since() => %v", dx) |
| 47 |
} |
| 48 |
tx = pc.Now() |
| 49 |
if tx != t2 { |
| 50 |
t.Errorf("Now() => %#+v", tx) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestFakePassiveClock(t *testing.T) { |
| 55 |
startTime := time.Now() |
| 56 |
tc := NewFakePassiveClock(startTime) |
| 57 |
exercisePassiveClock(t, tc) |
| 58 |
} |
| 59 |
|
| 60 |
func TestFakeClock(t *testing.T) { |
| 61 |
startTime := time.Now() |
| 62 |
tc := NewFakeClock(startTime) |
| 63 |
exercisePassiveClock(t, tc) |
| 64 |
tc.SetTime(startTime) |
| 65 |
tc.Step(time.Second) |
| 66 |
now := tc.Now() |
| 67 |
if now.Sub(startTime) != time.Second { |
| 68 |
t.Errorf("input: %s now=%s gap=%s expected=%s", startTime, now, now.Sub(startTime), time.Second) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestFakeClockSleep(t *testing.T) { |
| 73 |
startTime := time.Now() |
| 74 |
tc := NewFakeClock(startTime) |
| 75 |
tc.Sleep(time.Duration(1) * time.Hour) |
| 76 |
now := tc.Now() |
| 77 |
if now.Sub(startTime) != time.Hour { |
| 78 |
t.Errorf("Fake sleep failed, expected time to advance by one hour, instead, its %v", now.Sub(startTime)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestFakeAfter(t *testing.T) { |
| 83 |
tc := NewFakeClock(time.Now()) |
| 84 |
if tc.HasWaiters() { |
| 85 |
t.Errorf("unexpected waiter?") |
| 86 |
} |
| 87 |
oneSec := tc.After(time.Second) |
| 88 |
if !tc.HasWaiters() { |
| 89 |
t.Errorf("unexpected lack of waiter?") |
| 90 |
} |
| 91 |
|
| 92 |
oneOhOneSec := tc.After(time.Second + time.Millisecond) |
| 93 |
twoSec := tc.After(2 * time.Second) |
| 94 |
select { |
| 95 |
case <-oneSec: |
| 96 |
t.Errorf("unexpected channel read") |
| 97 |
case <-oneOhOneSec: |
| 98 |
t.Errorf("unexpected channel read") |
| 99 |
case <-twoSec: |
| 100 |
t.Errorf("unexpected channel read") |
| 101 |
default: |
| 102 |
} |
| 103 |
|
| 104 |
tc.Step(999 * time.Millisecond) |
| 105 |
select { |
| 106 |
case <-oneSec: |
| 107 |
t.Errorf("unexpected channel read") |
| 108 |
case <-oneOhOneSec: |
| 109 |
t.Errorf("unexpected channel read") |
| 110 |
case <-twoSec: |
| 111 |
t.Errorf("unexpected channel read") |
| 112 |
default: |
| 113 |
} |
| 114 |
|
| 115 |
tc.Step(time.Millisecond) |
| 116 |
select { |
| 117 |
case <-oneSec: |
| 118 |
// Expected! |
| 119 |
case <-oneOhOneSec: |
| 120 |
t.Errorf("unexpected channel read") |
| 121 |
case <-twoSec: |
| 122 |
t.Errorf("unexpected channel read") |
| 123 |
default: |
| 124 |
t.Errorf("unexpected non-channel read") |
| 125 |
} |
| 126 |
tc.Step(time.Millisecond) |
| 127 |
select { |
| 128 |
case <-oneSec: |
| 129 |
// should not double-trigger! |
| 130 |
t.Errorf("unexpected channel read") |
| 131 |
case <-oneOhOneSec: |
| 132 |
// Expected! |
| 133 |
case <-twoSec: |
| 134 |
t.Errorf("unexpected channel read") |
| 135 |
default: |
| 136 |
t.Errorf("unexpected non-channel read") |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func TestFakeAfterFunc(t *testing.T) { |
| 141 |
tc := NewFakeClock(time.Now()) |
| 142 |
if tc.HasWaiters() { |
| 143 |
t.Errorf("unexpected waiter?") |
| 144 |
} |
| 145 |
expectOneSecTimerFire := false |
| 146 |
oneSecTimerFire := 0 |
| 147 |
tc.AfterFunc(time.Second, func() { |
| 148 |
if !expectOneSecTimerFire { |
| 149 |
t.Errorf("oneSecTimer func fired") |
| 150 |
} else { |
| 151 |
oneSecTimerFire++ |
| 152 |
} |
| 153 |
}) |
| 154 |
if !tc.HasWaiters() { |
| 155 |
t.Errorf("unexpected lack of waiter?") |
| 156 |
} |
| 157 |
|
| 158 |
expectOneOhOneSecTimerFire := false |
| 159 |
oneOhOneSecTimerFire := 0 |
| 160 |
tc.AfterFunc(time.Second+time.Millisecond, func() { |
| 161 |
if !expectOneOhOneSecTimerFire { |
| 162 |
t.Errorf("oneOhOneSecTimer func fired") |
| 163 |
} else { |
| 164 |
oneOhOneSecTimerFire++ |
| 165 |
} |
| 166 |
}) |
| 167 |
|
| 168 |
expectTwoSecTimerFire := false |
| 169 |
twoSecTimerFire := 0 |
| 170 |
twoSecTimer := tc.AfterFunc(2*time.Second, func() { |
| 171 |
if !expectTwoSecTimerFire { |
| 172 |
t.Errorf("twoSecTimer func fired") |
| 173 |
} else { |
| 174 |
twoSecTimerFire++ |
| 175 |
} |
| 176 |
}) |
| 177 |
|
| 178 |
tc.Step(999 * time.Millisecond) |
| 179 |
|
| 180 |
expectOneSecTimerFire = true |
| 181 |
tc.Step(time.Millisecond) |
| 182 |
if oneSecTimerFire != 1 { |
| 183 |
t.Errorf("expected oneSecTimerFire=1, got %d", oneSecTimerFire) |
| 184 |
} |
| 185 |
expectOneSecTimerFire = false |
| 186 |
|
| 187 |
expectOneOhOneSecTimerFire = true |
| 188 |
tc.Step(time.Millisecond) |
| 189 |
if oneOhOneSecTimerFire != 1 { |
| 190 |
// should not double-trigger! |
| 191 |
t.Errorf("expected oneOhOneSecTimerFire=1, got %d", oneOhOneSecTimerFire) |
| 192 |
} |
| 193 |
expectOneOhOneSecTimerFire = false |
| 194 |
|
| 195 |
// ensure a canceled timer doesn't fire |
| 196 |
twoSecTimer.Stop() |
| 197 |
tc.Step(time.Second) |
| 198 |
} |
| 199 |
|
| 200 |
func TestFakeTick(t *testing.T) { |
| 201 |
tc := NewFakeClock(time.Now()) |
| 202 |
if tc.HasWaiters() { |
| 203 |
t.Errorf("unexpected waiter?") |
| 204 |
} |
| 205 |
oneSec := tc.Tick(time.Second) |
| 206 |
if !tc.HasWaiters() { |
| 207 |
t.Errorf("unexpected lack of waiter?") |
| 208 |
} |
| 209 |
|
| 210 |
oneOhOneSec := tc.Tick(time.Second + time.Millisecond) |
| 211 |
twoSec := tc.Tick(2 * time.Second) |
| 212 |
select { |
| 213 |
case <-oneSec: |
| 214 |
t.Errorf("unexpected channel read") |
| 215 |
case <-oneOhOneSec: |
| 216 |
t.Errorf("unexpected channel read") |
| 217 |
case <-twoSec: |
| 218 |
t.Errorf("unexpected channel read") |
| 219 |
default: |
| 220 |
} |
| 221 |
|
| 222 |
tc.Step(999 * time.Millisecond) // t=.999 |
| 223 |
select { |
| 224 |
case <-oneSec: |
| 225 |
t.Errorf("unexpected channel read") |
| 226 |
case <-oneOhOneSec: |
| 227 |
t.Errorf("unexpected channel read") |
| 228 |
case <-twoSec: |
| 229 |
t.Errorf("unexpected channel read") |
| 230 |
default: |
| 231 |
} |
| 232 |
|
| 233 |
tc.Step(time.Millisecond) // t=1.000 |
| 234 |
select { |
| 235 |
case <-oneSec: |
| 236 |
// Expected! |
| 237 |
case <-oneOhOneSec: |
| 238 |
t.Errorf("unexpected channel read") |
| 239 |
case <-twoSec: |
| 240 |
t.Errorf("unexpected channel read") |
| 241 |
default: |
| 242 |
t.Errorf("unexpected non-channel read") |
| 243 |
} |
| 244 |
tc.Step(time.Millisecond) // t=1.001 |
| 245 |
select { |
| 246 |
case <-oneSec: |
| 247 |
// should not double-trigger! |
| 248 |
t.Errorf("unexpected channel read") |
| 249 |
case <-oneOhOneSec: |
| 250 |
// Expected! |
| 251 |
case <-twoSec: |
| 252 |
t.Errorf("unexpected channel read") |
| 253 |
default: |
| 254 |
t.Errorf("unexpected non-channel read") |
| 255 |
} |
| 256 |
|
| 257 |
tc.Step(time.Second) // t=2.001 |
| 258 |
tc.Step(time.Second) // t=3.001 |
| 259 |
tc.Step(time.Second) // t=4.001 |
| 260 |
tc.Step(time.Second) // t=5.001 |
| 261 |
|
| 262 |
// The one second ticker should not accumulate ticks |
| 263 |
accumulatedTicks := 0 |
| 264 |
drained := false |
| 265 |
for !drained { |
| 266 |
select { |
| 267 |
case <-oneSec: |
| 268 |
accumulatedTicks++ |
| 269 |
default: |
| 270 |
drained = true |
| 271 |
} |
| 272 |
} |
| 273 |
if accumulatedTicks != 1 { |
| 274 |
t.Errorf("unexpected number of accumulated ticks: %d", accumulatedTicks) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
func TestFakeStop(t *testing.T) { |
| 279 |
tc := NewFakeClock(time.Now()) |
| 280 |
timer := tc.NewTimer(time.Second) |
| 281 |
if !tc.HasWaiters() { |
| 282 |
t.Errorf("expected a waiter to be present, but it is not") |
| 283 |
} |
| 284 |
if !timer.Stop() { |
| 285 |
t.Errorf("stop should return true as we are stopping an unexpired timer") |
| 286 |
} |
| 287 |
if tc.HasWaiters() { |
| 288 |
t.Errorf("expected existing waiter to be cleaned up, but it is still present") |
| 289 |
} |
| 290 |
if timer.Stop() { |
| 291 |
t.Errorf("stop should return false as the timer has already been stopped") |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// This tests the pattern documented in the go docs here: https://golang.org/pkg/time/#Timer.Stop |
| 296 |
// This pattern is required to safely reset a timer prior to Go 1.23, so should be common. |
| 297 |
// This also tests resetting the timer |
| 298 |
func TestFakeStopDrain(t *testing.T) { |
| 299 |
start := time.Time{} |
| 300 |
tc := NewFakeClock(start) |
| 301 |
timer := tc.NewTimer(time.Second) |
| 302 |
tc.Step(1 * time.Second) |
| 303 |
// Effectively `if !timer.Stop { <-t.C }` but with more asserts |
| 304 |
if timer.Stop() { |
| 305 |
t.Errorf("stop should report the timer had triggered") |
| 306 |
} |
| 307 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(1 * time.Second)) { |
| 308 |
t.Errorf("timer should have ticked after 1 second, got %v", readTime) |
| 309 |
} |
| 310 |
|
| 311 |
if timer.Reset(time.Second) { |
| 312 |
t.Errorf("reset should return false as the timer had expired") |
| 313 |
} |
| 314 |
if !tc.HasWaiters() { |
| 315 |
t.Errorf("expected a waiter to be present, but it is not") |
| 316 |
} |
| 317 |
select { |
| 318 |
case <-timer.C(): |
| 319 |
t.Fatal("got time early on clock; haven't stepped yet") |
| 320 |
default: |
| 321 |
} |
| 322 |
tc.Step(1 * time.Second) |
| 323 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(2 * time.Second)) { |
| 324 |
t.Errorf("timer should have ticked again after reset + 1 more second, got %v", readTime) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
func TestFakeReset(t *testing.T) { |
| 329 |
start := time.Now() |
| 330 |
t.Run("reset active timer", func(t *testing.T) { |
| 331 |
tc := NewFakeClock(start) |
| 332 |
timer := tc.NewTimer(time.Second) |
| 333 |
if !tc.HasWaiters() { |
| 334 |
t.Errorf("expected a waiter to be present, but it is not") |
| 335 |
} |
| 336 |
tc.Step(999 * time.Millisecond) |
| 337 |
if !tc.HasWaiters() { |
| 338 |
t.Errorf("expected a waiter to be present, but it is not") |
| 339 |
} |
| 340 |
if !timer.Reset(time.Second) { |
| 341 |
t.Errorf("reset should return true as the timer is active") |
| 342 |
} |
| 343 |
tc.Step(time.Millisecond) |
| 344 |
if !tc.HasWaiters() { |
| 345 |
t.Errorf("expected a waiter to be present, but it is not") |
| 346 |
} |
| 347 |
tc.Step(999 * time.Millisecond) |
| 348 |
if tc.HasWaiters() { |
| 349 |
t.Errorf("expected existing waiter to be cleaned up, but it is still present") |
| 350 |
} |
| 351 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(1999 * time.Millisecond)) { |
| 352 |
t.Errorf("timer should have ticked after reset + 1 second, got %v", readTime) |
| 353 |
} |
| 354 |
}) |
| 355 |
|
| 356 |
t.Run("reset expired timer", func(t *testing.T) { |
| 357 |
tc := NewFakeClock(start) |
| 358 |
timer := tc.NewTimer(time.Second) |
| 359 |
if !tc.HasWaiters() { |
| 360 |
t.Errorf("expected a waiter to be present, but it is not") |
| 361 |
} |
| 362 |
tc.Step(time.Second) |
| 363 |
if tc.HasWaiters() { |
| 364 |
t.Errorf("expected existing waiter to be cleaned up, but it is still present") |
| 365 |
} |
| 366 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(time.Second)) { |
| 367 |
t.Errorf("timer should have ticked after 1 second, got %v", readTime) |
| 368 |
} |
| 369 |
if timer.Reset(time.Second) { |
| 370 |
t.Errorf("reset should return false as the timer had expired") |
| 371 |
} |
| 372 |
if !tc.HasWaiters() { |
| 373 |
t.Errorf("expected a waiter to be present, but it is not") |
| 374 |
} |
| 375 |
tc.Step(time.Second) |
| 376 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(2 * time.Second)) { |
| 377 |
t.Errorf("timer should have ticked again after reset + 1 more second, got %v", readTime) |
| 378 |
} |
| 379 |
}) |
| 380 |
|
| 381 |
t.Run("reset stopped timer", func(t *testing.T) { |
| 382 |
tc := NewFakeClock(start) |
| 383 |
timer := tc.NewTimer(time.Second) |
| 384 |
if !tc.HasWaiters() { |
| 385 |
t.Errorf("expected a waiter to be present, but it is not") |
| 386 |
} |
| 387 |
timer.Stop() |
| 388 |
if timer.Reset(time.Second) { |
| 389 |
t.Errorf("reset should return false as the timer had been stopped") |
| 390 |
} |
| 391 |
if !tc.HasWaiters() { |
| 392 |
t.Errorf("expected a waiter to be present, but it is not") |
| 393 |
} |
| 394 |
tc.Step(time.Second) |
| 395 |
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(time.Second)) { |
| 396 |
t.Errorf("timer should have ticked after reset + 1 second, got %v", readTime) |
| 397 |
} |
| 398 |
}) |
| 399 |
} |
| 400 |
|
| 401 |
func TestTimerNegative(t *testing.T) { |
| 402 |
tc := NewFakeClock(time.Now()) |
| 403 |
timer := tc.NewTimer(-1 * time.Second) |
| 404 |
if !tc.HasWaiters() { |
| 405 |
t.Errorf("expected a waiter to be present, but it is not") |
| 406 |
} |
| 407 |
// force waiters to be called |
| 408 |
tc.Step(0) |
| 409 |
tick := assertReadTime(t, timer.C()) |
| 410 |
if tick != tc.Now() { |
| 411 |
t.Errorf("expected -1s to turn into now: %v != %v", tick, tc.Now()) |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestTickNegative(t *testing.T) { |
| 416 |
// The stdlib 'Tick' returns nil for negative and zero values, so our fake |
| 417 |
// should too. |
| 418 |
tc := NewFakeClock(time.Now()) |
| 419 |
if tick := tc.Tick(-1 * time.Second); tick != nil { |
| 420 |
t.Errorf("expected negative tick to be nil: %v", tick) |
| 421 |
} |
| 422 |
if tick := tc.Tick(0); tick != nil { |
| 423 |
t.Errorf("expected negative tick to be nil: %v", tick) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// assertReadTime asserts that the channel can be read and returns the time it |
| 428 |
// reads from the channel. |
| 429 |
func assertReadTime(t testing.TB, c <-chan time.Time) time.Time { |
| 430 |
type helper interface { |
| 431 |
Helper() |
| 432 |
} |
| 433 |
if h, ok := t.(helper); ok { |
| 434 |
h.Helper() |
| 435 |
} |
| 436 |
select { |
| 437 |
case ti, ok := <-c: |
| 438 |
if !ok { |
| 439 |
t.Fatalf("expected to read time from channel, but it was closed") |
| 440 |
} |
| 441 |
return ti |
| 442 |
default: |
| 443 |
t.Fatalf("expected to read time from channel, but couldn't") |
| 444 |
} |
| 445 |
panic("unreachable") |
| 446 |
} |
| 447 |
|
| 448 |
func TestFakeClockWaiters(t *testing.T) { |
| 449 |
startTime := time.Now() |
| 450 |
tc := NewFakeClock(startTime) |
| 451 |
|
| 452 |
// Initial state |
| 453 |
if count := tc.Waiters(); count != 0 { |
| 454 |
t.Errorf("Expected 0 waiters initially, got %d", count) |
| 455 |
} |
| 456 |
|
| 457 |
// Add a Timer |
| 458 |
timer1 := tc.NewTimer(1 * time.Second) |
| 459 |
if count := tc.Waiters(); count != 1 { |
| 460 |
t.Errorf("Expected 1 waiter after NewTimer, got %d", count) |
| 461 |
} |
| 462 |
|
| 463 |
// Add an After |
| 464 |
_ = tc.After(2 * time.Second) |
| 465 |
if count := tc.Waiters(); count != 2 { |
| 466 |
t.Errorf("Expected 2 waiters after After, got %d", count) |
| 467 |
} |
| 468 |
|
| 469 |
// Add a Ticker |
| 470 |
ticker := tc.NewTicker(3 * time.Second) |
| 471 |
if count := tc.Waiters(); count != 3 { |
| 472 |
t.Errorf("Expected 3 waiters after NewTicker, got %d", count) |
| 473 |
} |
| 474 |
|
| 475 |
// Step past the first timer |
| 476 |
tc.Step(1 * time.Second) |
| 477 |
<-timer1.C() // Drain channel |
| 478 |
if count := tc.Waiters(); count != 2 { |
| 479 |
t.Errorf("Expected 2 waiters after first timer fired, got %d", count) |
| 480 |
} |
| 481 |
|
| 482 |
// Step past the After |
| 483 |
tc.Step(1 * time.Second) |
| 484 |
// Note: After channel is implicitly drained by setTimeLocked |
| 485 |
if count := tc.Waiters(); count != 1 { |
| 486 |
t.Errorf("Expected 1 waiter after After fired, got %d", count) |
| 487 |
} |
| 488 |
|
| 489 |
// Step past the Ticker (it should re-arm) |
| 490 |
tc.Step(1 * time.Second) |
| 491 |
<-ticker.C() // Drain channel |
| 492 |
if count := tc.Waiters(); count != 1 { |
| 493 |
t.Errorf("Expected 1 waiter after Ticker fired (should re-arm), got %d", count) |
| 494 |
} |
| 495 |
|
| 496 |
// Stop the ticker (Note: fakeTicker.Stop is currently a no-op, so this won't change the count) |
| 497 |
// If fakeTicker.Stop were implemented to remove the waiter, the expected count would be 0. |
| 498 |
ticker.Stop() |
| 499 |
if count := tc.Waiters(); count != 1 { |
| 500 |
t.Errorf("Expected 1 waiter after stopping ticker (no-op), got %d", count) |
| 501 |
} |
| 502 |
|
| 503 |
// Add another timer and stop it |
| 504 |
timer2 := tc.NewTimer(5 * time.Second) |
| 505 |
if count := tc.Waiters(); count != 2 { |
| 506 |
t.Errorf("Expected 2 waiters after adding second timer, got %d", count) |
| 507 |
} |
| 508 |
if stopped := timer2.Stop(); !stopped { |
| 509 |
t.Errorf("Expected timer2.Stop() to return true") |
| 510 |
} |
| 511 |
if count := tc.Waiters(); count != 1 { |
| 512 |
t.Errorf("Expected 1 waiter after stopping second timer, got %d", count) |
| 513 |
} |
| 514 |
} |