Files
coder/coderd
Kyle Carberry 69917b4516 fix: resolve flaky TestWatchChats/DiffStatusChangeIncludesDiffStatus (#24298)
## Problem

`TestWatchChats/DiffStatusChangeIncludesDiffStatus` flakes with:
```
failed to read JSON message: failed to get reader: context deadline exceeded
```

Tracker: https://github.com/coder/internal/issues/1467

## Root Cause

The test published a pubsub event **once**, immediately after
`client.Dial()` returned. However, `Dial` completes as soon as the HTTP
upgrade finishes — before the server-side `watchChats` handler has
called `SubscribeWithErr` on the pubsub channel. When the publish races
ahead of the subscription, the message is silently dropped and the
`wsjson.Read` loop blocks until the context deadline.

## Fix

Publish the event on a short ticker (`testutil.IntervalFast` = 25ms) in
a background goroutine instead of publishing once. This guarantees that
at least one publish lands **after** the subscription is active,
regardless of goroutine scheduling. The goroutine is cleanly stopped via
a `publishDone` channel once the expected event is received.

## Verification

- `go test -race -count=50 -run
TestWatchChats/DiffStatusChangeIncludesDiffStatus` → **50/50 PASS, 0
races**
- `go test -race -count=5 -run TestWatchChats` → **all 5 subtests pass
5/5 times**
2026-04-13 10:20:57 -04:00
..