Skip to content

Commit d1dcb0e

Browse files
authored
test: document why InstantTimer is required (#2351)
originally explained by @MarcoPolo here: #2260 (comment)
1 parent efb90ae commit d1dcb0e

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

core/test/mockclock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ func NewMockClock() *MockClock {
5050
return &MockClock{now: time.Unix(0, 0), advanceBySem: make(chan struct{}, 1)}
5151
}
5252

53+
// InstantTimer implements a timer that triggers at a fixed instant in time as opposed to after a
54+
// fixed duration from the moment of creation/reset.
55+
//
56+
// In test environments, when using a Timer which fires after a duration, there is a race between
57+
// the goroutine moving time forward using `clock.Advanceby` and the goroutine resetting the
58+
// timer by doing `timer.Reset(desiredInstant.Sub(time.Now()))`. The value of
59+
// `desiredInstance.sub(time.Now())` is different depending on whether `clock.AdvanceBy` finishes
60+
// before or after the timer reset.
5361
func (c *MockClock) InstantTimer(when time.Time) *mockInstantTimer {
5462
c.mu.Lock()
5563
defer c.mu.Unlock()

0 commit comments

Comments
 (0)