test(app): pace broadcast bursts to remove a race flake

This commit is contained in:
2026-11-23 14:31:49 +00:00
parent fe2d9f8b3a
commit 4d31e535ff
2 changed files with 19 additions and 6 deletions
+14 -6
View File
@@ -16,15 +16,23 @@ func TestWorkersDrainTheBroadcastChannel(t *testing.T) {
defer cancel()
a.startWorkers(ctx)
const events = 1000
for i := 0; i < events; i++ {
a.wsServer.BroadcastEvent("cache_hit", map[string]interface{}{"i": i})
if i%200 == 0 {
time.Sleep(5 * time.Millisecond)
// Bursts stay under the 256-slot channel and wait for the loop to drain between them, so the
// result depends on the loop running, not on how fast the scheduler runs it.
const (
bursts = 10
burstLen = 100
)
for b := 0; b < bursts; b++ {
for i := 0; i < burstLen; i++ {
a.wsServer.BroadcastEvent("cache_hit", map[string]interface{}{"i": i})
}
deadline := time.Now().Add(10 * time.Second)
for a.wsServer.Pending() > 0 && time.Now().Before(deadline) {
time.Sleep(time.Millisecond)
}
}
if dropped := a.wsServer.Dropped(); dropped != 0 {
t.Fatalf("dropped %d of %d events; the broadcast loop is not draining the channel", dropped, events)
t.Fatalf("dropped %d of %d events; the broadcast loop is not draining the channel", dropped, bursts*burstLen)
}
}