test(channelserver): replace time.Sleep with polling loops

Blind sleeps accumulate serially (no t.Parallel anywhere) and inflate
under the race detector's scheduling overhead — contributing to the
~136s channelserver test run time.

Replace ~75 arbitrary sleeps (50ms–1s) across 7 test files with 2s
polling loops that exit as soon as the expected condition holds. Sleeps
that are genuinely intentional (race-condition stress tests, cache
expiry, temporal spacing in timestamp tests, backpressure pacing) are
left untouched.
This commit is contained in:
Houmgaor
2026-03-23 10:57:01 +01:00
parent 0c6dc39371
commit d0efc4e81a
8 changed files with 159 additions and 75 deletions

View File

@@ -116,11 +116,23 @@ func TestPacketQueueIndividualSending(t *testing.T) {
}
// Wait for packets to be processed
time.Sleep(100 * time.Millisecond)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= tt.wantPackets {
break
}
time.Sleep(10 * time.Millisecond)
}
// Stop the session
s.closed.Store(true)
time.Sleep(50 * time.Millisecond)
deadline = time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= tt.wantPackets {
break
}
time.Sleep(10 * time.Millisecond)
}
// Verify packet count
sentPackets := mock.GetSentPackets()
@@ -165,9 +177,21 @@ func TestPacketQueueNoConcatenation(t *testing.T) {
s.sendPackets <- packet{packet2, true}
s.sendPackets <- packet{packet3, true}
time.Sleep(100 * time.Millisecond)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 3 {
break
}
time.Sleep(10 * time.Millisecond)
}
s.closed.Store(true)
time.Sleep(50 * time.Millisecond)
deadline = time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 3 {
break
}
time.Sleep(10 * time.Millisecond)
}
sentPackets := mock.GetSentPackets()
@@ -218,7 +242,13 @@ func TestQueueSendUsesQueue(t *testing.T) {
// Now start sendLoop and verify it gets sent
go s.sendLoop()
time.Sleep(100 * time.Millisecond)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 1 {
break
}
time.Sleep(10 * time.Millisecond)
}
if mock.PacketCount() != 1 {
t.Errorf("expected 1 packet sent after sendLoop, got %d", mock.PacketCount())
@@ -237,9 +267,21 @@ func TestPacketTerminatorFormat(t *testing.T) {
testData := []byte{0x00, 0x01, 0xAA, 0xBB}
s.sendPackets <- packet{testData, true}
time.Sleep(100 * time.Millisecond)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 1 {
break
}
time.Sleep(10 * time.Millisecond)
}
s.closed.Store(true)
time.Sleep(50 * time.Millisecond)
deadline = time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 1 {
break
}
time.Sleep(10 * time.Millisecond)
}
sentPackets := mock.GetSentPackets()
if len(sentPackets) != 1 {
@@ -313,9 +355,21 @@ func TestPacketQueueAckFormat(t *testing.T) {
ackData := []byte{0xAA, 0xBB, 0xCC, 0xDD}
s.QueueAck(ackHandle, ackData)
time.Sleep(100 * time.Millisecond)
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 1 {
break
}
time.Sleep(10 * time.Millisecond)
}
s.closed.Store(true)
time.Sleep(50 * time.Millisecond)
deadline = time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if mock.PacketCount() >= 1 {
break
}
time.Sleep(10 * time.Millisecond)
}
sentPackets := mock.GetSentPackets()
if len(sentPackets) != 1 {