Skip to content

Commit b71761c

Browse files
authored
fix: stop logging after test cleanup (#19)
Fixes Quartz so we don't accidentally log after the test ends, which [can cause racy test failures](https://app.graphite.dev/github/pr/coder/coder/18007/chore-updated-to-coder%2Fquartz-v0.2.0#discussion-IC_kwDOGkVX1s6tQd_u)
1 parent 09f5542 commit b71761c

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

mock.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
// Mock is the testing implementation of Clock. It tracks a time that monotonically increases
1414
// during a test, triggering any timers or tickers automatically.
1515
type Mock struct {
16-
tb testing.TB
17-
mu sync.Mutex
16+
tb testing.TB
17+
mu sync.Mutex
18+
testOver bool
1819

1920
// cur is the current time
2021
cur time.Time
@@ -197,7 +198,9 @@ func (m *Mock) matchCallLocked(c *apiCall) {
197198
traps = append(traps, t)
198199
}
199200
}
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+
}
201204
if len(traps) == 0 {
202205
return
203206
}
@@ -261,7 +264,9 @@ func (m *Mock) Advance(d time.Duration) AdvanceWaiter {
261264
m.tb.Helper()
262265
w := AdvanceWaiter{tb: m.tb, ch: make(chan struct{})}
263266
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+
}
265270
fin := m.cur.Add(d)
266271
// nextTime.IsZero implies no events scheduled.
267272
if m.nextTime.IsZero() || fin.Before(m.nextTime) {
@@ -309,7 +314,9 @@ func (m *Mock) Set(t time.Time) AdvanceWaiter {
309314
m.tb.Helper()
310315
w := AdvanceWaiter{tb: m.tb, ch: make(chan struct{})}
311316
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+
}
313320
if t.Before(m.cur) {
314321
defer close(w.ch)
315322
defer m.mu.Unlock()
@@ -346,7 +353,9 @@ func (m *Mock) Set(t time.Time) AdvanceWaiter {
346353
// wait for the timer/tick event(s) to finish.
347354
func (m *Mock) AdvanceNext() (time.Duration, AdvanceWaiter) {
348355
m.mu.Lock()
349-
m.tb.Logf("Mock Clock - AdvanceNext()")
356+
if !m.testOver {
357+
m.tb.Logf("Mock Clock - AdvanceNext()")
358+
}
350359
m.tb.Helper()
351360
w := AdvanceWaiter{tb: m.tb, ch: make(chan struct{})}
352361
if m.nextTime.IsZero() {
@@ -435,7 +444,9 @@ func (m *Mock) Trap() Trapper {
435444
func (m *Mock) newTrap(fn clockFunction, tags []string) *Trap {
436445
m.mu.Lock()
437446
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+
}
439450
tr := &Trap{
440451
fn: fn,
441452
tags: tags,
@@ -455,10 +466,17 @@ func NewMock(tb testing.TB) *Mock {
455466
if err != nil {
456467
panic(err)
457468
}
458-
return &Mock{
469+
m := &Mock{
459470
tb: tb,
460471
cur: cur,
461472
}
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
462480
}
463481

464482
var _ Clock = &Mock{}

0 commit comments

Comments
 (0)