2022-01-20 15:39:16 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-11-17 07:58:04 +00:00
|
|
|
"bytes"
|
2023-05-28 19:37:37 +00:00
|
|
|
"context"
|
2022-01-20 15:39:16 +00:00
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2022-01-20 15:39:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlobovniczaIterate(t *testing.T) {
|
|
|
|
filename := filepath.Join(t.TempDir(), "blob")
|
|
|
|
b := New(WithPath(filename))
|
2024-10-21 08:20:17 +00:00
|
|
|
require.NoError(t, b.Open(context.Background()))
|
2024-10-21 13:27:28 +00:00
|
|
|
require.NoError(t, b.Init(context.Background()))
|
2022-01-20 15:39:16 +00:00
|
|
|
|
|
|
|
data := [][]byte{{0, 1, 2, 3}, {5, 6, 7, 8}}
|
2022-05-31 17:00:41 +00:00
|
|
|
addr := oidtest.Address()
|
2023-06-20 08:24:14 +00:00
|
|
|
_, err := b.Put(context.Background(), PutPrm{addr: addr, objData: data[0]})
|
2022-01-20 15:39:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, b.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
buck := tx.Bucket(bucketKeyFromBounds(firstBucketBound))
|
|
|
|
return buck.Put([]byte("invalid address"), data[1])
|
|
|
|
}))
|
|
|
|
|
|
|
|
seen := make([][]byte, 0, 2)
|
|
|
|
inc := func(e IterationElement) error {
|
2023-11-17 07:58:04 +00:00
|
|
|
seen = append(seen, bytes.Clone(e.data))
|
2022-01-20 15:39:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err = b.Iterate(context.Background(), IteratePrm{handler: inc})
|
2022-01-20 15:39:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, seen, data)
|
|
|
|
|
|
|
|
seen = seen[:0]
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err = b.Iterate(context.Background(), IteratePrm{handler: inc, decodeAddresses: true})
|
2022-01-20 15:39:16 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
seen = seen[:0]
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err = b.Iterate(context.Background(), IteratePrm{handler: inc, decodeAddresses: true, ignoreErrors: true})
|
2022-01-20 15:39:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, seen, data[:1])
|
|
|
|
|
|
|
|
seen = seen[:0]
|
|
|
|
expectedErr := errors.New("stop iteration")
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err = b.Iterate(context.Background(), IteratePrm{
|
2022-01-20 15:39:16 +00:00
|
|
|
decodeAddresses: true,
|
|
|
|
handler: func(IterationElement) error { return expectedErr },
|
|
|
|
ignoreErrors: true,
|
|
|
|
})
|
2022-04-29 10:22:29 +00:00
|
|
|
require.ErrorIs(t, err, expectedErr)
|
2022-01-20 15:39:16 +00:00
|
|
|
}
|