forked from libp2p/go-libp2p-pubsub-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub_test.go
396 lines (325 loc) · 8.02 KB
/
pubsub_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package namesys
import (
"bytes"
"context"
"fmt"
"testing"
"time"
"golang.org/x/sync/errgroup"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
bhost "github.com/libp2p/go-libp2p-blankhost"
pubsub "github.com/libp2p/go-libp2p-pubsub"
record "github.com/libp2p/go-libp2p-record"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
)
func newNetHost(ctx context.Context, t *testing.T) host.Host {
netw := swarmt.GenSwarm(t, ctx)
return bhost.NewBlankHost(netw)
}
func newNetHosts(ctx context.Context, t *testing.T, n int) []host.Host {
var out []host.Host
for i := 0; i < n; i++ {
h := newNetHost(ctx, t)
out = append(out, h)
}
return out
}
type testValidator struct{}
func (testValidator) Validate(key string, value []byte) error {
ns, k, err := record.SplitKey(key)
if err != nil {
return err
}
if ns != "namespace" {
return record.ErrInvalidRecordType
}
if !bytes.Contains(value, []byte(k)) {
return record.ErrInvalidRecordType
}
if bytes.Contains(value, []byte("invalid")) {
return record.ErrInvalidRecordType
}
return nil
}
func (testValidator) Select(key string, vals [][]byte) (int, error) {
if len(vals) == 0 {
panic("selector with no values")
}
var best []byte
idx := 0
for i, val := range vals {
if bytes.Compare(best, val) < 0 {
best = val
idx = i
}
}
return idx, nil
}
func setupTest(ctx context.Context, t *testing.T) (*PubsubValueStore, []*PubsubValueStore) {
key := "/namespace/key"
hosts := newNetHosts(ctx, t, 5)
vss := make([]*PubsubValueStore, len(hosts))
for i := 0; i < len(vss); i++ {
fs, err := pubsub.NewFloodSub(ctx, hosts[i])
if err != nil {
t.Fatal(err)
}
vss[i], err = NewPubsubValueStore(ctx, hosts[i], fs, testValidator{})
if err != nil {
t.Fatal(err)
}
}
pub := vss[0]
vss = vss[1:]
pubinfo := hosts[0].Peerstore().PeerInfo(hosts[0].ID())
for _, h := range hosts[1:] {
if err := h.Connect(ctx, pubinfo); err != nil {
t.Fatal(err)
}
}
time.Sleep(time.Millisecond * 100)
for i, vs := range vss {
checkNotFound(ctx, t, i, vs, key)
// delay to avoid connection storms
time.Sleep(time.Millisecond * 100)
}
// let the bootstrap finish
time.Sleep(time.Second * 1)
return pub, vss
}
// tests
func TestEarlyPublish(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := newNetHosts(ctx, t, 5)
key := "/namespace/key"
val := []byte("valid for key 1")
vss := make([]*PubsubValueStore, len(hosts))
for i := 0; i < len(vss); i++ {
fs, err := pubsub.NewFloodSub(ctx, hosts[i])
if err != nil {
t.Fatal(err)
}
vss[i], err = NewPubsubValueStore(ctx, hosts[i], fs, testValidator{})
if err != nil {
t.Fatal(err)
}
}
pub := vss[0]
vss = vss[1:]
if err := pub.PutValue(ctx, key, val); err != nil {
t.Fatal(err)
}
for i, vs := range vss {
connect(t, hosts[i], hosts[i+1])
if err := vs.Subscribe(key); err != nil {
t.Fatal(err)
}
}
// Wait for Fetch protocol to retrieve data
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, val)
}
}
func TestPubsubPublishSubscribe(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pub, vss := setupTest(ctx, t)
key := "/namespace/key"
key2 := "/namespace/key2"
val := []byte("valid for key 1")
err := pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
// let the flood propagate
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, val)
}
val = []byte("valid for key 2")
err = pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
// let the flood propagate
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, val)
}
// Check selector.
nval := []byte("valid for key 1")
err = pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
// let the flood propagate
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, val)
}
// Check validator.
nval = []byte("valid for key 9999 invalid")
err = pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
// let the flood propagate
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, val)
}
// Different key?
// subscribe to the second key
for i, vs := range vss {
checkNotFound(ctx, t, i, vs, key2)
}
time.Sleep(time.Second * 1)
// Put to the second key
nval = []byte("valid for key2")
err = pub.PutValue(ctx, key2, nval)
if err != nil {
t.Fatal(err)
}
// let the flood propagate
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key2, nval)
checkValue(ctx, t, i, vs, key, val)
}
// cancel subscriptions
for _, vs := range vss {
vs.Cancel(key)
}
time.Sleep(time.Millisecond * 100)
// Get missed value?
nval = []byte("valid for key 3")
err = pub.PutValue(ctx, key, nval)
if err != nil {
t.Fatal(err)
}
// resubscribe
for _, vs := range vss {
if err := vs.Subscribe(key); err != nil {
t.Fatal(err)
}
}
// check that we get the new value
time.Sleep(time.Second * 1)
for i, vs := range vss {
checkValue(ctx, t, i, vs, key, nval)
}
}
func TestWatch(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pub, vss := setupTest(ctx, t)
key := "/namespace/key"
key2 := "/namespace/key2"
ch, err := vss[1].SearchValue(ctx, key)
if err != nil {
t.Fatal(err)
}
val := []byte("valid for key 1")
err = pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
v := string(<-ch)
if v != "valid for key 1" {
t.Errorf("got unexpected value: %s", v)
}
val = []byte("valid for key 2")
err = pub.PutValue(ctx, key, val)
if err != nil {
t.Fatal(err)
}
ch, err = vss[1].SearchValue(ctx, key2)
if err != nil {
t.Fatal(err)
}
_, err = vss[1].Cancel(key2)
if err.Error() != "key has active subscriptions" {
t.Fatal("cancel should have failed")
}
// let the flood propagate
time.Sleep(time.Second * 1)
ch, err = vss[1].SearchValue(ctx, key)
if err != nil {
t.Fatal(err)
}
v = string(<-ch)
if v != "valid for key 2" {
t.Errorf("got unexpected value: %s", v)
}
}
func TestPutMany(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := newNetHosts(ctx, t, 5)
vss := make([]*PubsubValueStore, len(hosts))
for i := 0; i < len(vss); i++ {
fs, err := pubsub.NewFloodSub(ctx, hosts[i])
if err != nil {
t.Fatal(err)
}
vss[i], err = NewPubsubValueStore(ctx, hosts[i], fs, testValidator{})
if err != nil {
t.Fatal(err)
}
}
for i := 1; i < len(hosts); i++ {
connect(t, hosts[0], hosts[i])
}
const numRuns = 10
const numRoutines = 1000 // Note: if changing the numRoutines also change the number of digits in the fmtString
const fmtString = "%s-%04d"
const baseKey = "/namespace/key"
for i := 0; i < numRuns; i++ {
key := fmt.Sprintf("%s/%d", baseKey, i)
var eg errgroup.Group
for j := 0; j < numRoutines; j++ {
rtNum := j
eg.Go(func() error {
return vss[0].PutValue(ctx, key, []byte(fmt.Sprintf(fmtString, key, rtNum)))
})
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
}
finalValue := []byte(fmt.Sprintf(fmtString, key, numRoutines-1))
for j := 0; j < len(hosts); j++ {
for {
v, err := vss[j].GetValue(ctx, key)
if err != routing.ErrNotFound {
if err != nil {
t.Fatal(err)
}
if bytes.Equal(v, finalValue) {
break
}
}
time.Sleep(time.Millisecond * 100)
}
}
}
}
func checkNotFound(ctx context.Context, t *testing.T, i int, vs routing.ValueStore, key string) {
t.Helper()
_, err := vs.GetValue(ctx, key)
if err != routing.ErrNotFound {
t.Fatalf("[vssolver %d] unexpected error: %s", i, err.Error())
}
}
func checkValue(ctx context.Context, t *testing.T, i int, vs routing.ValueStore, key string, val []byte) {
t.Helper()
xval, err := vs.GetValue(ctx, key)
if err != nil {
t.Fatalf("[ValueStore %d] vssolve failed: %s", i, err.Error())
}
if !bytes.Equal(xval, val) {
t.Fatalf("[ValueStore %d] unexpected value: expected '%s', got '%s'", i, val, xval)
}
}