-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer_test.go
193 lines (162 loc) · 4.63 KB
/
peer_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package p2p
import (
"bytes"
"encoding/gob"
"fmt"
"net"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
func testRandomPeer(net *Network) *Peer {
p := new(Peer)
p.net = net
p.Endpoint = testRandomEndpoint()
p.stop = make(chan bool, 1)
p.Hash = fmt.Sprintf("%s:%s %08x", p.Endpoint.IP, p.Endpoint.Port, p.net.rng.Uint64())
p.logger = peerLogger.WithFields(log.Fields{
"hash": p.Hash,
"address": p.Endpoint.IP,
"Port": p.Endpoint.Port,
"node": p.net.conf.NodeName,
})
p.send = newParcelChannel(p.net.conf.ChannelCapacity)
p.IsIncoming = net.rng.Intn(1) == 0
p.connected = time.Now()
if net.conf.PeerResendFilter {
p.resend = NewPeerHashCache(net.conf.PeerResendBuckets, net.conf.PeerResendInterval)
}
return p
}
func (p *Peer) _setEndpoint(ep Endpoint) {
p.Endpoint = ep
p.Hash = fmt.Sprintf("%s:%s %08x", p.Endpoint.IP, p.Endpoint.Port, p.net.rng.Uint64())
p.logger = peerLogger.WithFields(log.Fields{
"hash": p.Hash,
"address": p.Endpoint.IP,
"Port": p.Endpoint.Port,
"node": p.net.conf.NodeName,
})
}
func (p *Peer) _setProtocol(prot uint16, conn net.Conn) {
p.metrics = NewMetricsReadWriter(conn)
switch prot {
case 9:
p.prot = newProtocolV9(p.net.conf.Network, p.net.conf.NodeID, p.net.conf.ListenPort, gob.NewDecoder(p.metrics), gob.NewEncoder(p.metrics))
case 10:
p.prot = newProtocolV10(gob.NewDecoder(p.metrics), gob.NewEncoder(p.metrics))
case 11:
p.prot = newProtocolV11(p.metrics)
}
}
func TestPeer_sendLoop(t *testing.T) {
A, B := net.Pipe()
p := new(Peer)
p.net = testNetworkHarness(t)
p.logger = packageLogger
p.conn = A
p.prot = newProtocolV10(gob.NewDecoder(A), gob.NewEncoder(A))
start := time.Now()
p.lastSend = start
done := make(chan bool, 2)
p.stop = make(chan bool, 1)
p.send = newParcelChannel(128)
go func() {
p.sendLoop()
done <- true
}()
p.Send(nil)
parcels := make([]*Parcel, 128)
for i := range parcels {
parcels[i] = testRandomParcel()
added, dropped := p.send.Send(parcels[i])
if !added || dropped > 0 {
t.Errorf("failed to add parcel %d. added = %v, dropped = %d", i, added, dropped)
}
}
time.Sleep(time.Millisecond * 50)
if p.LastSendAge() > time.Since(start) {
t.Errorf("peer.lastSend did not update")
}
go func() {
dec := gob.NewDecoder(B)
for i := 0; i < len(parcels); i++ {
v10msg := new(V10Msg)
dec.Decode(v10msg)
if v10msg.Type != parcels[i].ptype || !bytes.Equal(v10msg.Payload, parcels[i].Payload) {
t.Errorf("parcel %d didn't arrive the same. got = (%s, %x), want = (%s, %x)", i, v10msg.Type, v10msg.Payload, parcels[i].ptype, parcels[i].Payload)
}
}
p.Stop()
}()
<-done
update := <-p.net.controller.peerStatus
if update.peer != p {
t.Errorf("peerStatus: received unknown peer %+v", update.peer)
}
if update.online {
t.Errorf("peerStatus: received online signal")
}
}
func TestPeer_readLoop(t *testing.T) {
A, B := net.Pipe()
p := new(Peer)
p.Hash = "unit test peer"
p.conn = A
p.net = testNetworkHarness(t)
p.logger = packageLogger
p.stop = make(chan bool, 1)
p.prot = newProtocolV10(gob.NewDecoder(A), gob.NewEncoder(A))
start := time.Now()
p.lastReceive = start
parcels := make([]*Parcel, p.net.conf.ChannelCapacity*2)
for i := range parcels {
parcels[i] = testRandomParcel()
}
done := make(chan bool, 1)
stopped := make(chan bool, 1)
// check delivery
go func() {
for i := 0; i < len(parcels); i++ {
pp := <-p.net.controller.peerData
if pp.peer.Hash != p.Hash {
t.Errorf("peerData: parcel %d received has wrong peer. got = %s, want = %s", i, pp.peer.Hash, p.Hash)
}
if pp.parcel.ptype != parcels[i].ptype || !bytes.Equal(pp.parcel.Payload, parcels[i].Payload) {
t.Errorf("peerData: parcel %d differs. got = (%s, %x), want = (%s, %x)", i, pp.parcel.ptype, pp.parcel.Payload, parcels[i].ptype, parcels[i].Payload)
}
}
done <- true
}()
// run readloop
go func() {
p.readLoop()
stopped <- true
}()
// send messages
go func() {
enc := gob.NewEncoder(B)
for _, p := range parcels {
msg := new(V10Msg)
msg.Type = p.ptype
msg.Payload = p.Payload
B.SetWriteDeadline(time.Now().Add(time.Millisecond * 50))
if err := enc.Encode(msg); err != nil {
t.Errorf("error encoding: %v", err)
}
}
}()
<-done // all parcels were sent to the controller
p.Stop()
<-stopped // the readloop exited successfully
if p.lastReceive.Equal(start) {
t.Errorf("peer.lastReceive did not update. lastReceive = %s, start = %s", p.lastReceive, start)
}
update := <-p.net.controller.peerStatus
if update.peer != p {
t.Errorf("peerStatus: received unknown peer %+v", update.peer)
}
if update.online {
t.Errorf("peerStatus: received online signal")
}
}