Skip to content

Commit

Permalink
chore: extra unit test for compaction (#4400)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Oct 13, 2023
1 parent 847a902 commit 783d300
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions pkg/storer/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ package storer_test
import (
"bytes"
"context"
"os"
"testing"
"time"

"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/postage"
postagetesting "github.com/ethersphere/bee/pkg/postage/testing"
pullerMock "github.com/ethersphere/bee/pkg/puller/mock"
Expand All @@ -24,14 +22,14 @@ import (
// The first batch is then expired, causing free slots to accumulate in sharky.
// Next, sharky is compacted, after which, it is tested that valid chunks can still be retrieved.
func TestCompact(t *testing.T) {
t.Parallel()

baseAddr := swarm.RandAddress(t)
ctx := context.Background()
basePath := t.TempDir()

opts := dbTestOps(baseAddr, 10_000, nil, nil, time.Second)
opts.CacheCapacity = 0
opts.Logger = log.NewLogger("test", log.WithSink(os.Stdout))

st, err := storer.New(ctx, basePath, opts)
if err != nil {
Expand Down Expand Up @@ -113,3 +111,80 @@ func TestCompact(t *testing.T) {
t.Fatal(err)
}
}

// TestCompactNoEvictions compacts a store that has no free slots to ensure that no chunks get lost.
func TestCompactNoEvictions(t *testing.T) {
t.Parallel()

baseAddr := swarm.RandAddress(t)
ctx := context.Background()
basePath := t.TempDir()

opts := dbTestOps(baseAddr, 10_000, nil, nil, time.Second)
opts.CacheCapacity = 0

st, err := storer.New(ctx, basePath, opts)
if err != nil {
t.Fatal(err)
}
st.StartReserveWorker(ctx, pullerMock.NewMockRateReporter(0), networkRadiusFunc(0))

var chunks []swarm.Chunk
batches := []*postage.Batch{postagetesting.MustNewBatch(), postagetesting.MustNewBatch(), postagetesting.MustNewBatch()}

putter := st.ReservePutter()

for b := 0; b < len(batches); b++ {
for i := uint64(0); i < 100; i++ {
ch := chunk.GenerateTestRandomChunk()
ch = ch.WithStamp(postagetesting.MustNewBatchStamp(batches[b].ID))
chunks = append(chunks, ch)
err := putter.Put(ctx, ch)
if err != nil {
t.Fatal(err)
}
}
}

if err := st.Close(); err != nil {
t.Fatal(err)
}

err = storer.Compact(ctx, basePath, opts, false)
if err != nil {
t.Fatal(err)
}

st, err = storer.New(ctx, basePath, opts)
if err != nil {
t.Fatal(err)
}

putter = st.ReservePutter()
for i := uint64(0); i < 100; i++ {
ch := chunk.GenerateTestRandomChunk()
ch = ch.WithStamp(postagetesting.MustNewBatchStamp(batches[0].ID))
chunks = append(chunks, ch)
err := putter.Put(ctx, ch)
if err != nil {
t.Fatal(err)
}
}

for _, ch := range chunks {
has, err := st.ReserveHas(ch.Address(), ch.Stamp().BatchID())
if err != nil {
t.Fatal(err)
}

if !has {
t.Fatal("store should have chunk")
}

checkSaved(t, st, ch, true, true)
}

if err := st.Close(); err != nil {
t.Fatal(err)
}
}

0 comments on commit 783d300

Please sign in to comment.