From d67a89d6789285eefdecfff98c28ac3e2912e4bf Mon Sep 17 00:00:00 2001 From: notanatol Date: Mon, 29 Jan 2024 12:08:55 -0600 Subject: [PATCH] fix: add test --- pkg/storer/internal/pinning/export_test.go | 1 + pkg/storer/internal/pinning/pinning.go | 4 +++- pkg/storer/internal/pinning/pinning_test.go | 24 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pkg/storer/internal/pinning/export_test.go b/pkg/storer/internal/pinning/export_test.go index f80bbd3c945..79e5864dfba 100644 --- a/pkg/storer/internal/pinning/export_test.go +++ b/pkg/storer/internal/pinning/export_test.go @@ -23,6 +23,7 @@ var ( ErrInvalidPinCollectionItemSize = errInvalidPinCollectionSize ErrPutterAlreadyClosed = errPutterAlreadyClosed ErrCollectionRootAddressIsZero = errCollectionRootAddressIsZero + ErrDuplicatePinCollection = errDuplicatePinCollection ) var NewUUID = newUUID diff --git a/pkg/storer/internal/pinning/pinning.go b/pkg/storer/internal/pinning/pinning.go index 2a4c4e856a7..8fb25ba92ad 100644 --- a/pkg/storer/internal/pinning/pinning.go +++ b/pkg/storer/internal/pinning/pinning.go @@ -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 @@ -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. diff --git a/pkg/storer/internal/pinning/pinning_test.go b/pkg/storer/internal/pinning/pinning_test.go index 096b69b0018..017b135f6f8 100644 --- a/pkg/storer/internal/pinning/pinning_test.go +++ b/pkg/storer/internal/pinning/pinning_test.go @@ -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) @@ -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) } - }) }