| 1 | package clock |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "sync" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // Ensure that the clock's After channel sends at the correct time. |
| 13 | func TestClock_After(t *testing.T) { |
| 14 | var ok bool |
| 15 | go func() { |
| 16 | time.Sleep(10 * time.Millisecond) |
| 17 | ok = true |
| 18 | }() |
| 19 | go func() { |
| 20 | time.Sleep(30 * time.Millisecond) |
| 21 | t.Fatal("too late") |
| 22 | }() |
| 23 | gosched() |
| 24 | |
| 25 | <-New().After(20 * time.Millisecond) |
| 26 | if !ok { |
| 27 | t.Fatal("too early") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Ensure that the clock's AfterFunc executes at the correct time. |
| 32 | func TestClock_AfterFunc(t *testing.T) { |
| 33 | var ok bool |
| 34 | go func() { |
| 35 | time.Sleep(10 * time.Millisecond) |
| 36 | ok = true |
| 37 | }() |
| 38 | go func() { |
| 39 | time.Sleep(30 * time.Millisecond) |
| 40 | t.Fatal("too late") |
| 41 | }() |
| 42 | gosched() |
| 43 | |
| 44 | var wg sync.WaitGroup |
| 45 | wg.Add(1) |
| 46 | New().AfterFunc(20*time.Millisecond, func() { |
| 47 | wg.Done() |
| 48 | }) |
| 49 | wg.Wait() |
| 50 | if !ok { |
| 51 | t.Fatal("too early") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Ensure that the clock's time matches the standary library. |
| 56 | func TestClock_Now(t *testing.T) { |
| 57 | a := time.Now().Round(time.Second) |
| 58 | b := New().Now().Round(time.Second) |
| 59 | if !a.Equal(b) { |
| 60 | t.Errorf("not equal: %s != %s", a, b) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Ensure that the clock sleeps for the appropriate amount of time. |
| 65 | func TestClock_Sleep(t *testing.T) { |
| 66 | var ok bool |
| 67 | go func() { |
| 68 | time.Sleep(10 * time.Millisecond) |
| 69 | ok = true |
| 70 | }() |
| 71 | go func() { |
| 72 | time.Sleep(30 * time.Millisecond) |
| 73 | t.Fatal("too late") |
| 74 | }() |
| 75 | gosched() |
| 76 | |
| 77 | New().Sleep(20 * time.Millisecond) |
| 78 | if !ok { |
| 79 | t.Fatal("too early") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Ensure that the clock ticks correctly. |
| 84 | func TestClock_Tick(t *testing.T) { |
| 85 | var ok bool |
| 86 | go func() { |
| 87 | time.Sleep(10 * time.Millisecond) |
| 88 | ok = true |
| 89 | }() |
| 90 | go func() { |
| 91 | time.Sleep(50 * time.Millisecond) |
| 92 | t.Fatal("too late") |
| 93 | }() |
| 94 | gosched() |
| 95 | |
| 96 | c := New().Tick(20 * time.Millisecond) |
| 97 | <-c |
| 98 | <-c |
| 99 | if !ok { |
| 100 | t.Fatal("too early") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Ensure that the clock's ticker ticks correctly. |
| 105 | func TestClock_Ticker(t *testing.T) { |
| 106 | var ok bool |
| 107 | go func() { |
| 108 | time.Sleep(100 * time.Millisecond) |
| 109 | ok = true |
| 110 | }() |
| 111 | go func() { |
| 112 | time.Sleep(200 * time.Millisecond) |
| 113 | t.Fatal("too late") |
| 114 | }() |
| 115 | gosched() |
| 116 | |
| 117 | ticker := New().Ticker(50 * time.Millisecond) |
| 118 | <-ticker.C |
| 119 | <-ticker.C |
| 120 | if !ok { |
| 121 | t.Fatal("too early") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Ensure that the clock's ticker can stop correctly. |
| 126 | func TestClock_Ticker_Stp(t *testing.T) { |
| 127 | var ok bool |
| 128 | go func() { |
| 129 | time.Sleep(10 * time.Millisecond) |
| 130 | ok = true |
| 131 | }() |
| 132 | gosched() |
| 133 | |
| 134 | ticker := New().Ticker(20 * time.Millisecond) |
| 135 | <-ticker.C |
| 136 | ticker.Stop() |
| 137 | select { |
| 138 | case <-ticker.C: |
| 139 | t.Fatal("unexpected send") |
| 140 | case <-time.After(30 * time.Millisecond): |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Ensure that the clock's timer waits correctly. |
| 145 | func TestClock_Timer(t *testing.T) { |
| 146 | var ok bool |
| 147 | go func() { |
| 148 | time.Sleep(10 * time.Millisecond) |
| 149 | ok = true |
| 150 | }() |
| 151 | go func() { |
| 152 | time.Sleep(30 * time.Millisecond) |
| 153 | t.Fatal("too late") |
| 154 | }() |
| 155 | gosched() |
| 156 | |
| 157 | timer := New().Timer(20 * time.Millisecond) |
| 158 | <-timer.C |
| 159 | if !ok { |
| 160 | t.Fatal("too early") |
| 161 | } |
| 162 | |
| 163 | if timer.Stop() { |
| 164 | t.Fatal("timer still running") |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Ensure that the clock's timer can be stopped. |
| 169 | func TestClock_Timer_Stop(t *testing.T) { |
| 170 | var ok bool |
| 171 | go func() { |
| 172 | time.Sleep(10 * time.Millisecond) |
| 173 | ok = true |
| 174 | }() |
| 175 | |
| 176 | timer := New().Timer(20 * time.Millisecond) |
| 177 | if !timer.Stop() { |
| 178 | t.Fatal("timer not running") |
| 179 | } |
| 180 | if timer.Stop() { |
| 181 | t.Fatal("timer wasn't cancelled") |
| 182 | } |
| 183 | select { |
| 184 | case <-timer.C: |
| 185 | t.Fatal("unexpected send") |
| 186 | case <-time.After(30 * time.Millisecond): |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Ensure that the clock's timer can be reset. |
| 191 | func TestClock_Timer_Reset(t *testing.T) { |
| 192 | var ok bool |
| 193 | go func() { |
| 194 | time.Sleep(20 * time.Millisecond) |
| 195 | ok = true |
| 196 | }() |
| 197 | go func() { |
| 198 | time.Sleep(30 * time.Millisecond) |
| 199 | t.Fatal("too late") |
| 200 | }() |
| 201 | gosched() |
| 202 | |
| 203 | timer := New().Timer(10 * time.Millisecond) |
| 204 | if !timer.Reset(20 * time.Millisecond) { |
| 205 | t.Fatal("timer not running") |
| 206 | } |
| 207 | |
| 208 | <-timer.C |
| 209 | if !ok { |
| 210 | t.Fatal("too early") |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Ensure that the mock's After channel sends at the correct time. |
| 215 | func TestMock_After(t *testing.T) { |
| 216 | var ok int32 |
| 217 | clock := NewMock() |
| 218 | |
| 219 | // Create a channel to execute after 10 mock seconds. |
| 220 | ch := clock.After(10 * time.Second) |
| 221 | go func(ch <-chan time.Time) { |
| 222 | <-ch |
| 223 | atomic.StoreInt32(&ok, 1) |
| 224 | }(ch) |
| 225 | |
| 226 | // Move clock forward to just before the time. |
| 227 | clock.Add(9 * time.Second) |
| 228 | if atomic.LoadInt32(&ok) == 1 { |
| 229 | t.Fatal("too early") |
| 230 | } |
| 231 | |
| 232 | // Move clock forward to the after channel's time. |
| 233 | clock.Add(1 * time.Second) |
| 234 | if atomic.LoadInt32(&ok) == 0 { |
| 235 | t.Fatal("too late") |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Ensure that the mock's After channel doesn't block on write. |
| 240 | func TestMock_UnusedAfter(t *testing.T) { |
| 241 | mock := NewMock() |
| 242 | mock.After(1 * time.Millisecond) |
| 243 | |
| 244 | done := make(chan bool, 1) |
| 245 | go func() { |
| 246 | mock.Add(1 * time.Second) |
| 247 | done <- true |
| 248 | }() |
| 249 | |
| 250 | select { |
| 251 | case <-done: |
| 252 | case <-time.After(1 * time.Second): |
| 253 | t.Fatal("mock.Add hung") |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Ensure that the mock's AfterFunc executes at the correct time. |
| 258 | func TestMock_AfterFunc(t *testing.T) { |
| 259 | var ok int32 |
| 260 | clock := NewMock() |
| 261 | |
| 262 | // Execute function after duration. |
| 263 | clock.AfterFunc(10*time.Second, func() { |
| 264 | atomic.StoreInt32(&ok, 1) |
| 265 | }) |
| 266 | |
| 267 | // Move clock forward to just before the time. |
| 268 | clock.Add(9 * time.Second) |
| 269 | if atomic.LoadInt32(&ok) == 1 { |
| 270 | t.Fatal("too early") |
| 271 | } |
| 272 | |
| 273 | // Move clock forward to the after channel's time. |
| 274 | clock.Add(1 * time.Second) |
| 275 | if atomic.LoadInt32(&ok) == 0 { |
| 276 | t.Fatal("too late") |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Ensure that the mock's AfterFunc doesn't execute if stopped. |
| 281 | func TestMock_AfterFunc_Stop(t *testing.T) { |
| 282 | // Execute function after duration. |
| 283 | clock := NewMock() |
| 284 | timer := clock.AfterFunc(10*time.Second, func() { |
| 285 | t.Fatal("unexpected function execution") |
| 286 | }) |
| 287 | gosched() |
| 288 | |
| 289 | // Stop timer & move clock forward. |
| 290 | timer.Stop() |
| 291 | clock.Add(10 * time.Second) |
| 292 | gosched() |
| 293 | } |
| 294 | |
| 295 | // Ensure that the mock's current time can be changed. |
| 296 | func TestMock_Now(t *testing.T) { |
| 297 | clock := NewMock() |
| 298 | if now := clock.Now(); !now.Equal(time.Unix(0, 0)) { |
| 299 | t.Fatalf("expected epoch, got: %v", now) |
| 300 | } |
| 301 | |
| 302 | // Add 10 seconds and check the time. |
| 303 | clock.Add(10 * time.Second) |
| 304 | if now := clock.Now(); !now.Equal(time.Unix(10, 0)) { |
| 305 | t.Fatalf("expected epoch, got: %v", now) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestMock_Since(t *testing.T) { |
| 310 | clock := NewMock() |
| 311 | |
| 312 | beginning := clock.Now() |
| 313 | clock.Add(500 * time.Second) |
| 314 | if since := clock.Since(beginning); since.Seconds() != 500 { |
| 315 | t.Fatalf("expected 500 since beginning, actually: %v", since.Seconds()) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Ensure that the mock can sleep for the correct time. |
| 320 | func TestMock_Sleep(t *testing.T) { |
| 321 | var ok int32 |
| 322 | clock := NewMock() |
| 323 | |
| 324 | // Create a channel to execute after 10 mock seconds. |
| 325 | go func() { |
| 326 | clock.Sleep(10 * time.Second) |
| 327 | atomic.StoreInt32(&ok, 1) |
| 328 | }() |
| 329 | gosched() |
| 330 | |
| 331 | // Move clock forward to just before the sleep duration. |
| 332 | clock.Add(9 * time.Second) |
| 333 | if atomic.LoadInt32(&ok) == 1 { |
| 334 | t.Fatal("too early") |
| 335 | } |
| 336 | |
| 337 | // Move clock forward to the after the sleep duration. |
| 338 | clock.Add(1 * time.Second) |
| 339 | if atomic.LoadInt32(&ok) == 0 { |
| 340 | t.Fatal("too late") |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Ensure that the mock's Tick channel sends at the correct time. |
| 345 | func TestMock_Tick(t *testing.T) { |
| 346 | var n int32 |
| 347 | clock := NewMock() |
| 348 | |
| 349 | // Create a channel to increment every 10 seconds. |
| 350 | go func() { |
| 351 | tick := clock.Tick(10 * time.Second) |
| 352 | for { |
| 353 | <-tick |
| 354 | atomic.AddInt32(&n, 1) |
| 355 | } |
| 356 | }() |
| 357 | gosched() |
| 358 | |
| 359 | // Move clock forward to just before the first tick. |
| 360 | clock.Add(9 * time.Second) |
| 361 | if atomic.LoadInt32(&n) != 0 { |
| 362 | t.Fatalf("expected 0, got %d", n) |
| 363 | } |
| 364 | |
| 365 | // Move clock forward to the start of the first tick. |
| 366 | clock.Add(1 * time.Second) |
| 367 | if atomic.LoadInt32(&n) != 1 { |
| 368 | t.Fatalf("expected 1, got %d", n) |
| 369 | } |
| 370 | |
| 371 | // Move clock forward over several ticks. |
| 372 | clock.Add(30 * time.Second) |
| 373 | if atomic.LoadInt32(&n) != 4 { |
| 374 | t.Fatalf("expected 4, got %d", n) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Ensure that the mock's Ticker channel sends at the correct time. |
| 379 | func TestMock_Ticker(t *testing.T) { |
| 380 | var n int32 |
| 381 | clock := NewMock() |
| 382 | |
| 383 | // Create a channel to increment every microsecond. |
| 384 | go func() { |
| 385 | ticker := clock.Ticker(1 * time.Microsecond) |
| 386 | for { |
| 387 | <-ticker.C |
| 388 | atomic.AddInt32(&n, 1) |
| 389 | } |
| 390 | }() |
| 391 | gosched() |
| 392 | |
| 393 | // Move clock forward. |
| 394 | clock.Add(10 * time.Microsecond) |
| 395 | if atomic.LoadInt32(&n) != 10 { |
| 396 | t.Fatalf("unexpected: %d", n) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Ensure that the mock's Ticker channel won't block if not read from. |
| 401 | func TestMock_Ticker_Overflow(t *testing.T) { |
| 402 | clock := NewMock() |
| 403 | ticker := clock.Ticker(1 * time.Microsecond) |
| 404 | clock.Add(10 * time.Microsecond) |
| 405 | ticker.Stop() |
| 406 | } |
| 407 | |
| 408 | // Ensure that the mock's Ticker can be stopped. |
| 409 | func TestMock_Ticker_Stop(t *testing.T) { |
| 410 | var n int32 |
| 411 | clock := NewMock() |
| 412 | |
| 413 | // Create a channel to increment every second. |
| 414 | ticker := clock.Ticker(1 * time.Second) |
| 415 | go func() { |
| 416 | for { |
| 417 | <-ticker.C |
| 418 | atomic.AddInt32(&n, 1) |
| 419 | } |
| 420 | }() |
| 421 | gosched() |
| 422 | |
| 423 | // Move clock forward. |
| 424 | clock.Add(5 * time.Second) |
| 425 | if atomic.LoadInt32(&n) != 5 { |
| 426 | t.Fatalf("expected 5, got: %d", n) |
| 427 | } |
| 428 | |
| 429 | ticker.Stop() |
| 430 | |
| 431 | // Move clock forward again. |
| 432 | clock.Add(5 * time.Second) |
| 433 | if atomic.LoadInt32(&n) != 5 { |
| 434 | t.Fatalf("still expected 5, got: %d", n) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Ensure that multiple tickers can be used together. |
| 439 | func TestMock_Ticker_Multi(t *testing.T) { |
| 440 | var n int32 |
| 441 | clock := NewMock() |
| 442 | |
| 443 | go func() { |
| 444 | a := clock.Ticker(1 * time.Microsecond) |
| 445 | b := clock.Ticker(3 * time.Microsecond) |
| 446 | |
| 447 | for { |
| 448 | select { |
| 449 | case <-a.C: |
| 450 | atomic.AddInt32(&n, 1) |
| 451 | case <-b.C: |
| 452 | atomic.AddInt32(&n, 100) |
| 453 | } |
| 454 | } |
| 455 | }() |
| 456 | gosched() |
| 457 | |
| 458 | // Move clock forward. |
| 459 | clock.Add(10 * time.Microsecond) |
| 460 | gosched() |
| 461 | if atomic.LoadInt32(&n) != 310 { |
| 462 | t.Fatalf("unexpected: %d", n) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | func ExampleMock_After() { |
| 467 | // Create a new mock clock. |
| 468 | clock := NewMock() |
| 469 | count := 0 |
| 470 | |
| 471 | ready := make(chan struct{}) |
| 472 | // Create a channel to execute after 10 mock seconds. |
| 473 | go func() { |
| 474 | ch := clock.After(10 * time.Second) |
| 475 | close(ready) |
| 476 | <-ch |
| 477 | count = 100 |
| 478 | }() |
| 479 | <-ready |
| 480 | |
| 481 | // Print the starting value. |
| 482 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 483 | |
| 484 | // Move the clock forward 5 seconds and print the value again. |
| 485 | clock.Add(5 * time.Second) |
| 486 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 487 | |
| 488 | // Move the clock forward 5 seconds to the tick time and check the value. |
| 489 | clock.Add(5 * time.Second) |
| 490 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 491 | |
| 492 | // Output: |
| 493 | // 1970-01-01 00:00:00 +0000 UTC: 0 |
| 494 | // 1970-01-01 00:00:05 +0000 UTC: 0 |
| 495 | // 1970-01-01 00:00:10 +0000 UTC: 100 |
| 496 | } |
| 497 | |
| 498 | func ExampleMock_AfterFunc() { |
| 499 | // Create a new mock clock. |
| 500 | clock := NewMock() |
| 501 | count := 0 |
| 502 | |
| 503 | // Execute a function after 10 mock seconds. |
| 504 | clock.AfterFunc(10*time.Second, func() { |
| 505 | count = 100 |
| 506 | }) |
| 507 | gosched() |
| 508 | |
| 509 | // Print the starting value. |
| 510 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 511 | |
| 512 | // Move the clock forward 10 seconds and print the new value. |
| 513 | clock.Add(10 * time.Second) |
| 514 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 515 | |
| 516 | // Output: |
| 517 | // 1970-01-01 00:00:00 +0000 UTC: 0 |
| 518 | // 1970-01-01 00:00:10 +0000 UTC: 100 |
| 519 | } |
| 520 | |
| 521 | func ExampleMock_Sleep() { |
| 522 | // Create a new mock clock. |
| 523 | clock := NewMock() |
| 524 | count := 0 |
| 525 | |
| 526 | // Execute a function after 10 mock seconds. |
| 527 | go func() { |
| 528 | clock.Sleep(10 * time.Second) |
| 529 | count = 100 |
| 530 | }() |
| 531 | gosched() |
| 532 | |
| 533 | // Print the starting value. |
| 534 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 535 | |
| 536 | // Move the clock forward 10 seconds and print the new value. |
| 537 | clock.Add(10 * time.Second) |
| 538 | fmt.Printf("%s: %d\n", clock.Now().UTC(), count) |
| 539 | |
| 540 | // Output: |
| 541 | // 1970-01-01 00:00:00 +0000 UTC: 0 |
| 542 | // 1970-01-01 00:00:10 +0000 UTC: 100 |
| 543 | } |
| 544 | |
| 545 | func ExampleMock_Ticker() { |
| 546 | // Create a new mock clock. |
| 547 | clock := NewMock() |
| 548 | count := 0 |
| 549 | |
| 550 | ready := make(chan struct{}) |
| 551 | // Increment count every mock second. |
| 552 | go func() { |
| 553 | ticker := clock.Ticker(1 * time.Second) |
| 554 | close(ready) |
| 555 | for { |
| 556 | <-ticker.C |
| 557 | count++ |
| 558 | } |
| 559 | }() |
| 560 | <-ready |
| 561 | |
| 562 | // Move the clock forward 10 seconds and print the new value. |
| 563 | clock.Add(10 * time.Second) |
| 564 | fmt.Printf("Count is %d after 10 seconds\n", count) |
| 565 | |
| 566 | // Move the clock forward 5 more seconds and print the new value. |
| 567 | clock.Add(5 * time.Second) |
| 568 | fmt.Printf("Count is %d after 15 seconds\n", count) |
| 569 | |
| 570 | // Output: |
| 571 | // Count is 10 after 10 seconds |
| 572 | // Count is 15 after 15 seconds |
| 573 | } |
| 574 | |
| 575 | func ExampleMock_Timer() { |
| 576 | // Create a new mock clock. |
| 577 | clock := NewMock() |
| 578 | count := 0 |
| 579 | |
| 580 | ready := make(chan struct{}) |
| 581 | // Increment count after a mock second. |
| 582 | go func() { |
| 583 | timer := clock.Timer(1 * time.Second) |
| 584 | close(ready) |
| 585 | <-timer.C |
| 586 | count++ |
| 587 | }() |
| 588 | <-ready |
| 589 | |
| 590 | // Move the clock forward 10 seconds and print the new value. |
| 591 | clock.Add(10 * time.Second) |
| 592 | fmt.Printf("Count is %d after 10 seconds\n", count) |
| 593 | |
| 594 | // Output: |
| 595 | // Count is 1 after 10 seconds |
| 596 | } |
| 597 | |
| 598 | func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) } |
| 599 | func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) } |