Skip to content

Commit

Permalink
tests + data dependency fix: BytesSent bug now completely fixed
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dgrisham committed Apr 25, 2017
1 parent 7c1ecaf commit d7adb82
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
107 changes: 107 additions & 0 deletions exchange/bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
blocks "github.com/ipfs/go-ipfs/blocks"
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
decision "github.com/ipfs/go-ipfs/exchange/bitswap/decision"
tn "github.com/ipfs/go-ipfs/exchange/bitswap/testnet"
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
delay "github.com/ipfs/go-ipfs/thirdparty/delay"
Expand Down Expand Up @@ -489,3 +490,109 @@ func TestWantlistCleanup(t *testing.T) {
t.Fatal("should only have keys[0] in wantlist")
}
}

func assertLedgerMatch(ra, rb *decision.Receipt) error {
if ra.Sent != rb.Recv {
return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d sent vs %d recvd", ra.Sent, rb.Recv)
}

if ra.Recv != rb.Sent {
return fmt.Errorf("mismatch in ledgers (exchanged bytes): %d recvd vs %d sent", ra.Recv, rb.Sent)
}

if ra.Exchanged != rb.Exchanged {
return fmt.Errorf("mismatch in ledgers (exchanged blocks): %d vs %d ", ra.Exchanged, rb.Exchanged)
}

return nil
}

func TestBitswapBytesSentOneWay(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewTestSessionGenerator(net)
defer sg.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test ledgers match when one peer sends block to another")

instances := sg.Instances(2)
blocks := bg.Blocks(1)
err := instances[0].Exchange.HasBlock(blocks[0])
if err != nil {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
if err != nil {
t.Fatal(err)
}

ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)

err = assertLedgerMatch(ra, rb)
if err != nil {
t.Fatal(err)
}

t.Log(blk)
for _, inst := range instances {
err := inst.Exchange.Close()
if err != nil {
t.Fatal(err)
}
}
}

func TestBitswapBytesSentTwoWay(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewTestSessionGenerator(net)
defer sg.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test ledgers match when two peers send one block to each other")

instances := sg.Instances(2)
blocks := bg.Blocks(2)
err := instances[0].Exchange.HasBlock(blocks[0])
if err != nil {
t.Fatal(err)
}

err = instances[1].Exchange.HasBlock(blocks[1])
if err != nil {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Cid())
if err != nil {
t.Fatal(err)
}

ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
blk, err = instances[0].Exchange.GetBlock(ctx, blocks[1].Cid())
if err != nil {
t.Fatal(err)
}

ra := instances[0].Exchange.LedgerForPeer(instances[1].Peer)
rb := instances[1].Exchange.LedgerForPeer(instances[0].Peer)

err = assertLedgerMatch(ra, rb)
if err != nil {
t.Fatal(err)
}

t.Log(blk)
for _, inst := range instances {
err := inst.Exchange.Close()
if err != nil {
t.Fatal(err)
}
}
}
3 changes: 3 additions & 0 deletions exchange/bitswap/decision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ func (e *Engine) AddBlock(block blocks.Block) {

func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
l := e.findOrCreate(p)
l.lk.Lock()
defer l.lk.Unlock()

for _, block := range m.Blocks() {
l.SentBytes(len(block.RawData()))
l.wantList.Remove(block.Cid())
Expand Down

0 comments on commit d7adb82

Please sign in to comment.