OSDN Git Service

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