Skip to content

Commit

Permalink
test(client): add tests of block peers
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jun 30, 2023
1 parent 545cdf1 commit be83933
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions bitswap/client/bitswap_with_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ipfs/boxo/bitswap"
"github.com/ipfs/boxo/bitswap/client/internal/session"
"github.com/ipfs/boxo/bitswap/client/traceability"
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
tn "github.com/ipfs/boxo/bitswap/testnet"
"github.com/ipfs/boxo/internal/test"
Expand All @@ -17,6 +18,7 @@ import (
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
delay "github.com/ipfs/go-ipfs-delay"
tu "github.com/libp2p/go-libp2p-testing/etc"
"github.com/libp2p/go-libp2p/core/peer"
)

func getVirtualNetwork() tn.Network {
Expand Down Expand Up @@ -71,16 +73,32 @@ func TestBasicSessions(t *testing.T) {
if !blkout.Cid().Equals(block.Cid()) {
t.Fatal("got wrong block")
}

traceBlock, ok := blkout.(traceability.Block)
if !ok {
t.Fatal("did not get tracable block")
}

if traceBlock.From != b.Peer {
t.Fatal("should have received block from peer B, did not")
}
}

func assertBlockLists(got, exp []blocks.Block) error {
func assertBlockListsFrom(from peer.ID, got, exp []blocks.Block) error {
if len(got) != len(exp) {
return fmt.Errorf("got wrong number of blocks, %d != %d", len(got), len(exp))
}

h := cid.NewSet()
for _, b := range got {
h.Add(b.Cid())
traceableBlock, ok := b.(traceability.Block)
if !ok {
return fmt.Errorf("not a traceable block: %s", b.Cid())
}
if traceableBlock.From != from {
return fmt.Errorf("incorrect peer sent block, expect %s, got %s", from, traceableBlock.From)
}
}
for _, b := range exp {
if !h.Has(b.Cid()) {
Expand Down Expand Up @@ -133,7 +151,7 @@ func TestSessionBetweenPeers(t *testing.T) {
for b := range ch {
got = append(got, b)
}
if err := assertBlockLists(got, blks[i*10:(i+1)*10]); err != nil {
if err := assertBlockListsFrom(inst[0].Peer, got, blks[i*10:(i+1)*10]); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -192,7 +210,7 @@ func TestSessionSplitFetch(t *testing.T) {
for b := range ch {
got = append(got, b)
}
if err := assertBlockLists(got, blks[i*10:(i+1)*10]); err != nil {
if err := assertBlockListsFrom(inst[i].Peer, got, blks[i*10:(i+1)*10]); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -238,7 +256,7 @@ func TestFetchNotConnected(t *testing.T) {
for b := range ch {
got = append(got, b)
}
if err := assertBlockLists(got, blks); err != nil {
if err := assertBlockListsFrom(other.Peer, got, blks); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -289,7 +307,7 @@ func TestFetchAfterDisconnect(t *testing.T) {
got = append(got, b)
}

if err := assertBlockLists(got, blks[:5]); err != nil {
if err := assertBlockListsFrom(peerA.Peer, got, blks[:5]); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -318,7 +336,7 @@ func TestFetchAfterDisconnect(t *testing.T) {
}
}

if err := assertBlockLists(got, blks); err != nil {
if err := assertBlockListsFrom(peerA.Peer, got, blks); err != nil {
t.Fatal(err)
}
}
Expand Down

0 comments on commit be83933

Please sign in to comment.