forked from libp2p/go-libp2p-gostream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostream_test.go
134 lines (111 loc) · 2.8 KB
/
gostream_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gostream
import (
"bufio"
"context"
"testing"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
multiaddr "github.com/multiformats/go-multiaddr"
)
// newHost illustrates how to build a libp2p host with secio using
// a randomly generated key-pair
func newHost(t *testing.T, listen multiaddr.Multiaddr) host.Host {
h, err := libp2p.New(
context.Background(),
libp2p.ListenAddrs(listen),
)
if err != nil {
t.Fatal(err)
}
return h
}
func TestServerClient(t *testing.T) {
m1, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10000")
m2, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10001")
srvHost := newHost(t, m1)
clientHost := newHost(t, m2)
defer srvHost.Close()
defer clientHost.Close()
srvHost.Peerstore().AddAddrs(clientHost.ID(), clientHost.Addrs(), peerstore.PermanentAddrTTL)
clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL)
var tag protocol.ID = "/testitytest"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context) {
listener, err := Listen(srvHost, tag)
if err != nil {
t.Fatal(err)
}
defer listener.Close()
if listener.Addr().String() != srvHost.ID().Pretty() {
t.Fatal("bad listener address")
}
servConn, err := listener.Accept()
if err != nil {
t.Fatal(err)
}
defer servConn.Close()
reader := bufio.NewReader(servConn)
for {
msg, err := reader.ReadString('\n')
if err != nil {
t.Fatal(err)
}
if string(msg) != "is libp2p awesome?\n" {
t.Fatalf("Bad incoming message: %s", msg)
}
_, err = servConn.Write([]byte("yes it is\n"))
if err != nil {
t.Fatal(err)
}
select {
case <-ctx.Done():
return
}
}
}(ctx)
clientConn, err := Dial(clientHost, srvHost.ID(), tag)
if err != nil {
t.Fatal(err)
}
if clientConn.LocalAddr().String() != clientHost.ID().Pretty() {
t.Fatal("Bad LocalAddr")
}
if clientConn.RemoteAddr().String() != srvHost.ID().Pretty() {
t.Fatal("Bad RemoteAddr")
}
if clientConn.LocalAddr().Network() != Network {
t.Fatal("Bad Network()")
}
err = clientConn.SetDeadline(time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
}
err = clientConn.SetReadDeadline(time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
}
err = clientConn.SetWriteDeadline(time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
}
_, err = clientConn.Write([]byte("is libp2p awesome?\n"))
if err != nil {
t.Fatal(err)
}
reader := bufio.NewReader(clientConn)
resp, err := reader.ReadString('\n')
if err != nil {
t.Fatal(err)
}
if string(resp) != "yes it is\n" {
t.Errorf("Bad response: %s", resp)
}
err = clientConn.Close()
if err != nil {
t.Fatal(err)
}
}