@cryptotaxi247 / kubo / commits / a5782efb1

add test for double getting a block

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Apr 27, 2016 at 15:16 UTC a5782efb11d7a26cd8648354712c5576830e68da
1 file changed +52
exchange/bitswap/bitswap_test.go
+52
@@ -308,3 +308,55 @@ func TestBasicBitswap(t *testing.T) {
308 }
309 }
310 }
311 +
312 +func TestDoubleGet(t *testing.T) {
313 + net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
314 + sg := NewTestSessionGenerator(net)
315 + defer sg.Close()
316 + bg := blocksutil.NewBlockGenerator()
317 +
318 + t.Log("Test a one node trying to get one block from another")
319 +
320 + instances := sg.Instances(2)
321 + blocks := bg.Blocks(1)
322 +
323 + ctx1, cancel1 := context.WithCancel(context.Background())
324 +
325 + blkch1, err := instances[1].Exchange.GetBlocks(ctx1, []key.Key{blocks[0].Key()})
326 + if err != nil {
327 + t.Fatal(err)
328 + }
329 +
330 + ctx2, cancel2 := context.WithCancel(context.Background())
331 + defer cancel2()
332 +
333 + blkch2, err := instances[1].Exchange.GetBlocks(ctx2, []key.Key{blocks[0].Key()})
334 + if err != nil {
335 + t.Fatal(err)
336 + }
337 +
338 + cancel1()
339 +
340 + _, ok := <-blkch1
341 + if ok {
342 + t.Fatal("expected channel to be closed")
343 + }
344 +
345 + err = instances[0].Exchange.HasBlock(blocks[0])
346 + if err != nil {
347 + t.Fatal(err)
348 + }
349 +
350 + blk, ok := <-blkch2
351 + if !ok {
352 + t.Fatal("expected to get the block here")
353 + }
354 + t.Log(blk)
355 +
356 + for _, inst := range instances {
357 + err := inst.Exchange.Close()
358 + if err != nil {
359 + t.Fatal(err)
360 + }
361 + }
362 +}