Skip to content

Commit

Permalink
fix: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Jan 27, 2024
1 parent 13ab69e commit c34b9a5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ func (c *collectionPutter) Close(st internal.Storage, writer storage.Writer, roo
}

if has {
return fmt.Errorf("pin store: duplicate collection: %w", err) // will trigger the Cleanup
// trigger the Cleanup
return fmt.Errorf("pin store: duplicate collection")
}

// Save the root pin reference.
Expand Down
45 changes: 45 additions & 0 deletions pkg/storer/pinstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,51 @@ func testPinStore(t *testing.T, newStorer func() (*storer.DB, error)) {
verifyPinCollection(t, lstore.Repo(), testCases[0].chunks[0], testCases[0].chunks, true)
})
})

t.Run("duplicate parallel upload does not leave orphaned chunks", func(t *testing.T) {
chunks := chunktesting.GenerateTestRandomChunks(4)

session1, err := lstore.NewCollection(context.TODO())
if err != nil {
t.Fatalf("NewCollection(...): unexpected error: %v", err)
}

session2, err := lstore.NewCollection(context.TODO())
if err != nil {
t.Fatalf("NewCollection2(...): unexpected error: %v", err)
}

for _, ch := range chunks {
err := session2.Put(context.TODO(), ch)
if err != nil {
t.Fatalf("session2.Put(...): unexpected error: %v", err)
t.Fatal(err)
}

err = session1.Put(context.TODO(), ch)
if err != nil {
t.Fatalf("session1.Put(...): unexpected error: %v", err)
t.Fatal(err)
}
}

err = session1.Done(chunks[0].Address())
if err != nil {
t.Fatalf("session1.Done(...): unexpected error: %v", err)
}

err = session2.Done(chunks[0].Address())
if err == nil {
t.Fatalf("session2.Done(...): expected error, got nil")
}

if err := session2.Cleanup(); err != nil {
t.Fatalf("session2.Done(...): unexpected error: %v", err)
}

verifyPinCollection(t, lstore.Repo(), chunks[0], chunks, true)
verifyChunkRefCount(t, lstore.Repo(), chunks)
})
}

func TestPinStore(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/storer/storer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ethersphere/bee/pkg/storage/inmemchunkstore"
"github.com/ethersphere/bee/pkg/storage/migration"
"github.com/ethersphere/bee/pkg/storer"
cs "github.com/ethersphere/bee/pkg/storer/internal/chunkstore"
pinstore "github.com/ethersphere/bee/pkg/storer/internal/pinning"
"github.com/ethersphere/bee/pkg/storer/internal/upload"
localmigration "github.com/ethersphere/bee/pkg/storer/migration"
Expand All @@ -44,7 +45,26 @@ func verifyChunks(
t.Fatalf("unexpected chunk has state: want %t have %t", has, hasFound)
}
}
}

func verifyChunkRefCount(
t *testing.T,
repo storage.Repository,
chunks []swarm.Chunk,
) {
t.Helper()

for _, ch := range chunks {
repo.IndexStore().Iterate(storage.Query{
Factory: func() storage.Item { return new(cs.RetrievalIndexItem) },
}, func(r storage.Result) (bool, error) {
entry := r.Entry.(*cs.RetrievalIndexItem)
if entry.Address.Equal(ch.Address()) && entry.RefCnt != 1 {
t.Errorf("chunk %s has refCnt=%d", ch.Address(), entry.RefCnt)
}
return false, nil
})
}
}

func verifySessionInfo(
Expand Down

0 comments on commit c34b9a5

Please sign in to comment.