From 5a8886af19932ff91132d374a866e57b23622595 Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Mon, 27 Jul 2026 16:24:46 +0300 Subject: [PATCH] p2p: fix flaky TestWithReceiveTimeout on QUIC The zero receive timeout makes the server close the stream almost immediately, racing the client's request write. On TCP the close is a graceful FIN, so the write always lands and the failure surfaces as an EOF on the response read. QUIC instead resets the stream, which can abort the write already in flight, failing with "write request: stream reset (remote)" before the read is ever reached. Accept either error on the QUIC iteration. The TCP iteration stays strict. category: test ticket: none Co-Authored-By: Claude Opus 5 (1M context) --- p2p/sender_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/p2p/sender_test.go b/p2p/sender_test.go index 3c0c07ee2..4e4450228 100644 --- a/p2p/sender_test.go +++ b/p2p/sender_test.go @@ -5,6 +5,8 @@ package p2p_test import ( "context" "math" + "slices" + "strings" "sync/atomic" "testing" "time" @@ -26,8 +28,17 @@ func TestWithReceiveTimeout(t *testing.T) { servers := []host.Host{testutil.CreateHost(t, testutil.AvailableAddr(t)), testutil.CreateQUICHost(t, testutil.AvailableUDPAddr(t))} clients := []host.Host{testutil.CreateHost(t, testutil.AvailableAddr(t)), testutil.CreateQUICHost(t, testutil.AvailableUDPAddr(t))} + // The zero receive timeout makes the server close the stream almost immediately, + // racing the client's request write. TCP closes gracefully, so the write always + // lands and the failure surfaces as an EOF on the response read. QUIC instead + // resets the stream, which can abort the write already in flight, so accept either. + expected := [][]string{ + {"read response: EOF"}, + {"read response: EOF", "stream reset"}, + } + for i := range len(servers) { - client, server := clients[i], servers[i] + client, server, expect := clients[i], servers[i], expected[i] client.Peerstore().AddAddrs(server.ID(), server.Addrs(), time.Hour) @@ -40,7 +51,9 @@ func TestWithReceiveTimeout(t *testing.T) { err := p2p.SendReceive(context.Background(), client, server.ID(), new(pbv1.Duty), new(pbv1.Duty), protocolID) require.Error(t, err) - require.ErrorContains(t, err, "read response: EOF") + require.True(t, slices.ContainsFunc(expect, func(s string) bool { + return strings.Contains(err.Error(), s) + }), "error %q contains none of %v", err, expect) } }