From 76cb414e623872101d9e29813826f24b2f4aeb7d Mon Sep 17 00:00:00 2001 From: htiennv Date: Tue, 12 Nov 2024 16:48:35 +0700 Subject: [PATCH] p2p/pipes: add test for pipe --- p2p/pipes/pipe_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 p2p/pipes/pipe_test.go diff --git a/p2p/pipes/pipe_test.go b/p2p/pipes/pipe_test.go new file mode 100644 index 0000000000..44d2067e6c --- /dev/null +++ b/p2p/pipes/pipe_test.go @@ -0,0 +1,40 @@ +package pipes_test + +import ( + "io" + "testing" + "time" + + "github.com/ethereum/go-ethereum/p2p/pipes" +) + +func TestTCPPipe(t *testing.T) { + conn1, conn2, err := pipes.TCPPipe() + if err != nil { + t.Fatalf("Failed to create TCPPipe: %v", err) + } + defer conn1.Close() + defer conn2.Close() + + // Set deadlines to prevent hanging tests. + conn1.SetDeadline(time.Now().Add(time.Second)) + conn2.SetDeadline(time.Now().Add(time.Second)) + + testMessage := "Hello!" + + // Write from one connection and read from the other + go func() { + if _, err := conn1.Write([]byte(testMessage)); err != nil { + t.Errorf("Failed to write to conn1: %v", err) + } + }() + + buf := make([]byte, len(testMessage)) + if _, err := io.ReadFull(conn2, buf); err != nil { + t.Fatalf("Failed to read from conn2: %v", err) + } + + if string(buf) != testMessage { + t.Errorf("Data mismatch: got %q, want %q", buf, testMessage) + } +}