From 7f0ef31600684e16f8b5d9f28b729c734f5f3fca Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Mon, 31 Aug 2026 16:35:07 +0800 Subject: [PATCH] test(p2p/simulations/adapters): fix short read in TestTCPPipe Replace net.Conn.Read with io.ReadFull in TestTCPPipe and TestTCPPipeBidirections. A single TCP Read is not guaranteed to fill the buffer, so on loopback stacks that fragment the 1024-byte writes (e.g. WSL2) the test compared a partial read against the full message and failed at the same byte boundary on every run. TestNetPipe is unaffected because net.Pipe delivers each Write as one Read. --- p2p/simulations/adapters/inproc_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/p2p/simulations/adapters/inproc_test.go b/p2p/simulations/adapters/inproc_test.go index 0a3284f4778c..a4e278a472bb 100644 --- a/p2p/simulations/adapters/inproc_test.go +++ b/p2p/simulations/adapters/inproc_test.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/binary" "fmt" + "io" "sync" "testing" @@ -46,7 +47,9 @@ func TestTCPPipe(t *testing.T) { msg := make([]byte, size) binary.PutUvarint(msg, uint64(i)) out := make([]byte, size) - if _, err := c2.Read(out); err != nil { + // A single Read on a TCP connection is not guaranteed to fill the + // buffer, so read the whole message before comparing it. + if _, err := io.ReadFull(c2, out); err != nil { t.Fatal(err) } if !bytes.Equal(msg, out) { @@ -73,7 +76,9 @@ func TestTCPPipeBidirections(t *testing.T) { for i := 0; i < msgs; i++ { expected := []byte(fmt.Sprintf("ping %02d", i)) out := make([]byte, size) - if _, err := c2.Read(out); err != nil { + // A single Read on a TCP connection is not guaranteed to fill the + // buffer, so read the whole message before comparing it. + if _, err := io.ReadFull(c2, out); err != nil { t.Fatal(err) } @@ -90,7 +95,9 @@ func TestTCPPipeBidirections(t *testing.T) { for i := 0; i < msgs; i++ { expected := []byte(fmt.Sprintf("pong %02d", i)) out := make([]byte, size) - if _, err := c1.Read(out); err != nil { + // A single Read on a TCP connection is not guaranteed to fill the + // buffer, so read the whole message before comparing it. + if _, err := io.ReadFull(c1, out); err != nil { t.Fatal(err) } if !bytes.Equal(expected, out) {