@cryptotaxi247 / kubo / commits / 18cfe02d3

dht tests with context

Juan Batiz-Benet committed Oct 18, 2014 at 04:19 UTC 18cfe02d389ef994924f844b13c1f642173a6ed9
2 files changed +60 -54
routing/dht/dht_test.go
+53 -49
@@ -21,9 +21,7 @@ import (
21 "time"
22 )
23
24 -func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
25 - ctx := context.Background()
26 -
24 +func setupDHT(ctx context.Context, t *testing.T, p *peer.Peer) *IpfsDHT {
25 peerstore := peer.NewPeerstore()
26
27 dhts := netservice.NewService(nil) // nil handler for now, need to patch it
@@ -38,12 +36,12 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
36 t.Fatal(err)
37 }
38
41 - d := NewDHT(p, peerstore, net, dhts, ds.NewMapDatastore())
39 + d := NewDHT(ctx, p, peerstore, net, dhts, ds.NewMapDatastore())
40 dhts.SetHandler(d)
41 return d
42 }
43
46 -func setupDHTS(n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
44 +func setupDHTS(ctx context.Context, n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
45 var addrs []ma.Multiaddr
46 for i := 0; i < n; i++ {
47 a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
@@ -61,7 +59,7 @@ func setupDHTS(n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
59
60 dhts := make([]*IpfsDHT, n)
61 for i := 0; i < n; i++ {
64 - dhts[i] = setupDHT(t, peers[i])
62 + dhts[i] = setupDHT(ctx, t, peers[i])
63 }
64
65 return addrs, peers, dhts
@@ -87,7 +85,7 @@ func makePeer(addr ma.Multiaddr) *peer.Peer {
85
86 func TestPing(t *testing.T) {
87 // t.Skip("skipping test to debug another")
90 -
88 + ctx := context.Background()
89 u.Debug = false
90 addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
91 if err != nil {
@@ -101,28 +99,28 @@ func TestPing(t *testing.T) {
99 peerA := makePeer(addrA)
100 peerB := makePeer(addrB)
101
104 - dhtA := setupDHT(t, peerA)
105 - dhtB := setupDHT(t, peerB)
102 + dhtA := setupDHT(ctx, t, peerA)
103 + dhtB := setupDHT(ctx, t, peerB)
104
105 defer dhtA.Halt()
106 defer dhtB.Halt()
107 defer dhtA.network.Close()
108 defer dhtB.network.Close()
109
112 - _, err = dhtA.Connect(context.Background(), peerB)
110 + _, err = dhtA.Connect(ctx, peerB)
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 //Test that we can ping the node
118 - ctx, _ := context.WithTimeout(context.Background(), 5*time.Millisecond)
119 - err = dhtA.Ping(ctx, peerB)
116 + ctxT, _ := context.WithTimeout(ctx, 5*time.Millisecond)
117 + err = dhtA.Ping(ctxT, peerB)
118 if err != nil {
119 t.Fatal(err)
120 }
121
124 - ctx, _ = context.WithTimeout(context.Background(), 5*time.Millisecond)
125 - err = dhtB.Ping(ctx, peerA)
122 + ctxT, _ = context.WithTimeout(ctx, 5*time.Millisecond)
123 + err = dhtB.Ping(ctxT, peerA)
124 if err != nil {
125 t.Fatal(err)
126 }
@@ -131,6 +129,7 @@ func TestPing(t *testing.T) {
129 func TestValueGetSet(t *testing.T) {
130 // t.Skip("skipping test to debug another")
131
132 + ctx := context.Background()
133 u.Debug = false
134 addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
135 if err != nil {
@@ -144,23 +143,23 @@ func TestValueGetSet(t *testing.T) {
143 peerA := makePeer(addrA)
144 peerB := makePeer(addrB)
145
147 - dhtA := setupDHT(t, peerA)
148 - dhtB := setupDHT(t, peerB)
146 + dhtA := setupDHT(ctx, t, peerA)
147 + dhtB := setupDHT(ctx, t, peerB)
148
149 defer dhtA.Halt()
150 defer dhtB.Halt()
151 defer dhtA.network.Close()
152 defer dhtB.network.Close()
153
155 - _, err = dhtA.Connect(context.Background(), peerB)
154 + _, err = dhtA.Connect(ctx, peerB)
155 if err != nil {
156 t.Fatal(err)
157 }
158
160 - ctxT, _ := context.WithTimeout(context.Background(), time.Second)
159 + ctxT, _ := context.WithTimeout(ctx, time.Second)
160 dhtA.PutValue(ctxT, "hello", []byte("world"))
161
163 - ctxT, _ = context.WithTimeout(context.Background(), time.Second*2)
162 + ctxT, _ = context.WithTimeout(ctx, time.Second*2)
163 val, err := dhtA.GetValue(ctxT, "hello")
164 if err != nil {
165 t.Fatal(err)
@@ -170,7 +169,7 @@ func TestValueGetSet(t *testing.T) {
169 t.Fatalf("Expected 'world' got '%s'", string(val))
170 }
171
173 - ctxT, _ = context.WithTimeout(context.Background(), time.Second*2)
172 + ctxT, _ = context.WithTimeout(ctx, time.Second*2)
173 val, err = dhtB.GetValue(ctxT, "hello")
174 if err != nil {
175 t.Fatal(err)
@@ -183,10 +182,11 @@ func TestValueGetSet(t *testing.T) {
182
183 func TestProvides(t *testing.T) {
184 // t.Skip("skipping test to debug another")
185 + ctx := context.Background()
186
187 u.Debug = false
188
189 - _, peers, dhts := setupDHTS(4, t)
189 + _, peers, dhts := setupDHTS(ctx, 4, t)
190 defer func() {
191 for i := 0; i < 4; i++ {
192 dhts[i].Halt()
@@ -194,17 +194,17 @@ func TestProvides(t *testing.T) {
194 }
195 }()
196
197 - _, err := dhts[0].Connect(context.Background(), peers[1])
197 + _, err := dhts[0].Connect(ctx, peers[1])
198 if err != nil {
199 t.Fatal(err)
200 }
201
202 - _, err = dhts[1].Connect(context.Background(), peers[2])
202 + _, err = dhts[1].Connect(ctx, peers[2])
203 if err != nil {
204 t.Fatal(err)
205 }
206
207 - _, err = dhts[1].Connect(context.Background(), peers[3])
207 + _, err = dhts[1].Connect(ctx, peers[3])
208 if err != nil {
209 t.Fatal(err)
210 }
@@ -219,14 +219,14 @@ func TestProvides(t *testing.T) {
219 t.Fatal(err)
220 }
221
222 - err = dhts[3].Provide(context.Background(), u.Key("hello"))
222 + err = dhts[3].Provide(ctx, u.Key("hello"))
223 if err != nil {
224 t.Fatal(err)
225 }
226
227 time.Sleep(time.Millisecond * 60)
228
229 - ctxT, _ := context.WithTimeout(context.Background(), time.Second)
229 + ctxT, _ := context.WithTimeout(ctx, time.Second)
230 provchan := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 1)
231
232 after := time.After(time.Second)
@@ -243,9 +243,10 @@ func TestProvides(t *testing.T) {
243 func TestProvidesAsync(t *testing.T) {
244 // t.Skip("skipping test to debug another")
245
246 + ctx := context.Background()
247 u.Debug = false
248
248 - _, peers, dhts := setupDHTS(4, t)
249 + _, peers, dhts := setupDHTS(ctx, 4, t)
250 defer func() {
251 for i := 0; i < 4; i++ {
252 dhts[i].Halt()
@@ -253,17 +254,17 @@ func TestProvidesAsync(t *testing.T) {
254 }
255 }()
256
256 - _, err := dhts[0].Connect(context.Background(), peers[1])
257 + _, err := dhts[0].Connect(ctx, peers[1])
258 if err != nil {
259 t.Fatal(err)
260 }
261
261 - _, err = dhts[1].Connect(context.Background(), peers[2])
262 + _, err = dhts[1].Connect(ctx, peers[2])
263 if err != nil {
264 t.Fatal(err)
265 }
266
266 - _, err = dhts[1].Connect(context.Background(), peers[3])
267 + _, err = dhts[1].Connect(ctx, peers[3])
268 if err != nil {
269 t.Fatal(err)
270 }
@@ -278,21 +279,21 @@ func TestProvidesAsync(t *testing.T) {
279 t.Fatal(err)
280 }
281
281 - err = dhts[3].Provide(context.Background(), u.Key("hello"))
282 + err = dhts[3].Provide(ctx, u.Key("hello"))
283 if err != nil {
284 t.Fatal(err)
285 }
286
287 time.Sleep(time.Millisecond * 60)
288
288 - ctx, _ := context.WithTimeout(context.TODO(), time.Millisecond*300)
289 - provs := dhts[0].FindProvidersAsync(ctx, u.Key("hello"), 5)
289 + ctxT, _ := context.WithTimeout(ctx, time.Millisecond*300)
290 + provs := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 5)
291 select {
292 case p := <-provs:
293 if !p.ID.Equal(dhts[3].self.ID) {
294 t.Fatalf("got a provider, but not the right one. %s", p)
295 }
295 - case <-ctx.Done():
296 + case <-ctxT.Done():
297 t.Fatal("Didnt get back providers")
298 }
299 }
@@ -300,8 +301,9 @@ func TestProvidesAsync(t *testing.T) {
301 func TestLayeredGet(t *testing.T) {
302 // t.Skip("skipping test to debug another")
303
304 + ctx := context.Background()
305 u.Debug = false
304 - _, peers, dhts := setupDHTS(4, t)
306 + _, peers, dhts := setupDHTS(ctx, 4, t)
307 defer func() {
308 for i := 0; i < 4; i++ {
309 dhts[i].Halt()
@@ -309,17 +311,17 @@ func TestLayeredGet(t *testing.T) {
311 }
312 }()
313
312 - _, err := dhts[0].Connect(context.Background(), peers[1])
314 + _, err := dhts[0].Connect(ctx, peers[1])
315 if err != nil {
316 t.Fatalf("Failed to connect: %s", err)
317 }
318
317 - _, err = dhts[1].Connect(context.Background(), peers[2])
319 + _, err = dhts[1].Connect(ctx, peers[2])
320 if err != nil {
321 t.Fatal(err)
322 }
323
322 - _, err = dhts[1].Connect(context.Background(), peers[3])
324 + _, err = dhts[1].Connect(ctx, peers[3])
325 if err != nil {
326 t.Fatal(err)
327 }
@@ -329,14 +331,14 @@ func TestLayeredGet(t *testing.T) {
331 t.Fatal(err)
332 }
333
332 - err = dhts[3].Provide(context.Background(), u.Key("hello"))
334 + err = dhts[3].Provide(ctx, u.Key("hello"))
335 if err != nil {
336 t.Fatal(err)
337 }
338
339 time.Sleep(time.Millisecond * 60)
340
339 - ctxT, _ := context.WithTimeout(context.Background(), time.Second)
341 + ctxT, _ := context.WithTimeout(ctx, time.Second)
342 val, err := dhts[0].GetValue(ctxT, u.Key("hello"))
343 if err != nil {
344 t.Fatal(err)
@@ -351,9 +353,10 @@ func TestLayeredGet(t *testing.T) {
353 func TestFindPeer(t *testing.T) {
354 // t.Skip("skipping test to debug another")
355
356 + ctx := context.Background()
357 u.Debug = false
358
356 - _, peers, dhts := setupDHTS(4, t)
359 + _, peers, dhts := setupDHTS(ctx, 4, t)
360 defer func() {
361 for i := 0; i < 4; i++ {
362 dhts[i].Halt()
@@ -361,22 +364,22 @@ func TestFindPeer(t *testing.T) {
364 }
365 }()
366
364 - _, err := dhts[0].Connect(context.Background(), peers[1])
367 + _, err := dhts[0].Connect(ctx, peers[1])
368 if err != nil {
369 t.Fatal(err)
370 }
371
369 - _, err = dhts[1].Connect(context.Background(), peers[2])
372 + _, err = dhts[1].Connect(ctx, peers[2])
373 if err != nil {
374 t.Fatal(err)
375 }
376
374 - _, err = dhts[1].Connect(context.Background(), peers[3])
377 + _, err = dhts[1].Connect(ctx, peers[3])
378 if err != nil {
379 t.Fatal(err)
380 }
381
379 - ctxT, _ := context.WithTimeout(context.Background(), time.Second)
382 + ctxT, _ := context.WithTimeout(ctx, time.Second)
383 p, err := dhts[0].FindPeer(ctxT, peers[2].ID)
384 if err != nil {
385 t.Fatal(err)
@@ -394,6 +397,7 @@ func TestFindPeer(t *testing.T) {
397 func TestConnectCollision(t *testing.T) {
398 // t.Skip("skipping test to debug another")
399
400 + ctx := context.Background()
401 u.Debug = false
402 addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
403 if err != nil {
@@ -407,8 +411,8 @@ func TestConnectCollision(t *testing.T) {
411 peerA := makePeer(addrA)
412 peerB := makePeer(addrB)
413
410 - dhtA := setupDHT(t, peerA)
411 - dhtB := setupDHT(t, peerB)
414 + dhtA := setupDHT(ctx, t, peerA)
415 + dhtB := setupDHT(ctx, t, peerB)
416
417 defer dhtA.Halt()
418 defer dhtB.Halt()
@@ -417,14 +421,14 @@ func TestConnectCollision(t *testing.T) {
421
422 done := make(chan struct{})
423 go func() {
420 - _, err = dhtA.Connect(context.Background(), peerB)
424 + _, err = dhtA.Connect(ctx, peerB)
425 if err != nil {
426 t.Fatal(err)
427 }
428 done <- struct{}{}
429 }()
430 go func() {
427 - _, err = dhtB.Connect(context.Background(), peerA)
431 + _, err = dhtB.Connect(ctx, peerA)
432 if err != nil {
433 t.Fatal(err)
434 }
routing/dht/ext_test.go
+7 -5
@@ -110,7 +110,7 @@ func TestGetFailures(t *testing.T) {
110 local := new(peer.Peer)
111 local.ID = peer.ID("test_peer")
112
113 - d := NewDHT(local, peerstore, fn, fs, ds.NewMapDatastore())
113 + d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
114 other := &peer.Peer{ID: peer.ID("other_peer")}
115 d.Update(other)
116
@@ -200,6 +200,7 @@ func _randPeer() *peer.Peer {
200 func TestNotFound(t *testing.T) {
201 // t.Skip("skipping test because it makes a lot of output")
202
203 + ctx := context.Background()
204 fn := &fauxNet{}
205 fs := &fauxSender{}
206
@@ -207,7 +208,7 @@ func TestNotFound(t *testing.T) {
208 local.ID = peer.ID("test_peer")
209 peerstore := peer.NewPeerstore()
210
210 - d := NewDHT(local, peerstore, fn, fs, ds.NewMapDatastore())
211 + d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
212
213 var ps []*peer.Peer
214 for i := 0; i < 5; i++ {
@@ -243,7 +244,7 @@ func TestNotFound(t *testing.T) {
244
245 })
246
246 - ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
247 + ctx, _ = context.WithTimeout(ctx, time.Second*5)
248 v, err := d.GetValue(ctx, u.Key("hello"))
249 log.Debug("get value got %v", v)
250 if err != nil {
@@ -265,6 +266,7 @@ func TestNotFound(t *testing.T) {
266 func TestLessThanKResponses(t *testing.T) {
267 // t.Skip("skipping test because it makes a lot of output")
268
269 + ctx := context.Background()
270 u.Debug = false
271 fn := &fauxNet{}
272 fs := &fauxSender{}
@@ -272,7 +274,7 @@ func TestLessThanKResponses(t *testing.T) {
274 local := new(peer.Peer)
275 local.ID = peer.ID("test_peer")
276
275 - d := NewDHT(local, peerstore, fn, fs, ds.NewMapDatastore())
277 + d := NewDHT(ctx, local, peerstore, fn, fs, ds.NewMapDatastore())
278
279 var ps []*peer.Peer
280 for i := 0; i < 5; i++ {
@@ -307,7 +309,7 @@ func TestLessThanKResponses(t *testing.T) {
309
310 })
311
310 - ctx, _ := context.WithTimeout(context.Background(), time.Second*30)
312 + ctx, _ = context.WithTimeout(ctx, time.Second*30)
313 _, err := d.GetValue(ctx, u.Key("hello"))
314 if err != nil {
315 switch err {