mock_test.go 12.1 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
package mocknet

import (
	"bytes"
	"io"
Karthik Bala's avatar
Karthik Bala committed
6
	"math"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
8
9
	"math/rand"
	"sync"
	"testing"
Karthik Bala's avatar
Karthik Bala committed
10
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11

12
	testutil "github.com/ipfs/go-ipfs/util/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
14
15
	inet "github.com/ipfs/go-libp2p/p2p/net"
	peer "github.com/ipfs/go-libp2p/p2p/peer"
	protocol "github.com/ipfs/go-libp2p/p2p/protocol"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16

Jeromy's avatar
Jeromy committed
17
	detectrace "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-detect-race"
18
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
20
21
22
23
24
25
26
27
28
29
30
31
)

func randPeer(t *testing.T) peer.ID {
	p, err := testutil.RandPeerID()
	if err != nil {
		t.Fatal(err)
	}
	return p
}

func TestNetworkSetup(t *testing.T) {

	ctx := context.Background()
32
	sk1, _, err := testutil.RandTestKeyPair(512)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
	if err != nil {
		t.Fatal(t)
	}
36
	sk2, _, err := testutil.RandTestKeyPair(512)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
38
39
	if err != nil {
		t.Fatal(t)
	}
40
	sk3, _, err := testutil.RandTestKeyPair(512)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
42
43
44
45
46
47
48
49
50
51
52
	if err != nil {
		t.Fatal(t)
	}
	mn := New(ctx)
	// peers := []peer.ID{p1, p2, p3}

	// add peers to mock net

	a1 := testutil.RandLocalTCPAddress()
	a2 := testutil.RandLocalTCPAddress()
	a3 := testutil.RandLocalTCPAddress()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	h1, err := mn.AddPeer(sk1, a1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
55
56
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	p1 := h1.ID()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	h2, err := mn.AddPeer(sk2, a2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
61
62
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
	p2 := h2.ID()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
	h3, err := mn.AddPeer(sk3, a3)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66
67
68
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69
	p3 := h3.ID()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
71

	// check peers and net
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
73
74
75
76
77
78
79
80
81
82
	if mn.Host(p1) != h1 {
		t.Error("host for p1.ID != h1")
	}
	if mn.Host(p2) != h2 {
		t.Error("host for p2.ID != h2")
	}
	if mn.Host(p3) != h3 {
		t.Error("host for p3.ID != h3")
	}

	n1 := h1.Network()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
84
85
	if mn.Net(p1) != n1 {
		t.Error("net for p1.ID != n1")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86
	n2 := h2.Network()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
88
89
	if mn.Net(p2) != n2 {
		t.Error("net for p2.ID != n1")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
	n3 := h3.Network()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	if mn.Net(p3) != n3 {
		t.Error("net for p3.ID != n1")
	}

	// link p1<-->p2, p1<-->p1, p2<-->p3, p3<-->p2

	l12, err := mn.LinkPeers(p1, p2)
	if err != nil {
		t.Fatal(err)
	}
	if !(l12.Networks()[0] == n1 && l12.Networks()[1] == n2) &&
		!(l12.Networks()[0] == n2 && l12.Networks()[1] == n1) {
		t.Error("l12 networks incorrect")
	}

	l11, err := mn.LinkPeers(p1, p1)
	if err != nil {
		t.Fatal(err)
	}
	if !(l11.Networks()[0] == n1 && l11.Networks()[1] == n1) {
		t.Error("l11 networks incorrect")
	}

	l23, err := mn.LinkPeers(p2, p3)
	if err != nil {
		t.Fatal(err)
	}
	if !(l23.Networks()[0] == n2 && l23.Networks()[1] == n3) &&
		!(l23.Networks()[0] == n3 && l23.Networks()[1] == n2) {
		t.Error("l23 networks incorrect")
	}

	l32, err := mn.LinkPeers(p3, p2)
	if err != nil {
		t.Fatal(err)
	}
	if !(l32.Networks()[0] == n2 && l32.Networks()[1] == n3) &&
		!(l32.Networks()[0] == n3 && l32.Networks()[1] == n2) {
		t.Error("l32 networks incorrect")
	}

	// check things

	links12 := mn.LinksBetweenPeers(p1, p2)
	if len(links12) != 1 {
		t.Errorf("should be 1 link bt. p1 and p2 (found %d)", len(links12))
	}
	if links12[0] != l12 {
		t.Error("links 1-2 should be l12.")
	}

	links11 := mn.LinksBetweenPeers(p1, p1)
	if len(links11) != 1 {
		t.Errorf("should be 1 link bt. p1 and p1 (found %d)", len(links11))
	}
	if links11[0] != l11 {
		t.Error("links 1-1 should be l11.")
	}

	links23 := mn.LinksBetweenPeers(p2, p3)
	if len(links23) != 2 {
		t.Errorf("should be 2 link bt. p2 and p3 (found %d)", len(links23))
	}
	if !((links23[0] == l23 && links23[1] == l32) ||
		(links23[0] == l32 && links23[1] == l23)) {
		t.Error("links 2-3 should be l23 and l32.")
	}

	// unlinking

	if err := mn.UnlinkPeers(p2, p1); err != nil {
		t.Error(err)
	}

	// check only one link affected:

	links12 = mn.LinksBetweenPeers(p1, p2)
	if len(links12) != 0 {
		t.Errorf("should be 0 now...", len(links12))
	}

	links11 = mn.LinksBetweenPeers(p1, p1)
	if len(links11) != 1 {
		t.Errorf("should be 1 link bt. p1 and p1 (found %d)", len(links11))
	}
	if links11[0] != l11 {
		t.Error("links 1-1 should be l11.")
	}

	links23 = mn.LinksBetweenPeers(p2, p3)
	if len(links23) != 2 {
		t.Errorf("should be 2 link bt. p2 and p3 (found %d)", len(links23))
	}
	if !((links23[0] == l23 && links23[1] == l32) ||
		(links23[0] == l32 && links23[1] == l23)) {
		t.Error("links 2-3 should be l23 and l32.")
	}

	// check connecting

	// first, no conns
	if len(n2.Conns()) > 0 || len(n3.Conns()) > 0 {
		t.Error("should have 0 conn. Got: (%d, %d)", len(n2.Conns()), len(n3.Conns()))
	}

	// connect p2->p3
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197
	if _, err := n2.DialPeer(ctx, p3); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
198
199
200
201
202
203
204
205
206
207
208
209
210
		t.Error(err)
	}

	if len(n2.Conns()) != 1 || len(n3.Conns()) != 1 {
		t.Errorf("should have (1,1) conn. Got: (%d, %d)", len(n2.Conns()), len(n3.Conns()))
	}

	// p := PrinterTo(os.Stdout)
	// p.NetworkConns(n1)
	// p.NetworkConns(n2)
	// p.NetworkConns(n3)

	// can create a stream 2->3, 3->2,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
211
	if _, err := n2.NewStream(p3); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212
213
		t.Error(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
214
	if _, err := n3.NewStream(p2); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215
216
217
218
		t.Error(err)
	}

	// but not 1->2 nor 2->2 (not linked), nor 1->1 (not connected)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
219
	if _, err := n1.NewStream(p2); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
220
221
		t.Error("should not be able to connect")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222
	if _, err := n2.NewStream(p2); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223
224
		t.Error("should not be able to connect")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225
	if _, err := n1.NewStream(p1); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
226
227
228
229
		t.Error("should not be able to connect")
	}

	// connect p1->p1 (should work)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230
	if _, err := n1.DialPeer(ctx, p1); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
231
232
233
234
		t.Error("p1 should be able to dial self.", err)
	}

	// and a stream too
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235
	if _, err := n1.NewStream(p1); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
236
237
238
239
		t.Error(err)
	}

	// connect p1->p2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
240
	if _, err := n1.DialPeer(ctx, p2); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
241
242
243
244
		t.Error("p1 should not be able to dial p2, not connected...")
	}

	// connect p3->p1
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
245
	if _, err := n3.DialPeer(ctx, p1); err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
		t.Error("p3 should not be able to dial p1, not connected...")
	}

	// relink p1->p2

	l12, err = mn.LinkPeers(p1, p2)
	if err != nil {
		t.Fatal(err)
	}
	if !(l12.Networks()[0] == n1 && l12.Networks()[1] == n2) &&
		!(l12.Networks()[0] == n2 && l12.Networks()[1] == n1) {
		t.Error("l12 networks incorrect")
	}

	// should now be able to connect

	// connect p1->p2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
263
	if _, err := n1.DialPeer(ctx, p2); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
265
266
267
		t.Error(err)
	}

	// and a stream should work now too :)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
268
	if _, err := n2.NewStream(p3); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
269
270
271
272
273
274
275
276
277
278
279
280
281
		t.Error(err)
	}

}

func TestStreams(t *testing.T) {

	mn, err := FullMeshConnected(context.Background(), 3)
	if err != nil {
		t.Fatal(err)
	}

	handler := func(s inet.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
282
283
284
285
286
287
288
289
290
291
292
		b := make([]byte, 4)
		if _, err := io.ReadFull(s, b); err != nil {
			panic(err)
		}
		if !bytes.Equal(b, []byte("beep")) {
			panic("bytes mismatch")
		}
		if _, err := s.Write([]byte("boop")); err != nil {
			panic(err)
		}
		s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
293
294
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
295
296
297
	hosts := mn.Hosts()
	for _, h := range mn.Hosts() {
		h.SetStreamHandler(protocol.TestingID, handler)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
298
299
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300
	s, err := hosts[0].NewStream(protocol.TestingID, hosts[1].ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
	if err != nil {
		t.Fatal(err)
	}

	if _, err := s.Write([]byte("beep")); err != nil {
		panic(err)
	}
	b := make([]byte, 4)
	if _, err := io.ReadFull(s, b); err != nil {
		panic(err)
	}
	if !bytes.Equal(b, []byte("boop")) {
		panic("bytes mismatch 2")
	}

}

func makePinger(st string, n int) func(inet.Stream) {
	return func(s inet.Stream) {
		go func() {
			defer s.Close()

			for i := 0; i < n; i++ {
				b := make([]byte, 4+len(st))
				if _, err := s.Write([]byte("ping" + st)); err != nil {
					panic(err)
				}
				if _, err := io.ReadFull(s, b); err != nil {
					panic(err)
				}
				if !bytes.Equal(b, []byte("pong"+st)) {
					panic("bytes mismatch")
				}
			}
		}()
	}
}

func makePonger(st string) func(inet.Stream) {
	return func(s inet.Stream) {
		go func() {
			defer s.Close()

			for {
				b := make([]byte, 4+len(st))
				if _, err := io.ReadFull(s, b); err != nil {
					if err == io.EOF {
						return
					}
					panic(err)
				}
				if !bytes.Equal(b, []byte("ping"+st)) {
					panic("bytes mismatch")
				}
				if _, err := s.Write([]byte("pong" + st)); err != nil {
					panic(err)
				}
			}
		}()
	}
}

func TestStreamsStress(t *testing.T) {
Jeromy's avatar
Jeromy committed
364
365
366
367
	nnodes := 100
	if detectrace.WithRace() {
		nnodes = 50
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
368

Jeromy's avatar
Jeromy committed
369
	mn, err := FullMeshConnected(context.Background(), nnodes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
370
371
372
373
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
374
375
376
377
	hosts := mn.Hosts()
	for _, h := range hosts {
		ponger := makePonger(string(protocol.TestingID))
		h.SetStreamHandler(protocol.TestingID, ponger)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
378
379
380
381
382
383
384
	}

	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
385
386
387
			from := rand.Intn(len(hosts))
			to := rand.Intn(len(hosts))
			s, err := hosts[from].NewStream(protocol.TestingID, hosts[to].ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
388
			if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
389
				log.Debugf("%d (%s) %d (%s)", from, hosts[from], to, hosts[to])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
390
391
392
393
				panic(err)
			}

			log.Infof("%d start pinging", i)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
394
			makePinger("pingpong", rand.Intn(100))(s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
395
396
397
398
399
400
401
402
403
404
405
406
407
			log.Infof("%d done pinging", i)
		}(i)
	}

	wg.Wait()
}

func TestAdding(t *testing.T) {

	mn := New(context.Background())

	peers := []peer.ID{}
	for i := 0; i < 3; i++ {
408
		sk, _, err := testutil.RandTestKeyPair(512)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
409
410
411
412
413
		if err != nil {
			t.Fatal(err)
		}

		a := testutil.RandLocalTCPAddress()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
414
		h, err := mn.AddPeer(sk, a)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
415
416
417
418
		if err != nil {
			t.Fatal(err)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
419
		peers = append(peers, h.ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	}

	p1 := peers[0]
	p2 := peers[1]

	// link them
	for _, p1 := range peers {
		for _, p2 := range peers {
			if _, err := mn.LinkPeers(p1, p2); err != nil {
				t.Error(err)
			}
		}
	}

	// set the new stream handler on p2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
435
436
437
	h2 := mn.Host(p2)
	if h2 == nil {
		t.Fatalf("no host for %s", p2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
438
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
439
440
	h2.SetStreamHandler(protocol.TestingID, func(s inet.Stream) {
		defer s.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
441

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
442
443
444
445
446
447
448
		b := make([]byte, 4)
		if _, err := io.ReadFull(s, b); err != nil {
			panic(err)
		}
		if string(b) != "beep" {
			panic("did not beep!")
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
449

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
450
451
452
		if _, err := s.Write([]byte("boop")); err != nil {
			panic(err)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
453
454
455
	})

	// connect p1 to p2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
456
	if _, err := mn.ConnectPeers(p1, p2); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
457
458
459
460
		t.Fatal(err)
	}

	// talk to p2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
461
462
	h1 := mn.Host(p1)
	if h1 == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
463
464
465
		t.Fatalf("no network for %s", p1)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
466
	s, err := h1.NewStream(protocol.TestingID, p2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
	if err != nil {
		t.Fatal(err)
	}

	if _, err := s.Write([]byte("beep")); err != nil {
		t.Error(err)
	}
	b := make([]byte, 4)
	if _, err := io.ReadFull(s, b); err != nil {
		t.Error(err)
	}
	if !bytes.Equal(b, []byte("boop")) {
		t.Error("bytes mismatch 2")
	}

}
Karthik Bala's avatar
Karthik Bala committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

func TestRateLimiting(t *testing.T) {
	rl := NewRatelimiter(10)

	if !within(rl.Limit(10), time.Duration(float32(time.Second)), time.Millisecond/10) {
		t.Fail()
	}
	if !within(rl.Limit(10), time.Duration(float32(time.Second*2)), time.Millisecond) {
		t.Fail()
	}
	if !within(rl.Limit(10), time.Duration(float32(time.Second*3)), time.Millisecond) {
		t.Fail()
	}

	if within(rl.Limit(10), time.Duration(float32(time.Second*3)), time.Millisecond) {
		t.Fail()
	}

	rl.UpdateBandwidth(50)
	if !within(rl.Limit(75), time.Duration(float32(time.Second)*1.5), time.Millisecond/10) {
		t.Fail()
	}

	if within(rl.Limit(75), time.Duration(float32(time.Second)*1.5), time.Millisecond/10) {
		t.Fail()
	}

	rl.UpdateBandwidth(100)
	if !within(rl.Limit(1), time.Duration(time.Millisecond*10), time.Millisecond/10) {
		t.Fail()
	}

	if within(rl.Limit(1), time.Duration(time.Millisecond*10), time.Millisecond/10) {
		t.Fail()
	}
}

func within(t1 time.Duration, t2 time.Duration, tolerance time.Duration) bool {
	return math.Abs(float64(t1)-float64(t2)) < float64(tolerance)
}

func TestLimitedStreams(t *testing.T) {
	mn, err := FullMeshConnected(context.Background(), 2)
	if err != nil {
		t.Fatal(err)
	}

	var wg sync.WaitGroup
	messages := 4
	messageSize := 500
	handler := func(s inet.Stream) {
		b := make([]byte, messageSize)
		for i := 0; i < messages; i++ {
			if _, err := io.ReadFull(s, b); err != nil {
				log.Fatal(err)
			}
			if !bytes.Equal(b[:4], []byte("ping")) {
				log.Fatal("bytes mismatch")
			}
			wg.Done()
		}
		s.Close()
	}

	hosts := mn.Hosts()
	for _, h := range mn.Hosts() {
		h.SetStreamHandler(protocol.TestingID, handler)
	}

	peers := mn.Peers()
	links := mn.LinksBetweenPeers(peers[0], peers[1])
	//  1000 byte per second bandwidth
	bps := float64(1000)
	opts := links[0].Options()
	opts.Bandwidth = bps
	for _, link := range links {
		link.SetOptions(opts)
	}

	s, err := hosts[0].NewStream(protocol.TestingID, hosts[1].ID())
	if err != nil {
		t.Fatal(err)
	}

	filler := make([]byte, messageSize-4)
	data := append([]byte("ping"), filler...)
	before := time.Now()
	for i := 0; i < messages; i++ {
		wg.Add(1)
		if _, err := s.Write(data); err != nil {
			panic(err)
		}
	}

	wg.Wait()
	if !within(time.Since(before), time.Duration(time.Second*2), time.Second/3) {
		t.Fatal("Expected 2ish seconds but got ", time.Since(before))
	}
}