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 29, 2024
1 parent c0f555f commit d67a89d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/storer/internal/pinning/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
ErrInvalidPinCollectionItemSize = errInvalidPinCollectionSize
ErrPutterAlreadyClosed = errPutterAlreadyClosed
ErrCollectionRootAddressIsZero = errCollectionRootAddressIsZero
ErrDuplicatePinCollection = errDuplicatePinCollection
)

var NewUUID = newUUID
Expand Down
4 changes: 3 additions & 1 deletion pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
// errCollectionRootAddressIsZero is returned if the putter is closed with a zero
// swarm.Address. Root reference has to be set.
errCollectionRootAddressIsZero = errors.New("pin store: collection root address is zero")
// errDuplicatePinCollection is returned when attempted to pin the same file repeatedly
errDuplicatePinCollection = errors.New("pin store: duplicate pin collection")
)

// creates a new UUID and returns it as a byte slice
Expand Down Expand Up @@ -273,7 +275,7 @@ func (c *collectionPutter) Close(st internal.Storage, writer storage.Writer, roo

if has {
// trigger the Cleanup
return fmt.Errorf("pin store: duplicate collection")
return errDuplicatePinCollection
}

// Save the root pin reference.
Expand Down
24 changes: 23 additions & 1 deletion pkg/storer/internal/pinning/pinning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ func TestPinStore(t *testing.T) {
}
})

t.Run("duplicate collection", func(t *testing.T) {
root := chunktest.GenerateTestRandomChunk()
putter, err := pinstore.NewCollection(st)
if err != nil {
t.Fatal(err)
}

err = putter.Put(context.Background(), st, st.IndexStore(), root)
if err != nil {
t.Fatal(err)
}

err = putter.Close(st, st.IndexStore(), root.Address())
if err != nil {
t.Fatal(err)
}

err = putter.Close(st, st.IndexStore(), root.Address())
if err == nil || !errors.Is(err, pinstore.ErrDuplicatePinCollection) {
t.Fatalf("unexpected error during CLose, want: %v, got: %v", pinstore.ErrDuplicatePinCollection, err)
}
})

t.Run("zero address close", func(t *testing.T) {
root := chunktest.GenerateTestRandomChunk()
putter, err := pinstore.NewCollection(st)
Expand All @@ -292,7 +315,6 @@ func TestPinStore(t *testing.T) {
if !errors.Is(err, pinstore.ErrCollectionRootAddressIsZero) {
t.Fatalf("unexpected error on close, want: %v, got: %v", pinstore.ErrCollectionRootAddressIsZero, err)
}

})
}

Expand Down

0 comments on commit d67a89d

Please sign in to comment.