Skip to content

Commit

Permalink
Tests: fix tests after changes of routing options
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Nov 19, 2024
1 parent e235c72 commit a6a72fc
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 100 deletions.
24 changes: 13 additions & 11 deletions bitswap/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"testing"
"time"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-test/random"
protocol "github.com/libp2p/go-libp2p/core/protocol"

"github.com/ipfs/boxo/bitswap"
bsnet "github.com/ipfs/boxo/bitswap/network"
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
tn "github.com/ipfs/boxo/bitswap/testnet"
mockrouting "github.com/ipfs/boxo/routing/mock"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
delay "github.com/ipfs/go-ipfs-delay"
"github.com/ipfs/go-test/random"
protocol "github.com/libp2p/go-libp2p/core/protocol"
)

type fetchFunc func(b *testing.B, bs *bitswap.Bitswap, ks []cid.Cid)
Expand Down Expand Up @@ -134,6 +134,7 @@ func BenchmarkFetchFromOldBitswap(b *testing.B) {
benchmarkLog = nil
fixedDelay := delay.Fixed(10 * time.Millisecond)
bstoreLatency := time.Duration(0)
router := mockrouting.NewServer()

for _, bch := range mixedBenches {
b.Run(bch.name, func(b *testing.B) {
Expand All @@ -148,10 +149,10 @@ func BenchmarkFetchFromOldBitswap(b *testing.B) {
oldProtocol := []protocol.ID{bsnet.ProtocolBitswapOneOne}
oldNetOpts := []bsnet.NetOpt{bsnet.SupportedProtocols(oldProtocol)}
oldBsOpts := []bitswap.Option{bitswap.SetSendDontHaves(false)}
oldNodeGenerator := testinstance.NewTestInstanceGenerator(net, oldNetOpts, oldBsOpts)
oldNodeGenerator := testinstance.NewTestInstanceGenerator(net, router, oldNetOpts, oldBsOpts)

// Regular new Bitswap node
newNodeGenerator := testinstance.NewTestInstanceGenerator(net, nil, nil)
newNodeGenerator := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
var instances []testinstance.Instance

// Create new nodes (fetchers + seeds)
Expand Down Expand Up @@ -295,7 +296,8 @@ func BenchmarkDatacenterMultiLeechMultiSeed(b *testing.B) {
for i := 0; i < b.N; i++ {
net := tn.RateLimitedVirtualNetwork(d, rateLimitGenerator)

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

instances := ig.Instances(numnodes)
Expand All @@ -312,8 +314,8 @@ func BenchmarkDatacenterMultiLeechMultiSeed(b *testing.B) {
func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, bstoreLatency time.Duration, df distFunc, ff fetchFunc) {
for i := 0; i < b.N; i++ {
net := tn.VirtualNetwork(d)

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)

instances := ig.Instances(numnodes)
rootBlock := random.BlocksOfSize(1, rootBlockSize)
Expand All @@ -327,8 +329,8 @@ func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, b
func subtestDistributeAndFetchRateLimited(b *testing.B, numnodes, numblks int, d delay.D, rateLimitGenerator tn.RateLimitGenerator, blockSize int64, bstoreLatency time.Duration, df distFunc, ff fetchFunc) {
for i := 0; i < b.N; i++ {
net := tn.RateLimitedVirtualNetwork(d, rateLimitGenerator)

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

instances := ig.Instances(numnodes)
Expand Down
66 changes: 41 additions & 25 deletions bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ipfs/boxo/bitswap/server"
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
tn "github.com/ipfs/boxo/bitswap/testnet"
mockrouting "github.com/ipfs/boxo/routing/mock"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
detectrace "github.com/ipfs/go-detect-race"
Expand Down Expand Up @@ -51,7 +52,8 @@ const kNetworkDelay = 0 * time.Millisecond

func TestClose(t *testing.T) {
vnet := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(vnet, router, nil, nil)
defer ig.Close()
block := random.BlocksOfSize(1, blockSize)[0]
bitswap := ig.Next()
Expand All @@ -65,12 +67,13 @@ func TestClose(t *testing.T) {

func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

block := blocks.NewBlock([]byte("block"))
pinfo := p2ptestutil.RandTestBogusIdentityOrFatal(t)
err := ig.Routing().Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network
err := router.Client(pinfo).Provide(context.Background(), block.Cid(), true) // but not on network
if err != nil {
t.Fatal(err)
}
Expand All @@ -90,7 +93,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
block := blocks.NewBlock([]byte("block"))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

peers := ig.Instances(2)
Expand Down Expand Up @@ -119,7 +123,8 @@ func TestDoesNotProvideWhenConfiguredNotTo(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
block := blocks.NewBlock([]byte("block"))
bsOpts := []bitswap.Option{bitswap.ProviderSearchDelay(50 * time.Millisecond)}
ig := testinstance.NewTestInstanceGenerator(net, nil, bsOpts)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, bsOpts)
defer ig.Close()

hasBlock := ig.Next()
Expand Down Expand Up @@ -153,7 +158,8 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
bsMessage := bsmsg.New(true)
bsMessage.AddBlock(block)

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

peers := ig.Instances(2)
Expand All @@ -168,7 +174,7 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

doesNotWantBlock.Exchange.ReceiveMessage(ctx, hasBlock.Peer, bsMessage)
doesNotWantBlock.Exchange.ReceiveMessage(ctx, hasBlock.Identity.ID(), bsMessage)

blockInStore, err := doesNotWantBlock.Blockstore().Has(ctx, block.Cid())
if err != nil || blockInStore {
Expand All @@ -187,7 +193,8 @@ func TestPendingBlockAdded(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
sessionBroadcastWantCapacity := 4

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

instance := ig.Instances(1)[0]
Expand Down Expand Up @@ -276,7 +283,8 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
t.SkipNow()
}
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, []bitswap.Option{
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, []bitswap.Option{
bitswap.TaskWorkerCount(5),
bitswap.EngineTaskWorkerCount(5),
bitswap.MaxOutstandingBytesPerPeer(1 << 20),
Expand Down Expand Up @@ -332,15 +340,16 @@ func TestSendToWantingPeer(t *testing.T) {
}

net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

peers := ig.Instances(2)
peerA := peers[0]
peerB := peers[1]

t.Logf("Session %v\n", peerA.Peer)
t.Logf("Session %v\n", peerB.Peer)
t.Logf("Session %v\n", peerA.Identity.ID())
t.Logf("Session %v\n", peerB.Identity.ID())

waitTime := time.Second * 5

Expand Down Expand Up @@ -369,7 +378,8 @@ func TestSendToWantingPeer(t *testing.T) {

func TestEmptyKey(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()
bs := ig.Instances(1)[0].Exchange

Expand Down Expand Up @@ -402,7 +412,8 @@ func assertStat(t *testing.T, st *bitswap.Stat, sblks, rblks, sdata, rdata uint6

func TestBasicBitswap(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

t.Log("Test a one node trying to get one block from another")
Expand All @@ -426,7 +437,7 @@ func TestBasicBitswap(t *testing.T) {
// When second peer receives block, it should send out a cancel, so third
// peer should no longer keep second peer's want
if err = tu.WaitFor(ctx, func() error {
if len(instances[2].Exchange.WantlistForPeer(instances[1].Peer)) != 0 {
if len(instances[2].Exchange.WantlistForPeer(instances[1].Identity.ID())) != 0 {
return errors.New("should have no items in other peers wantlist")
}
if len(instances[1].Exchange.GetWantlist()) != 0 {
Expand Down Expand Up @@ -473,7 +484,8 @@ func TestBasicBitswap(t *testing.T) {

func TestDoubleGet(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

t.Log("Test a one node trying to get one block from another")
Expand Down Expand Up @@ -516,7 +528,7 @@ func TestDoubleGet(t *testing.T) {
}
t.Log(blk)
case <-time.After(time.Second * 5):
p1wl := instances[0].Exchange.WantlistForPeer(instances[1].Peer)
p1wl := instances[0].Exchange.WantlistForPeer(instances[1].Identity.ID())
if len(p1wl) != 1 {
t.Logf("wantlist view didnt have 1 item (had %d)", len(p1wl))
} else if !p1wl[0].Equals(blocks[0].Cid()) {
Expand All @@ -537,7 +549,8 @@ func TestDoubleGet(t *testing.T) {

func TestWantlistCleanup(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

instances := ig.Instances(2)
Expand Down Expand Up @@ -658,7 +671,8 @@ func newReceipt(sent, recv, exchanged uint64) *server.Receipt {

func TestBitswapLedgerOneWay(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

t.Log("Test ledgers match when one peer sends block to another")
Expand All @@ -674,8 +688,8 @@ func TestBitswapLedgerOneWay(t *testing.T) {
t.Fatal(err)
}

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

// compare peer ledger receipts
err = assertLedgerMatch(ra, rb)
Expand Down Expand Up @@ -706,7 +720,8 @@ func TestBitswapLedgerOneWay(t *testing.T) {

func TestBitswapLedgerTwoWay(t *testing.T) {
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
router := mockrouting.NewServer()
ig := testinstance.NewTestInstanceGenerator(net, router, nil, nil)
defer ig.Close()

t.Log("Test ledgers match when two peers send one block to each other")
Expand All @@ -730,8 +745,8 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
t.Fatal(err)
}

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

// compare peer ledger receipts
err = assertLedgerMatch(ra, rb)
Expand Down Expand Up @@ -794,8 +809,9 @@ func (tsl *testingScoreLedger) Stop() {
func TestWithScoreLedger(t *testing.T) {
tsl := newTestingScoreLedger()
net := tn.VirtualNetwork(delay.Fixed(kNetworkDelay))
router := mockrouting.NewServer()
bsOpts := []bitswap.Option{bitswap.WithScoreLedger(tsl)}
ig := testinstance.NewTestInstanceGenerator(net, nil, bsOpts)
ig := testinstance.NewTestInstanceGenerator(net, router, nil, bsOpts)
defer ig.Close()
i := ig.Next()
defer i.Exchange.Close()
Expand Down
Loading

0 comments on commit a6a72fc

Please sign in to comment.