mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
p2p/pipes: add test for pipe
This commit is contained in:
parent
df182a742c
commit
76cb414e62
1 changed files with 40 additions and 0 deletions
40
p2p/pipes/pipe_test.go
Normal file
40
p2p/pipes/pipe_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue