@@ -13,8 +13,9 @@ import (
13
13
// Mock is the testing implementation of Clock. It tracks a time that monotonically increases
14
14
// during a test, triggering any timers or tickers automatically.
15
15
type Mock struct {
16
- tb testing.TB
17
- mu sync.Mutex
16
+ tb testing.TB
17
+ mu sync.Mutex
18
+ testOver bool
18
19
19
20
// cur is the current time
20
21
cur time.Time
@@ -197,7 +198,9 @@ func (m *Mock) matchCallLocked(c *apiCall) {
197
198
traps = append (traps , t )
198
199
}
199
200
}
200
- m .tb .Logf ("Mock Clock - %s call, matched %d traps" , c , len (traps ))
201
+ if ! m .testOver {
202
+ m .tb .Logf ("Mock Clock - %s call, matched %d traps" , c , len (traps ))
203
+ }
201
204
if len (traps ) == 0 {
202
205
return
203
206
}
@@ -261,7 +264,9 @@ func (m *Mock) Advance(d time.Duration) AdvanceWaiter {
261
264
m .tb .Helper ()
262
265
w := AdvanceWaiter {tb : m .tb , ch : make (chan struct {})}
263
266
m .mu .Lock ()
264
- m .tb .Logf ("Mock Clock - Advance(%s)" , d )
267
+ if ! m .testOver {
268
+ m .tb .Logf ("Mock Clock - Advance(%s)" , d )
269
+ }
265
270
fin := m .cur .Add (d )
266
271
// nextTime.IsZero implies no events scheduled.
267
272
if m .nextTime .IsZero () || fin .Before (m .nextTime ) {
@@ -309,7 +314,9 @@ func (m *Mock) Set(t time.Time) AdvanceWaiter {
309
314
m .tb .Helper ()
310
315
w := AdvanceWaiter {tb : m .tb , ch : make (chan struct {})}
311
316
m .mu .Lock ()
312
- m .tb .Logf ("Mock Clock - Set(%s)" , t )
317
+ if ! m .testOver {
318
+ m .tb .Logf ("Mock Clock - Set(%s)" , t )
319
+ }
313
320
if t .Before (m .cur ) {
314
321
defer close (w .ch )
315
322
defer m .mu .Unlock ()
@@ -346,7 +353,9 @@ func (m *Mock) Set(t time.Time) AdvanceWaiter {
346
353
// wait for the timer/tick event(s) to finish.
347
354
func (m * Mock ) AdvanceNext () (time.Duration , AdvanceWaiter ) {
348
355
m .mu .Lock ()
349
- m .tb .Logf ("Mock Clock - AdvanceNext()" )
356
+ if ! m .testOver {
357
+ m .tb .Logf ("Mock Clock - AdvanceNext()" )
358
+ }
350
359
m .tb .Helper ()
351
360
w := AdvanceWaiter {tb : m .tb , ch : make (chan struct {})}
352
361
if m .nextTime .IsZero () {
@@ -435,7 +444,9 @@ func (m *Mock) Trap() Trapper {
435
444
func (m * Mock ) newTrap (fn clockFunction , tags []string ) * Trap {
436
445
m .mu .Lock ()
437
446
defer m .mu .Unlock ()
438
- m .tb .Logf ("Mock Clock - Trap %s(..., %v)" , fn , tags )
447
+ if ! m .testOver {
448
+ m .tb .Logf ("Mock Clock - Trap %s(..., %v)" , fn , tags )
449
+ }
439
450
tr := & Trap {
440
451
fn : fn ,
441
452
tags : tags ,
@@ -455,10 +466,17 @@ func NewMock(tb testing.TB) *Mock {
455
466
if err != nil {
456
467
panic (err )
457
468
}
458
- return & Mock {
469
+ m := & Mock {
459
470
tb : tb ,
460
471
cur : cur ,
461
472
}
473
+ tb .Cleanup (func () {
474
+ m .mu .Lock ()
475
+ defer m .mu .Unlock ()
476
+ m .testOver = true
477
+ tb .Logf ("Mock Clock - test cleanup; will no longer log clock events" )
478
+ })
479
+ return m
462
480
}
463
481
464
482
var _ Clock = & Mock {}
0 commit comments