mocknet: use host
Juan Batiz-Benet committed
Jan 1, 2015 at 12:02 UTC
128e820ccb8b2790455b7c96c1b6cfe8ce15ca24
3 files changed
+77
-41
p2p/net2/mock/interface.go
+5
-2
@@ -11,6 +11,7 @@ import (
11
"time"
12
13
ic "github.com/jbenet/go-ipfs/p2p/crypto"
14
+ host "github.com/jbenet/go-ipfs/p2p/host"
15
inet "github.com/jbenet/go-ipfs/p2p/net2"
16
peer "github.com/jbenet/go-ipfs/p2p/peer"
17
@@ -20,16 +21,18 @@ import (
21
type Mocknet interface {
22
23
// GenPeer generates a peer and its inet.Network in the Mocknet
23
- GenPeer() (inet.Network, error)
24
+ GenPeer() (host.Host, error)
25
26
// AddPeer adds an existing peer. we need both a privkey and addr.
27
// ID is derived from PrivKey
27
- AddPeer(ic.PrivKey, ma.Multiaddr) (inet.Network, error)
28
+ AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error)
29
30
// retrieve things (with randomized iteration order)
31
Peers() []peer.ID
32
Net(peer.ID) inet.Network
33
Nets() []inet.Network
34
+ Host(peer.ID) host.Host
35
+ Hosts() []host.Host
36
Links() LinkMap
37
LinksBetweenPeers(a, b peer.ID) []Link
38
LinksBetweenNets(a, b inet.Network) []Link
p2p/net2/mock/mock_net.go
+31
-13
@@ -5,6 +5,8 @@ import (
5
"sync"
6
7
ic "github.com/jbenet/go-ipfs/p2p/crypto"
8
+ host "github.com/jbenet/go-ipfs/p2p/host"
9
+ bhost "github.com/jbenet/go-ipfs/p2p/host/basic"
10
inet "github.com/jbenet/go-ipfs/p2p/net2"
11
peer "github.com/jbenet/go-ipfs/p2p/peer"
12
testutil "github.com/jbenet/go-ipfs/util/testutil"
@@ -16,9 +18,8 @@ import (
18
19
// mocknet implements mocknet.Mocknet
20
type mocknet struct {
19
- // must map on peer.ID (instead of peer.ID) because
20
- // each inet.Network has different peerstore
21
- nets map[peer.ID]*peernet
21
+ nets map[peer.ID]*peernet
22
+ hosts map[peer.ID]*bhost.BasicHost
23
24
// links make it possible to connect two peers.
25
// think of links as the physical medium.
@@ -35,12 +36,13 @@ type mocknet struct {
36
func New(ctx context.Context) Mocknet {
37
return &mocknet{
38
nets: map[peer.ID]*peernet{},
39
+ hosts: map[peer.ID]*bhost.BasicHost{},
40
links: map[peer.ID]map[peer.ID]map[*link]struct{}{},
41
cg: ctxgroup.WithContext(ctx),
42
}
43
}
44
43
-func (mn *mocknet) GenPeer() (inet.Network, error) {
45
+func (mn *mocknet) GenPeer() (host.Host, error) {
46
sk, _, err := testutil.RandKeyPair(512)
47
if err != nil {
48
return nil, err
@@ -48,20 +50,22 @@ func (mn *mocknet) GenPeer() (inet.Network, error) {
50
51
a := testutil.RandLocalTCPAddress()
52
51
- n, err := mn.AddPeer(sk, a)
53
+ h, err := mn.AddPeer(sk, a)
54
if err != nil {
55
return nil, err
56
}
57
56
- return n, nil
58
+ return h, nil
59
}
60
59
-func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (inet.Network, error) {
61
+func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
62
n, err := newPeernet(mn.cg.Context(), mn, k, a)
63
if err != nil {
64
return nil, err
65
}
66
67
+ h := bhost.New(n)
68
+
69
// make sure to add listening address!
70
// this makes debugging things simpler as remembering to register
71
// an address may cause unexpected failure.
@@ -72,8 +76,9 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (inet.Network, error) {
76
77
mn.Lock()
78
mn.nets[n.peer] = n
79
+ mn.hosts[n.peer] = h
80
mn.Unlock()
76
- return n, nil
81
+ return h, nil
82
}
83
84
func (mn *mocknet) Peers() []peer.ID {
@@ -87,16 +92,29 @@ func (mn *mocknet) Peers() []peer.ID {
92
return cp
93
}
94
95
+func (mn *mocknet) Host(pid peer.ID) host.Host {
96
+ mn.RLock()
97
+ host := mn.hosts[pid]
98
+ mn.RUnlock()
99
+ return host
100
+}
101
+
102
func (mn *mocknet) Net(pid peer.ID) inet.Network {
103
+ mn.RLock()
104
+ n := mn.nets[pid]
105
+ mn.RUnlock()
106
+ return n
107
+}
108
+
109
+func (mn *mocknet) Hosts() []host.Host {
110
mn.RLock()
111
defer mn.RUnlock()
112
94
- for _, n := range mn.nets {
95
- if n.peer == pid {
96
- return n
97
- }
113
+ cp := make([]host.Host, 0, len(mn.hosts))
114
+ for _, h := range mn.hosts {
115
+ cp = append(cp, h)
116
}
99
- return nil
117
+ return cp
118
}
119
120
func (mn *mocknet) Nets() []inet.Network {
p2p/net2/mock/mock_test.go
+41
-26
@@ -9,6 +9,7 @@ import (
9
10
inet "github.com/jbenet/go-ipfs/p2p/net2"
11
peer "github.com/jbenet/go-ipfs/p2p/peer"
12
+ protocol "github.com/jbenet/go-ipfs/p2p/protocol"
13
testutil "github.com/jbenet/go-ipfs/util/testutil"
14
15
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
@@ -46,31 +47,44 @@ func TestNetworkSetup(t *testing.T) {
47
a2 := testutil.RandLocalTCPAddress()
48
a3 := testutil.RandLocalTCPAddress()
49
49
- n1, err := mn.AddPeer(sk1, a1)
50
+ h1, err := mn.AddPeer(sk1, a1)
51
if err != nil {
52
t.Fatal(err)
53
}
53
- p1 := n1.LocalPeer()
54
+ p1 := h1.ID()
55
55
- n2, err := mn.AddPeer(sk2, a2)
56
+ h2, err := mn.AddPeer(sk2, a2)
57
if err != nil {
58
t.Fatal(err)
59
}
59
- p2 := n2.LocalPeer()
60
+ p2 := h2.ID()
61
61
- n3, err := mn.AddPeer(sk3, a3)
62
+ h3, err := mn.AddPeer(sk3, a3)
63
if err != nil {
64
t.Fatal(err)
65
}
65
- p3 := n3.LocalPeer()
66
+ p3 := h3.ID()
67
68
// check peers and net
69
+ if mn.Host(p1) != h1 {
70
+ t.Error("host for p1.ID != h1")
71
+ }
72
+ if mn.Host(p2) != h2 {
73
+ t.Error("host for p2.ID != h2")
74
+ }
75
+ if mn.Host(p3) != h3 {
76
+ t.Error("host for p3.ID != h3")
77
+ }
78
+
79
+ n1 := h1.Network()
80
if mn.Net(p1) != n1 {
81
t.Error("net for p1.ID != n1")
82
}
83
+ n2 := h2.Network()
84
if mn.Net(p2) != n2 {
85
t.Error("net for p2.ID != n1")
86
}
87
+ n3 := h3.Network()
88
if mn.Net(p3) != n3 {
89
t.Error("net for p3.ID != n1")
90
}
@@ -275,12 +289,12 @@ func TestStreams(t *testing.T) {
289
s.Close()
290
}
291
278
- nets := mn.Nets()
279
- for _, n := range nets {
280
- n.SetStreamHandler(handler)
292
+ hosts := mn.Hosts()
293
+ for _, h := range mn.Hosts() {
294
+ h.SetStreamHandler(protocol.TestingID, handler)
295
}
296
283
- s, err := nets[0].NewStream(nets[1].LocalPeer())
297
+ s, err := hosts[0].NewStream(protocol.TestingID, hosts[1].ID())
298
if err != nil {
299
t.Fatal(err)
300
}
@@ -350,9 +364,10 @@ func TestStreamsStress(t *testing.T) {
364
t.Fatal(err)
365
}
366
353
- nets := mn.Nets()
354
- for _, n := range nets {
355
- n.SetStreamHandler(makePonger("pingpong"))
367
+ hosts := mn.Hosts()
368
+ for _, h := range hosts {
369
+ ponger := makePonger(string(protocol.TestingID))
370
+ h.SetStreamHandler(protocol.TestingID, ponger)
371
}
372
373
var wg sync.WaitGroup
@@ -360,11 +375,11 @@ func TestStreamsStress(t *testing.T) {
375
wg.Add(1)
376
go func(i int) {
377
defer wg.Done()
363
- from := rand.Intn(len(nets))
364
- to := rand.Intn(len(nets))
365
- s, err := nets[from].NewStream(nets[to].LocalPeer())
378
+ from := rand.Intn(len(hosts))
379
+ to := rand.Intn(len(hosts))
380
+ s, err := hosts[from].NewStream(protocol.TestingID, hosts[to].ID())
381
if err != nil {
367
- log.Debugf("%d (%s) %d (%s)", from, nets[from], to, nets[to])
382
+ log.Debugf("%d (%s) %d (%s)", from, hosts[from], to, hosts[to])
383
panic(err)
384
}
385
@@ -389,12 +404,12 @@ func TestAdding(t *testing.T) {
404
}
405
406
a := testutil.RandLocalTCPAddress()
392
- n, err := mn.AddPeer(sk, a)
407
+ h, err := mn.AddPeer(sk, a)
408
if err != nil {
409
t.Fatal(err)
410
}
411
397
- peers = append(peers, n.LocalPeer())
412
+ peers = append(peers, h.ID())
413
}
414
415
p1 := peers[0]
@@ -410,11 +425,11 @@ func TestAdding(t *testing.T) {
425
}
426
427
// set the new stream handler on p2
413
- n2 := mn.Net(p2)
414
- if n2 == nil {
415
- t.Fatalf("no network for %s", p2)
428
+ h2 := mn.Host(p2)
429
+ if h2 == nil {
430
+ t.Fatalf("no host for %s", p2)
431
}
417
- n2.SetStreamHandler(func(s inet.Stream) {
432
+ h2.SetStreamHandler(protocol.TestingID, func(s inet.Stream) {
433
defer s.Close()
434
435
b := make([]byte, 4)
@@ -436,12 +451,12 @@ func TestAdding(t *testing.T) {
451
}
452
453
// talk to p2
439
- n1 := mn.Net(p1)
440
- if n1 == nil {
454
+ h1 := mn.Host(p1)
455
+ if h1 == nil {
456
t.Fatalf("no network for %s", p1)
457
}
458
444
- s, err := n1.NewStream(p2)
459
+ s, err := h1.NewStream(protocol.TestingID, p2)
460
if err != nil {
461
t.Fatal(err)
462
}