Skip to content

Commit 6c1afb8

Browse files
authored
fix: flaky TestCancelOverridesPendingWants (#1016)
added 10ms delay before AddCancels to fix race condition. after collectMessages returns, the queue's goroutine might still be marking items as "sent". without this delay, AddCancels might not see the CIDs as sent and won't create cancel messages, causing test timeout. fixes CI failures with -race flag.
1 parent 30868de commit 6c1afb8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

bitswap/client/internal/messagequeue/messagequeue_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,27 @@ func TestCancelOverridesPendingWants(t *testing.T) {
356356
}
357357

358358
// Cancel the remaining want-blocks and want-haves
359-
cancels = append(wantHaves, wantBlocks...)
359+
// These CIDs were sent in the first message, so canceling them should
360+
// generate cancel messages
361+
362+
// Small delay to avoid race condition: after collectMessages returns,
363+
// the message has been sent to the channel, but the queue's goroutine
364+
// might still be marking items as "sent". Without this delay, AddCancels
365+
// might not find the CIDs in the sent list and won't generate cancel messages.
366+
time.Sleep(10 * time.Millisecond)
367+
368+
cancels = []cid.Cid{wantBlocks[1], wantHaves[1]}
360369
messageQueue.AddCancels(cancels)
361370
messages = collectMessages(ctx, t, messagesSent, collectTimeout)
362371

363372
// The remaining 2 cancels should be sent to the network as they are for
364373
// wants that were sent to the network
374+
if len(messages) == 0 {
375+
t.Fatal("Expected cancel messages but got none")
376+
}
365377
_, _, cl = filterWantTypes(messages[0])
366378
if len(cl) != 2 {
367-
t.Fatal("Expected 2 cancels")
379+
t.Fatalf("Expected 2 cancels, got %d", len(cl))
368380
}
369381
}
370382

0 commit comments

Comments
 (0)