mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
test(app): pace broadcast bursts to remove a race flake
This commit is contained in:
+14
-6
@@ -16,15 +16,23 @@ func TestWorkersDrainTheBroadcastChannel(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
a.startWorkers(ctx)
|
a.startWorkers(ctx)
|
||||||
|
|
||||||
const events = 1000
|
// Bursts stay under the 256-slot channel and wait for the loop to drain between them, so the
|
||||||
for i := 0; i < events; i++ {
|
// result depends on the loop running, not on how fast the scheduler runs it.
|
||||||
a.wsServer.BroadcastEvent("cache_hit", map[string]interface{}{"i": i})
|
const (
|
||||||
if i%200 == 0 {
|
bursts = 10
|
||||||
time.Sleep(5 * time.Millisecond)
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -251,6 +251,11 @@ func (s *Server) Dropped() uint64 {
|
|||||||
return s.dropped.Load()
|
return s.dropped.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pending returns how many events wait in the broadcast channel.
|
||||||
|
func (s *Server) Pending() int {
|
||||||
|
return len(s.broadcast)
|
||||||
|
}
|
||||||
|
|
||||||
// BroadcastEvent satisfies the events.Broadcaster contract used by
|
// BroadcastEvent satisfies the events.Broadcaster contract used by
|
||||||
// cache/scanner sub-systems. It accepts a string event type and an
|
// cache/scanner sub-systems. It accepts a string event type and an
|
||||||
// arbitrary payload (typically map[string]any) and forwards to the
|
// arbitrary payload (typically map[string]any) and forwards to the
|
||||||
|
|||||||
Reference in New Issue
Block a user