github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/p2p/listener_test.go (about) 1 // +build !network 2 3 package p2p 4 5 import ( 6 "bytes" 7 "testing" 8 ) 9 10 func TestListener(t *testing.T) { 11 // Create a listener 12 l, _ := NewDefaultListener("tcp", "localhost:8001", true) 13 14 // Dial the listener 15 lAddr := l.InternalAddress() 16 connOut, err := lAddr.Dial() 17 if err != nil { 18 t.Fatalf("Could not connect to listener address %v", lAddr) 19 } 20 21 connIn, ok := <-l.Connections() 22 if !ok { 23 t.Fatalf("Could not get inbound connection from listener") 24 } 25 26 msg := []byte("hi!") 27 go connIn.Write(msg) 28 b := make([]byte, 32) 29 n, err := connOut.Read(b) 30 if err != nil { 31 t.Fatalf("Error reading off connection: %v", err) 32 } 33 34 b = b[:n] 35 if !bytes.Equal(msg, b) { 36 t.Fatalf("Got %s, expected %s", b, msg) 37 } 38 39 // Close the server, no longer needed. 40 l.Stop() 41 }