2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-09-13 10:13:18 +00:00
|
|
|
"encoding/binary"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-01-20 18:13:18 +00:00
|
|
|
"math/rand"
|
2020-11-26 14:26:53 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger/test"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/address/test"
|
2020-11-26 14:26:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-03 14:16:49 +00:00
|
|
|
func testPutGet(t *testing.T, blz *Blobovnicza, addr *addressSDK.Address, sz uint64, assertErrPut, assertErrGet func(error) bool) *addressSDK.Address {
|
2020-12-02 09:58:42 +00:00
|
|
|
// create binary object
|
|
|
|
data := make([]byte, sz)
|
2022-03-03 14:16:49 +00:00
|
|
|
rand.Read(data)
|
2020-12-02 09:58:42 +00:00
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
pPut := new(PutPrm)
|
2020-12-02 09:58:42 +00:00
|
|
|
pPut.SetAddress(addr)
|
|
|
|
pPut.SetMarshaledObject(data)
|
2020-11-26 14:26:53 +00:00
|
|
|
_, err := blz.Put(pPut)
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErrPut != nil {
|
|
|
|
require.True(t, assertErrPut(err))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErrPut != nil {
|
2020-11-26 14:26:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
testGet(t, blz, addr, data, assertErrGet)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
return addr
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
func testGet(t *testing.T, blz *Blobovnicza, addr *addressSDK.Address, expObj []byte, assertErr func(error) bool) {
|
2020-11-26 14:26:53 +00:00
|
|
|
pGet := new(GetPrm)
|
|
|
|
pGet.SetAddress(addr)
|
|
|
|
|
|
|
|
// try to read object from Blobovnicza
|
|
|
|
res, err := blz.Get(pGet)
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErr != nil {
|
|
|
|
require.True(t, assertErr(err))
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
if assertErr == nil {
|
2020-11-26 14:26:53 +00:00
|
|
|
require.Equal(t, expObj, res.Object())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlobovnicza(t *testing.T) {
|
2021-01-20 18:13:18 +00:00
|
|
|
rand.Seed(1024)
|
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
p := "./test_blz"
|
|
|
|
|
|
|
|
sizeLim := uint64(256 * 1 << 10) // 256KB
|
|
|
|
objSizeLim := sizeLim / 2
|
|
|
|
|
|
|
|
// create Blobovnicza instance
|
|
|
|
blz := New(
|
|
|
|
WithPath(p),
|
|
|
|
WithObjectSizeLimit(objSizeLim),
|
|
|
|
WithFullSizeLimit(sizeLim),
|
|
|
|
WithLogger(test.NewLogger(false)),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer os.Remove(p)
|
|
|
|
|
|
|
|
// open Blobovnicza
|
|
|
|
require.NoError(t, blz.Open())
|
|
|
|
|
|
|
|
// initialize Blobovnicza
|
|
|
|
require.NoError(t, blz.Init())
|
|
|
|
|
|
|
|
// try to read non-existent address
|
2022-05-12 16:37:46 +00:00
|
|
|
testGet(t, blz, objecttest.Address(), nil, IsErrNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
filled := uint64(15 * 1 << 10)
|
|
|
|
|
|
|
|
// test object 15KB
|
2022-05-12 16:37:46 +00:00
|
|
|
addr := testPutGet(t, blz, objecttest.Address(), filled, nil, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// remove the object
|
|
|
|
dPrm := new(DeletePrm)
|
|
|
|
dPrm.SetAddress(addr)
|
|
|
|
|
|
|
|
_, err := blz.Delete(dPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// should return 404
|
2022-03-17 08:03:58 +00:00
|
|
|
testGet(t, blz, addr, nil, IsErrNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// fill Blobovnicza fully
|
|
|
|
for ; filled < sizeLim; filled += objSizeLim {
|
2022-05-12 16:37:46 +00:00
|
|
|
testPutGet(t, blz, objecttest.Address(), objSizeLim, nil, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// from now objects should not be saved
|
2022-05-12 16:37:46 +00:00
|
|
|
testPutGet(t, blz, objecttest.Address(), 1024, func(err error) bool {
|
2022-03-17 08:03:58 +00:00
|
|
|
return errors.Is(err, ErrFull)
|
|
|
|
}, nil)
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, blz.Close())
|
|
|
|
}
|
2021-09-13 10:13:18 +00:00
|
|
|
|
|
|
|
func TestIterateObjects(t *testing.T) {
|
|
|
|
p := t.Name()
|
|
|
|
|
|
|
|
// create Blobovnicza instance
|
|
|
|
blz := New(
|
|
|
|
WithPath(p),
|
|
|
|
WithObjectSizeLimit(1<<10),
|
|
|
|
WithFullSizeLimit(100<<10),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer os.Remove(p)
|
|
|
|
|
|
|
|
// open Blobovnicza
|
|
|
|
require.NoError(t, blz.Open())
|
|
|
|
|
|
|
|
// initialize Blobovnicza
|
|
|
|
require.NoError(t, blz.Init())
|
|
|
|
|
|
|
|
const objNum = 5
|
|
|
|
|
|
|
|
mObjs := make(map[string][]byte)
|
|
|
|
|
|
|
|
for i := uint64(0); i < objNum; i++ {
|
|
|
|
data := make([]byte, 8) // actual data doesn't really matter for test
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(data, i)
|
|
|
|
|
|
|
|
mObjs[string(data)] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
var putPrm PutPrm
|
|
|
|
|
|
|
|
for _, v := range mObjs {
|
|
|
|
putPrm.SetAddress(objecttest.Address())
|
|
|
|
putPrm.SetMarshaledObject(v)
|
|
|
|
|
|
|
|
_, err := blz.Put(&putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := IterateObjects(blz, func(data []byte) error {
|
|
|
|
v, ok := mObjs[string(data)]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, v, data)
|
|
|
|
|
|
|
|
delete(mObjs, string(data))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, mObjs)
|
|
|
|
}
|