@cryptotaxi247 / kubo / commits / 74e19eae6

tests + data dependency fix: `BytesSent` bug now completely fixed

Tests were added to ensure that the bug fix in commit 000fbd25 was correct. The tests caught an error where a peer's ledger was not properly locked when updating it in the `MessageSent()` function. The appropriate calls to lock the ledger were made, and the tests successfully passed. License: MIT Signed-off-by: David Grisham <dgrisham@mines.edu>

dgrisham committed Apr 24, 2017 at 20:33 UTC 74e19eae62b13411b1456f3534ccdbb7f739edfc
2 files changed +110
exchange/bitswap/bitswap_test.go
+107
@@ -11,6 +11,7 @@ import (
11 blocks "github.com/ipfs/go-ipfs/blocks"
12 blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
13 blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
14 + decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
15 tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
16 mockrouting "github.com/ipfs/go-ipfs/routing/mock"
17 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
@@ -489,3 +490,109 @@ func TestWantlistCleanup(t *testing.T) {
490 t.Fatal("should only have keys[0] in wantlist")
491 }
492 }
493 +
494 +func assertLedgerMatch(ra, rb *decision.Receipt) error {
495 + if ra.Sent != rb.Recv {
496 + return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d sent vs %d recvd", ra.Sent, rb.Recv)
497 + }
498 +
499 + if ra.Recv != rb.Sent {
500 + return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d recvd vs %d sent", ra.Recv, rb.Sent)
501 + }
502 +
503 + if ra.Exchanged != rb.Exchanged {
504 + return fmt.Errorf("mismatch in ledgers (exchanged blocks): %d vs %d ", ra.Exchanged, rb.Exchanged)
505 + }
506 +
507 + return nil
508 +}
509 +
510 +func TestBitswapBytesSentOneWay(t *testing.T) {
511 + net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
512 + sg := NewTestSessionGenerator(net)
513 + defer sg.Close()
514 + bg := blocksutil.NewBlockGenerator()
515 +
516 + t.Log("Test ledgers match when one peer sends block to another")
517 +
518 + instances := sg.Instances(2)
519 + blocks := bg.Blocks(1)
520 + err := instances[0].Exchange.HasBlock(blocks[0])
521 + if err != nil {
522 + t.Fatal(err)
523 + }
524 +
525 + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
526 + defer cancel()
527 + blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
528 + if err != nil {
529 + t.Fatal(err)
530 + }
531 +
532 + ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
533 + rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)
534 +
535 + err = assertLedgerMatch(ra, rb)
536 + if err != nil {
537 + t.Fatal(err)
538 + }
539 +
540 + t.Log(blk)
541 + for _, inst := range instances {
542 + err := inst.Exchange.Close()
543 + if err != nil {
544 + t.Fatal(err)
545 + }
546 + }
547 +}
548 +
549 +func TestBitswapBytesSentTwoWay(t *testing.T) {
550 + net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
551 + sg := NewTestSessionGenerator(net)
552 + defer sg.Close()
553 + bg := blocksutil.NewBlockGenerator()
554 +
555 + t.Log("Test ledgers match when two peers send one block to each other")
556 +
557 + instances := sg.Instances(2)
558 + blocks := bg.Blocks(2)
559 + err := instances[0].Exchange.HasBlock(blocks[0])
560 + if err != nil {
561 + t.Fatal(err)
562 + }
563 +
564 + err = instances[1].Exchange.HasBlock(blocks[1])
565 + if err != nil {
566 + t.Fatal(err)
567 + }
568 +
569 + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
570 + defer cancel()
571 + blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
572 + if err != nil {
573 + t.Fatal(err)
574 + }
575 +
576 + ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
577 + defer cancel()
578 + blk, err = instances[0].Exchange.GetBlock(ctx, blocks[1].Cid())
579 + if err != nil {
580 + t.Fatal(err)
581 + }
582 +
583 + ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
584 + rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)
585 +
586 + err = assertLedgerMatch(ra, rb)
587 + if err != nil {
588 + t.Fatal(err)
589 + }
590 +
591 + t.Log(blk)
592 + for _, inst := range instances {
593 + err := inst.Exchange.Close()
594 + if err != nil {
595 + t.Fatal(err)
596 + }
597 + }
598 +}
exchange/bitswap/decision/engine.go
+3
@@ -286,6 +286,9 @@ func (e *Engine) AddBlock(block blocks.Block) {
286
287 func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
288 l := e.findOrCreate(p)
289 + l.lk.Lock()
290 + defer l.lk.Unlock()
291 +
292 for _, block := range m.Blocks() {
293 l.SentBytes(len(block.RawData()))
294 l.wantList.Remove(block.Cid())