@cryptotaxi247 / kubo / commits / b573d6620

mocknet: internals test

Found bugs with it :)

Juan Batiz-Benet committed Dec 17, 2014 at 11:28 UTC b573d662089be7d5c1eff5af420cee41964ea70b
1 file changed +218 -31
net/mock/mock_test.go
+218 -31
@@ -14,45 +14,232 @@ import (
14 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
15 )
16
17 -// func TestNetworkSetup(t *testing.T) {
17 +func TestNetworkSetup(t *testing.T) {
18
19 -// p1 := testutil.RandPeer()
20 -// p2 := testutil.RandPeer()
21 -// p3 := testutil.RandPeer()
22 -// peers := []peer.Peer{p1, p2, p3}
19 + ctx := context.Background()
20 + p1 := testutil.RandPeer()
21 + p2 := testutil.RandPeer()
22 + p3 := testutil.RandPeer()
23 + mn := New(ctx)
24 + // peers := []peer.Peer{p1, p2, p3}
25 +
26 + // add peers to mock net
27 +
28 + n1, err := mn.AddPeer(p1.ID())
29 + if err != nil {
30 + t.Fatal(err)
31 + }
32 +
33 + n2, err := mn.AddPeer(p2.ID())
34 + if err != nil {
35 + t.Fatal(err)
36 + }
37 +
38 + n3, err := mn.AddPeer(p3.ID())
39 + if err != nil {
40 + t.Fatal(err)
41 + }
42 +
43 + // check peers and net
44 +
45 + if !mn.Peer(p1.ID()).ID().Equal(p1.ID()) {
46 + t.Error("peer for p1.ID != p1.ID")
47 + }
48 + if !mn.Peer(p2.ID()).ID().Equal(p2.ID()) {
49 + t.Error("peer for p2.ID != p2.ID")
50 + }
51 + if !mn.Peer(p3.ID()).ID().Equal(p3.ID()) {
52 + t.Error("peer for p3.ID != p3.ID")
53 + }
54 +
55 + if mn.Net(p1.ID()) != n1 {
56 + t.Error("net for p1.ID != n1")
57 + }
58 + if mn.Net(p2.ID()) != n2 {
59 + t.Error("net for p2.ID != n1")
60 + }
61 + if mn.Net(p3.ID()) != n3 {
62 + t.Error("net for p3.ID != n1")
63 + }
64 +
65 + // link p1<-->p2, p1<-->p1, p2<-->p3, p3<-->p2
66
24 -// nets, err := MakeNetworks(context.Background(), peers)
25 -// if err != nil {
26 -// t.Fatal(err)
27 -// }
67 + l12, err := mn.LinkPeers(p1, p2)
68 + if err != nil {
69 + t.Fatal(err)
70 + }
71 + if !(l12.Networks()[0] == n1 && l12.Networks()[1] == n2) &&
72 + !(l12.Networks()[0] == n2 && l12.Networks()[1] == n1) {
73 + t.Error("l12 networks incorrect")
74 + }
75 +
76 + l11, err := mn.LinkPeers(p1, p1)
77 + if err != nil {
78 + t.Fatal(err)
79 + }
80 + if !(l11.Networks()[0] == n1 && l11.Networks()[1] == n1) {
81 + t.Error("l11 networks incorrect")
82 + }
83 +
84 + l23, err := mn.LinkPeers(p2, p3)
85 + if err != nil {
86 + t.Fatal(err)
87 + }
88 + if !(l23.Networks()[0] == n2 && l23.Networks()[1] == n3) &&
89 + !(l23.Networks()[0] == n3 && l23.Networks()[1] == n2) {
90 + t.Error("l23 networks incorrect")
91 + }
92
29 -// // check things
93 + l32, err := mn.LinkPeers(p3, p2)
94 + if err != nil {
95 + t.Fatal(err)
96 + }
97 + if !(l32.Networks()[0] == n2 && l32.Networks()[1] == n3) &&
98 + !(l32.Networks()[0] == n3 && l32.Networks()[1] == n2) {
99 + t.Error("l32 networks incorrect")
100 + }
101
31 -// if len(nets) != 3 {
32 -// t.Error("nets must be 3")
33 -// }
102 + // check things
103
35 -// for i, n := range nets {
36 -// if n.local != peers[i] {
37 -// t.Error("peer mismatch")
38 -// }
104 + links12 := mn.LinksBetweenPeers(p1, p2)
105 + if len(links12) != 1 {
106 + t.Errorf("should be 1 link bt. p1 and p2 (found %d)", len(links12))
107 + }
108 + if links12[0] != l12 {
109 + t.Error("links 1-2 should be l12.")
110 + }
111
40 -// if len(n.conns) != len(nets) {
41 -// t.Error("conn mismatch")
42 -// }
112 + links11 := mn.LinksBetweenPeers(p1, p1)
113 + if len(links11) != 1 {
114 + t.Errorf("should be 1 link bt. p1 and p1 (found %d)", len(links11))
115 + }
116 + if links11[0] != l11 {
117 + t.Error("links 1-1 should be l11.")
118 + }
119
44 -// for _, c := range n.conns {
45 -// if c.remote.conns[n.local] == nil {
46 -// t.Error("conn other side fail")
47 -// }
48 -// if c.remote.conns[n.local].remote.local != n.local {
49 -// t.Error("conn other side fail")
50 -// }
51 -// }
120 + links23 := mn.LinksBetweenPeers(p2, p3)
121 + if len(links23) != 2 {
122 + t.Errorf("should be 2 link bt. p2 and p3 (found %d)", len(links23))
123 + }
124 + if !((links23[0] == l23 && links23[1] == l32) ||
125 + (links23[0] == l32 && links23[1] == l23)) {
126 + t.Error("links 2-3 should be l23 and l32.")
127 + }
128
53 -// }
129 + // unlinking
130
55 -// }
131 + if err := mn.UnlinkPeers(p2, p1); err != nil {
132 + t.Error(err)
133 + }
134 +
135 + // check only one link affected:
136 +
137 + links12 = mn.LinksBetweenPeers(p1, p2)
138 + if len(links12) != 0 {
139 + t.Errorf("should be 0 now...", len(links12))
140 + }
141 +
142 + links11 = mn.LinksBetweenPeers(p1, p1)
143 + if len(links11) != 1 {
144 + t.Errorf("should be 1 link bt. p1 and p1 (found %d)", len(links11))
145 + }
146 + if links11[0] != l11 {
147 + t.Error("links 1-1 should be l11.")
148 + }
149 +
150 + links23 = mn.LinksBetweenPeers(p2, p3)
151 + if len(links23) != 2 {
152 + t.Errorf("should be 2 link bt. p2 and p3 (found %d)", len(links23))
153 + }
154 + if !((links23[0] == l23 && links23[1] == l32) ||
155 + (links23[0] == l32 && links23[1] == l23)) {
156 + t.Error("links 2-3 should be l23 and l32.")
157 + }
158 +
159 + // check connecting
160 +
161 + // first, no conns
162 + if len(n2.Conns()) > 0 || len(n3.Conns()) > 0 {
163 + t.Error("should have 0 conn. Got: (%d, %d)", len(n2.Conns()), len(n3.Conns()))
164 + }
165 +
166 + // connect p2->p3
167 + if err := n2.DialPeer(ctx, p3); err != nil {
168 + t.Error(err)
169 + }
170 +
171 + if len(n2.Conns()) != 1 || len(n3.Conns()) != 1 {
172 + t.Errorf("should have (1,1) conn. Got: (%d, %d)", len(n2.Conns()), len(n3.Conns()))
173 + }
174 +
175 + // p := PrinterTo(os.Stdout)
176 + // p.NetworkConns(n1)
177 + // p.NetworkConns(n2)
178 + // p.NetworkConns(n3)
179 +
180 + // can create a stream 2->3, 3->2,
181 + if _, err := n2.NewStream(inet.ProtocolDiag, p3); err != nil {
182 + t.Error(err)
183 + }
184 + if _, err := n3.NewStream(inet.ProtocolDiag, p2); err != nil {
185 + t.Error(err)
186 + }
187 +
188 + // but not 1->2 nor 2->2 (not linked), nor 1->1 (not connected)
189 + if _, err := n1.NewStream(inet.ProtocolDiag, p2); err == nil {
190 + t.Error("should not be able to connect")
191 + }
192 + if _, err := n2.NewStream(inet.ProtocolDiag, p2); err == nil {
193 + t.Error("should not be able to connect")
194 + }
195 + if _, err := n1.NewStream(inet.ProtocolDiag, p1); err == nil {
196 + t.Error("should not be able to connect")
197 + }
198 +
199 + // connect p1->p1 (should work)
200 + if err := n1.DialPeer(ctx, p1); err != nil {
201 + t.Error("p1 should be able to dial self.", err)
202 + }
203 +
204 + // and a stream too
205 + if _, err := n1.NewStream(inet.ProtocolDiag, p1); err != nil {
206 + t.Error(err)
207 + }
208 +
209 + // connect p1->p2
210 + if err := n1.DialPeer(ctx, p2); err == nil {
211 + t.Error("p1 should not be able to dial p2, not connected...")
212 + }
213 +
214 + // connect p3->p1
215 + if err := n3.DialPeer(ctx, p1); err == nil {
216 + t.Error("p3 should not be able to dial p1, not connected...")
217 + }
218 +
219 + // relink p1->p2
220 +
221 + l12, err = mn.LinkPeers(p1, p2)
222 + if err != nil {
223 + t.Fatal(err)
224 + }
225 + if !(l12.Networks()[0] == n1 && l12.Networks()[1] == n2) &&
226 + !(l12.Networks()[0] == n2 && l12.Networks()[1] == n1) {
227 + t.Error("l12 networks incorrect")
228 + }
229 +
230 + // should now be able to connect
231 +
232 + // connect p1->p2
233 + if err := n1.DialPeer(ctx, p2); err != nil {
234 + t.Error(err)
235 + }
236 +
237 + // and a stream should work now too :)
238 + if _, err := n2.NewStream(inet.ProtocolDiag, p3); err != nil {
239 + t.Error(err)
240 + }
241 +
242 +}
243
244 func TestStreams(t *testing.T) {
245
@@ -174,9 +361,9 @@ func TestStreamsStress(t *testing.T) {
361 to := rand.Intn(len(nets))
362 p := rand.Intn(3)
363 proto := protos[p]
177 - // log.Debug("%d (%s) %d (%s) %d (%s)", from, nets[from], to, nets[to], p, protos[p])
364 s, err := nets[from].NewStream(protos[p], nets[to].LocalPeer())
365 if err != nil {
366 + log.Debugf("%d (%s) %d (%s) %d (%s)", from, nets[from], to, nets[to], p, protos[p])
367 panic(err)
368 }
369