Skip to content

Commit 213ae69

Browse files
authored
fix: start timer before subscribing to avoid test race (coder#12031)
Fixes coder#12030 This is a good example of the kind of thing I'd like to address with a time-testing lib. The problem is that there is a race between the watchdog starting it's timer and the test incrementing the time. What would make this easier is if the time-testing library could wait for and assert the call to start the timer before incrementing the time.
1 parent b6806bc commit 213ae69

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

coderd/database/pubsub/watchdog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ func (w *Watchdog) publishLoop() {
103103

104104
func (w *Watchdog) subscribeMonitor() {
105105
defer w.wg.Done()
106+
tmr := w.clock.Timer(periodTimeout)
107+
defer tmr.Stop()
106108
beats := make(chan struct{})
107109
unsub, err := w.ps.Subscribe(EventPubsubWatchdog, func(context.Context, []byte) {
108110
w.logger.Debug(w.ctx, "got heartbeat for pubsub watchdog")
@@ -117,8 +119,6 @@ func (w *Watchdog) subscribeMonitor() {
117119
return
118120
}
119121
defer unsub()
120-
tmr := w.clock.Timer(periodTimeout)
121-
defer tmr.Stop()
122122
for {
123123
select {
124124
case <-w.ctx.Done():

0 commit comments

Comments
 (0)