From 77e7d82d5a47da4b5fcd06a9cd9bfeb16def28d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Tue, 11 Aug 2026 17:59:48 +0200 Subject: [PATCH] [client] Keep the peer endpoint open in the sweep test connPair closed the accepted connection right after the handshake, so the reads in TestSweepClosesRegisteredConns failed on the peer's own close rather than on the sweep. The test passed even with Sweep's close loop removed. Hold the peer until cleanup so the read errors come from Sweep. --- client/netsweep/netsweep_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client/netsweep/netsweep_test.go b/client/netsweep/netsweep_test.go index 5c631d15b..dd26536e1 100644 --- a/client/netsweep/netsweep_test.go +++ b/client/netsweep/netsweep_test.go @@ -93,7 +93,9 @@ func TestNilSweeperIsNoop(t *testing.T) { assert.NoError(t, ctx.Err(), "nil sweeper must not cancel the dial context") } -// connPair dials a loopback TCP connection against a throwaway listener. +// connPair dials a loopback TCP connection and keeps the accepted peer open +// until the test ends: a peer that closed early would make the connection +// unreadable on its own, so a read error after the sweep would prove nothing. func connPair(t *testing.T) net.Conn { t.Helper() @@ -105,15 +107,26 @@ func connPair(t *testing.T) net.Conn { } }) + accepted := make(chan net.Conn, 1) go func() { conn, err := l.Accept() if err != nil { + close(accepted) return } - _ = conn.Close() + accepted <- conn }() conn, err := net.Dial("tcp", l.Addr().String()) require.NoError(t, err) + + peer, ok := <-accepted + require.True(t, ok, "listener must accept the dialed connection") + t.Cleanup(func() { + if err := peer.Close(); err != nil { + t.Logf("peer close error: %v", err) + } + }) + return conn }