test(broadcast): fix flaky TestBroadcastMHFAllSessions under race detector

The fixed 100ms sleep was too short for sendLoop goroutines to drain
under the race detector's scheduling overhead, causing intermittent
count=4/want=5 failures. Replace with a 2s polling loop that exits
as soon as all sessions report delivery.
This commit is contained in:
Houmgaor
2026-03-23 10:26:29 +01:00
parent 1f0544fd10
commit 635b9890c8

View File

@@ -278,13 +278,26 @@ func TestBroadcastMHFAllSessions(t *testing.T) {
testPkt := &mhfpacket.MsgSysNop{}
server.BroadcastMHF(testPkt, nil)
time.Sleep(100 * time.Millisecond)
// Poll until all sessions have received the packet or the deadline is reached.
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
receivedCount := 0
for _, sess := range server.sessions {
mock := sess.cryptConn.(*MockCryptConn)
if mock.PacketCount() > 0 {
receivedCount++
}
}
if receivedCount == sessionCount {
break
}
time.Sleep(10 * time.Millisecond)
}
// Stop all sessions
for _, sess := range sessions {
sess.closed.Store(true)
}
time.Sleep(50 * time.Millisecond)
// Verify all sessions received the packet
receivedCount := 0