Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use ipfs/go-test to consolidate code to generate test data #638

Merged
merged 8 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following emojis are used to highlight certain changes:
- `bitswap/server` minor memory use and performance improvements
- `bitswap` unify logger names to use uniform format bitswap/path/pkgname
- `gateway` now always returns meaningful cache-control headers for generated HTML listings of UnixFS directories
- generate random test data using `ipfs/go-test` instead of internal util code

### Removed

Expand Down
16 changes: 8 additions & 8 deletions bitswap/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"testing"
"time"

"github.com/ipfs/boxo/bitswap/internal/testutil"
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"
Expand Down Expand Up @@ -169,8 +169,8 @@ func BenchmarkFetchFromOldBitswap(b *testing.B) {
testinstance.ConnectInstances(instances)

// Generate blocks, with a smaller root block
rootBlock := testutil.GenerateBlocksOfSize(1, rootBlockSize)
blocks := testutil.GenerateBlocksOfSize(bch.blockCount, stdBlockSize)
rootBlock := random.BlocksOfSize(1, rootBlockSize)
blocks := random.BlocksOfSize(bch.blockCount, stdBlockSize)
blocks[0] = rootBlock[0]

// Run the distribution
Expand Down Expand Up @@ -300,7 +300,7 @@ func BenchmarkDatacenterMultiLeechMultiSeed(b *testing.B) {
defer ig.Close()

instances := ig.Instances(numnodes)
blocks := testutil.GenerateBlocksOfSize(numblks, blockSize)
blocks := random.BlocksOfSize(numblks, int(blockSize))
runDistributionMulti(b, instances[:3], instances[3:], blocks, bstoreLatency, df, ff)
}
})
Expand All @@ -317,8 +317,8 @@ func subtestDistributeAndFetch(b *testing.B, numnodes, numblks int, d delay.D, b
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)

instances := ig.Instances(numnodes)
rootBlock := testutil.GenerateBlocksOfSize(1, rootBlockSize)
blocks := testutil.GenerateBlocksOfSize(numblks, stdBlockSize)
rootBlock := random.BlocksOfSize(1, rootBlockSize)
blocks := random.BlocksOfSize(numblks, stdBlockSize)
blocks[0] = rootBlock[0]
runDistribution(b, instances, blocks, bstoreLatency, df, ff)
ig.Close()
Expand All @@ -333,8 +333,8 @@ func subtestDistributeAndFetchRateLimited(b *testing.B, numnodes, numblks int, d
defer ig.Close()

instances := ig.Instances(numnodes)
rootBlock := testutil.GenerateBlocksOfSize(1, rootBlockSize)
blocks := testutil.GenerateBlocksOfSize(numblks, blockSize)
rootBlock := random.BlocksOfSize(1, rootBlockSize)
blocks := random.BlocksOfSize(numblks, int(blockSize))
blocks[0] = rootBlock[0]
runDistribution(b, instances, blocks, bstoreLatency, df, ff)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package blockpresencemanager
import (
"testing"

"github.com/ipfs/boxo/bitswap/internal/testutil"
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-test/random"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -18,111 +19,72 @@ const (
func TestBlockPresenceManager(t *testing.T) {
bpm := New()

p := testutil.GeneratePeers(1)[0]
cids := testutil.GenerateCids(2)
p := random.Peers(1)[0]
cids := random.Cids(2)
c0 := cids[0]
c1 := cids[1]

// Nothing stored yet, both PeerHasBlock and PeerDoesNotHaveBlock should
// return false
if bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.False(t, bpm.PeerHasBlock(p, c0), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c0), expDoesNotHaveFalseMsg)

// HAVE cid0 / DONT_HAVE cid1
bpm.ReceiveFrom(p, []cid.Cid{c0}, []cid.Cid{c1})

// Peer has received HAVE for cid0
if !bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasTrueMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.True(t, bpm.PeerHasBlock(p, c0), expHasTrueMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c0), expDoesNotHaveFalseMsg)

// Peer has received DONT_HAVE for cid1
if !bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveTrueMsg)
}
if bpm.PeerHasBlock(p, c1) {
t.Fatal(expHasFalseMsg)
}
require.True(t, bpm.PeerDoesNotHaveBlock(p, c1), expDoesNotHaveTrueMsg)
require.False(t, bpm.PeerHasBlock(p, c1), expHasFalseMsg)

// HAVE cid1 / DONT_HAVE cid0
bpm.ReceiveFrom(p, []cid.Cid{c1}, []cid.Cid{c0})

// DONT_HAVE cid0 should NOT over-write earlier HAVE cid0
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if !bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasTrueMsg)
}
require.False(t, bpm.PeerDoesNotHaveBlock(p, c0), expDoesNotHaveFalseMsg)
require.True(t, bpm.PeerHasBlock(p, c0), expHasTrueMsg)

// HAVE cid1 should over-write earlier DONT_HAVE cid1
if !bpm.PeerHasBlock(p, c1) {
t.Fatal(expHasTrueMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.True(t, bpm.PeerHasBlock(p, c1), expHasTrueMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c1), expDoesNotHaveFalseMsg)

// Remove cid0
bpm.RemoveKeys([]cid.Cid{c0})

// Nothing stored, both PeerHasBlock and PeerDoesNotHaveBlock should
// return false
if bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.False(t, bpm.PeerHasBlock(p, c0), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c0), expDoesNotHaveFalseMsg)

// Remove cid1
bpm.RemoveKeys([]cid.Cid{c1})

// Nothing stored, both PeerHasBlock and PeerDoesNotHaveBlock should
// return false
if bpm.PeerHasBlock(p, c1) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.False(t, bpm.PeerHasBlock(p, c1), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c1), expDoesNotHaveFalseMsg)

bpm.ReceiveFrom(p, []cid.Cid{c0}, []cid.Cid{c1})
if !bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveTrueMsg)
}
require.True(t, bpm.PeerHasBlock(p, c0), expHasTrueMsg)
require.True(t, bpm.PeerDoesNotHaveBlock(p, c1), expDoesNotHaveTrueMsg)

bpm.RemovePeer(p)
if bpm.PeerHasBlock(p, c0) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c0) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if bpm.PeerHasBlock(p, c1) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.False(t, bpm.PeerHasBlock(p, c0), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c0), expDoesNotHaveFalseMsg)
require.False(t, bpm.PeerHasBlock(p, c1), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p, c1), expDoesNotHaveFalseMsg)
}

func TestAddRemoveMulti(t *testing.T) {
bpm := New()

peers := testutil.GeneratePeers(2)
peers := random.Peers(2)
p0 := peers[0]
p1 := peers[1]
cids := testutil.GenerateCids(3)
cids := random.Cids(3)
c0 := cids[0]
c1 := cids[1]
c2 := cids[2]
Expand All @@ -136,78 +98,46 @@ func TestAddRemoveMulti(t *testing.T) {
// - HAVE cid0
// - HAVE cid1
// - DONT_HAVE cid2
if !bpm.PeerHasBlock(p0, c0) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerHasBlock(p0, c1) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerDoesNotHaveBlock(p0, c2) {
t.Fatal(expDoesNotHaveTrueMsg)
}
require.True(t, bpm.PeerHasBlock(p0, c0), expHasTrueMsg)
require.True(t, bpm.PeerHasBlock(p0, c1), expHasTrueMsg)
require.True(t, bpm.PeerDoesNotHaveBlock(p0, c2), expDoesNotHaveTrueMsg)

// Peer 1 should end up with
// - HAVE cid1
// - HAVE cid2
// - DONT_HAVE cid0
if !bpm.PeerHasBlock(p1, c1) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerHasBlock(p1, c2) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerDoesNotHaveBlock(p1, c0) {
t.Fatal(expDoesNotHaveTrueMsg)
}
require.True(t, bpm.PeerHasBlock(p1, c1), expHasTrueMsg)
require.True(t, bpm.PeerHasBlock(p1, c2), expHasTrueMsg)
require.True(t, bpm.PeerDoesNotHaveBlock(p1, c0), expDoesNotHaveTrueMsg)

// Remove cid1 and cid2. Should end up with
// Peer 0: HAVE cid0
// Peer 1: DONT_HAVE cid0
bpm.RemoveKeys([]cid.Cid{c1, c2})
if !bpm.PeerHasBlock(p0, c0) {
t.Fatal(expHasTrueMsg)
}
if !bpm.PeerDoesNotHaveBlock(p1, c0) {
t.Fatal(expDoesNotHaveTrueMsg)
}
require.True(t, bpm.PeerHasBlock(p0, c0), expHasTrueMsg)
require.True(t, bpm.PeerDoesNotHaveBlock(p1, c0), expDoesNotHaveTrueMsg)

// The other keys should have been cleared, so both HasBlock() and
// DoesNotHaveBlock() should return false
if bpm.PeerHasBlock(p0, c1) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p0, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if bpm.PeerHasBlock(p0, c2) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p0, c2) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if bpm.PeerHasBlock(p1, c1) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p1, c1) {
t.Fatal(expDoesNotHaveFalseMsg)
}
if bpm.PeerHasBlock(p1, c2) {
t.Fatal(expHasFalseMsg)
}
if bpm.PeerDoesNotHaveBlock(p1, c2) {
t.Fatal(expDoesNotHaveFalseMsg)
}
require.False(t, bpm.PeerHasBlock(p0, c1), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p0, c1), expDoesNotHaveFalseMsg)
require.False(t, bpm.PeerHasBlock(p0, c2), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p0, c2), expDoesNotHaveFalseMsg)
require.False(t, bpm.PeerHasBlock(p1, c1), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p1, c1), expDoesNotHaveFalseMsg)
require.False(t, bpm.PeerHasBlock(p1, c2), expHasFalseMsg)
require.False(t, bpm.PeerDoesNotHaveBlock(p1, c2), expDoesNotHaveFalseMsg)
}

func TestAllPeersDoNotHaveBlock(t *testing.T) {
bpm := New()

peers := testutil.GeneratePeers(3)
peers := random.Peers(3)
p0 := peers[0]
p1 := peers[1]
p2 := peers[2]

cids := testutil.GenerateCids(3)
cids := random.Cids(3)
c0 := cids[0]
c1 := cids[1]
c2 := cids[2]
Expand Down Expand Up @@ -248,11 +178,7 @@ func TestAllPeersDoNotHaveBlock(t *testing.T) {
}

for i, tc := range testcases {
if !testutil.MatchKeysIgnoreOrder(
bpm.AllPeersDoNotHaveBlock(tc.peers, tc.ks),
tc.exp,
) {
t.Fatalf("test case %d failed: expected matching keys", i)
}
require.ElementsMatchf(t, bpm.AllPeersDoNotHaveBlock(tc.peers, tc.ks), tc.exp,
"test case %d failed: expected matching keys", i)
}
}
Loading
Loading