-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragment_test.go
276 lines (214 loc) · 8.76 KB
/
fragment_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package rpc25519
import (
"context"
//"fmt"
//"os"
//"strings"
"testing"
"time"
cv "github.com/glycerine/goconvey/convey"
)
func Test400_Fragments_riding_Circuits_API(t *testing.T) {
cv.Convey("our peer-to-peer Fragment/Circuit API "+
"generalizes peer-to-multiple-peers and dealing with infinite "+
"streams of Fragments, both arriving and being sent.", t, func() {
cfg := NewConfig()
cfg.TCPonly_no_TLS = true
cfg.ServerAddr = "127.0.0.1:0"
srv := NewServer("srv_test400", cfg)
serverAddr, err := srv.Start()
panicOn(err)
defer srv.Close()
cfg.ClientDialToHostPort = serverAddr.String()
cli, err := NewClient("client_test400", cfg)
panicOn(err)
err = cli.Start()
panicOn(err)
defer cli.Close()
// An unexpected race we have to prevent:
// the client TCP connection would get made,
// but the subsequent server side Go code that
// gets the listener accept and sets up the rwPair
// was racing with us coming from the other direction
// and telling the server to contact the client.
// We saw this race in 6% of 1000 runs, but
// by running this next round trip, we see the
// race 0% of 1000 runs. This call makes us
// wait on the client who waits on the server
// to be ready. So when it returns we know
// the server knows about the client.
// simplify echo test by leaving this out. the auto retry should suffice.
// Arg! Auto-retry does not always suffice! back in!
preventRaceByDoingPriorClientToServerRoundTrip(cli, srv)
vv("back from preventRaceByDoingPriorClientToServerRoundTrip")
ctx := context.Background()
srvServiceName := "speer1_on_server"
// Fragment/Circuit Peer API on Client/Server
cliServiceName := "cpeer0_on_client"
cpeer0 := &PeerImpl{}
cli.PeerAPI.RegisterPeerServiceFunc(cliServiceName, cpeer0.Start)
speer1 := &PeerImpl{
DoEchoToThisPeerURL: make(chan string),
ReportEchoTestCanSee: make(chan string),
}
srv.PeerAPI.RegisterPeerServiceFunc(srvServiceName, speer1.Start)
vv("srv registered speer1.Start") // seen
cliAddr := cli.LocalAddr()
// This call starts the PeerImpl on the remote Client, from the server.
// Arg. This was racing with the server actually getting
// the rwPair spun up, crashing 6% of the time.
// Thus we added a prior round-trip cli->srv->cli above.
peerURL_client, peerID_client, err := srv.PeerAPI.StartRemotePeer(
ctx, cliServiceName, cliAddr, 50*time.Microsecond)
panicOn(err)
vv("started remote with peerURL_client = '%v'; cliServiceName = '%v'; peerID_client = '%v'", peerURL_client, cliServiceName, peerID_client)
// any number of known peers can be supplied, or none, to bootstrap.
lpb, err := srv.PeerAPI.StartLocalPeer(ctx, srvServiceName, nil)
panicOn(err)
defer lpb.Close()
peerURL_server := lpb.URL()
peerID_server := lpb.PeerID
_ = peerURL_server
_ = peerID_server
vv("StartLocalPeer: on server peerURL_server = '%v'; peerID_server = '%v'", peerURL_server, peerID_server)
// lets ask the client to ask the server to start one, to
// test the symmetry of the CallPeerStartCircuit handling.
// get the tcp:// or udp:// in front like the client expects.
with_network_saddr := serverAddr.Network() + "://" + serverAddr.String()
vv("with_network_saddr = '%v'", with_network_saddr)
_ = with_network_saddr
/* simplify the echo test by leaving out 2nd server for now
peerURL_server2_from_remote_req, peerID_server2, err := cli.PeerAPI.StartRemotePeer(
ctx, srvServiceName, with_network_saddr, 0)
panicOn(err)
vv("started second instance of speer1_on_server, this time remote with PeerURL = '%v'; for service name = '%v'; peerID_server2 = '%v'",
peerURL_server2_from_remote_req, srvServiceName, peerID_server2)
// we should have seen the client peer start 1x, and the server 2x.
cv.So(cpeer0.StartCount.Load(), cv.ShouldEqual, 1)
cv.So(speer1.StartCount.Load(), cv.ShouldEqual, 2)
*/
vv(" ============= about to begin speer1.DoEchoToThisPeerURL <- peerURL_client = '%v'", peerURL_client)
// simple echo test from srv -> cli -> srv, over a circuit between peers.
// url :=
speer1.DoEchoToThisPeerURL <- peerURL_client
select {
case report := <-speer1.ReportEchoTestCanSee:
vv("speer1 echo got back: '%v'", report)
}
})
}
func preventRaceByDoingPriorClientToServerRoundTrip(cli *Client, srv *Server) {
serviceName := "customEcho"
srv.Register2Func(serviceName, customEcho)
req := NewMessage()
req.HDR.ServiceName = serviceName
req.JobSerz = []byte("Hello from client!")
reply, err := cli.SendAndGetReply(req, nil, 0)
panicOn(err)
_ = reply
//vv("reply = '%v'", reply)
//vv("good, response from Server means it definitely has client registered now.")
}
func Test401_PeerURL_parsing(t *testing.T) {
cv.Convey("parsePeerURL should extract network address, peerID, and optionally circuitID", t, func() {
peerURL := "tcp://x.x.x.x:5023/serviceName/peerID/circuitID"
netAddr, serviceName, peerID, circuitID, err := ParsePeerURL(peerURL)
panicOn(err)
vv("%v -> netAddr = '%v'", peerURL, netAddr)
vv("%v -> serviceName = '%v'", peerURL, serviceName)
vv("%v -> peerID = '%v'", peerURL, peerID)
vv("%v -> circuitID = '%v'", peerURL, circuitID)
cv.So(netAddr, cv.ShouldEqual, "tcp://x.x.x.x:5023")
cv.So(serviceName, cv.ShouldEqual, "serviceName")
cv.So(peerID, cv.ShouldEqual, "peerID")
cv.So(circuitID, cv.ShouldEqual, "circuitID")
})
}
func Test402_simpler_startup_peer_service_test(t *testing.T) {
cv.Convey("402: clone 400 and shrink it to focus just on not getting port 0 back", t, func() {
cfg := NewConfig()
cfg.TCPonly_no_TLS = true
cfg.UseQUIC = false // true
cfg.ServerAddr = "127.0.0.1:0"
srv := NewServer("srv_test402", cfg)
serverAddr, err := srv.Start()
panicOn(err)
defer srv.Close()
cfg.ClientDialToHostPort = serverAddr.String()
cli, err := NewClient("client_test402", cfg)
panicOn(err)
err = cli.Start()
panicOn(err)
defer cli.Close()
preventRaceByDoingPriorClientToServerRoundTrip(cli, srv)
ctx := context.Background()
srvServiceName := "speer1_on_server"
// Fragment/Circuit Peer API on Client/Server
cliServiceName := "cpeer0_on_client"
cpeer0 := &PeerImpl{}
cli.PeerAPI.RegisterPeerServiceFunc(cliServiceName, cpeer0.Start)
speer1 := &PeerImpl{
DoEchoToThisPeerURL: make(chan string),
}
srv.PeerAPI.RegisterPeerServiceFunc(srvServiceName, speer1.Start)
cliAddr := cli.LocalAddr()
vv("cliAddr = '%v'", cliAddr)
lpb, err := srv.PeerAPI.StartLocalPeer(ctx, srvServiceName, nil)
panicOn(err)
peerURL_server, peerID_server := lpb.URL(), lpb.PeerID
vv("StartLocalPeer: on server peerURL_server = '%v'; peerID_server = '%v'", peerURL_server, peerID_server)
if cfg.UseQUIC {
cv.So(peerURL_server, cv.ShouldStartWith, "udp://")
} else {
cv.So(peerURL_server, cv.ShouldStartWith, "tcp://")
}
})
}
// 403 just bringing up new Circuit from existing peer
func Test403_new_circuit_from_existing_peer(t *testing.T) {
cv.Convey("new circuit bootstrapped, from existing registered and started peers.", t, func() {
cfg := NewConfig()
cfg.TCPonly_no_TLS = true
cfg.ServerAddr = "127.0.0.1:0"
srv := NewServer("srv_test403", cfg)
serverAddr, err := srv.Start()
panicOn(err)
defer srv.Close()
cfg.ClientDialToHostPort = serverAddr.String()
cli, err := NewClient("client_test403", cfg)
panicOn(err)
err = cli.Start()
panicOn(err)
defer cli.Close()
preventRaceByDoingPriorClientToServerRoundTrip(cli, srv)
ctx := context.Background()
srvServiceName := "speer1_on_server"
// Fragment/Circuit Peer API on Client/Server
cliServiceName := "cpeer0_on_client"
cpeer0 := &PeerImpl{}
cli.PeerAPI.RegisterPeerServiceFunc(cliServiceName, cpeer0.Start)
speer1 := &PeerImpl{
DoEchoToThisPeerURL: make(chan string),
ReportEchoTestCanSee: make(chan string),
}
srv.PeerAPI.RegisterPeerServiceFunc(srvServiceName, speer1.Start)
cliAddr := cli.LocalAddr()
// This call starts the PeerImpl on the remote Client, from the server.
peerURL_client, peerID_client, err := srv.PeerAPI.StartRemotePeer(
ctx, cliServiceName, cliAddr, 50*time.Microsecond)
panicOn(err)
vv("started remote with peerURL_client = '%v'; cliServiceName = '%v'; peerID_client = '%v'", peerURL_client, cliServiceName, peerID_client)
lpb, err := srv.PeerAPI.StartLocalPeer(ctx, srvServiceName, nil)
peerURL_server, peerID_server := lpb.URL(), lpb.PeerID
panicOn(err)
vv("srv.PeerAPI.StartLocalPeer() on server peerURL_server = '%v'; peerID_server = '%v'", peerURL_server, peerID_server)
vv(" ============= about to begin speer1.DoEchoToThisPeerURL <- peerURL_client = '%v'", peerURL_client)
// simple echo test from srv -> cli -> srv, over a circuit between peers.
// url :=
speer1.DoEchoToThisPeerURL <- peerURL_client
select {
case report := <-speer1.ReportEchoTestCanSee:
vv("speer1 echo got back: '%v'", report)
}
})
}