2020-11-01 12:55:43 +00:00
|
|
|
package badgerbs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-02 13:32:07 +00:00
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-29 20:01:00 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-11-01 12:55:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBadgerBlockstore(t *testing.T) {
|
|
|
|
(&Suite{
|
|
|
|
NewBlockstore: newBlockstore(DefaultOptions),
|
|
|
|
OpenBlockstore: openBlockstore(DefaultOptions),
|
|
|
|
}).RunTests(t, "non_prefixed")
|
|
|
|
|
|
|
|
prefixed := func(path string) Options {
|
|
|
|
opts := DefaultOptions(path)
|
|
|
|
opts.Prefix = "/prefixed/"
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
(&Suite{
|
|
|
|
NewBlockstore: newBlockstore(prefixed),
|
|
|
|
OpenBlockstore: openBlockstore(prefixed),
|
|
|
|
}).RunTests(t, "prefixed")
|
|
|
|
}
|
|
|
|
|
2020-11-02 13:32:07 +00:00
|
|
|
func TestStorageKey(t *testing.T) {
|
|
|
|
bs, _ := newBlockstore(DefaultOptions)(t)
|
|
|
|
bbs := bs.(*Blockstore)
|
|
|
|
defer bbs.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
cid1 := blocks.NewBlock([]byte("some data")).Cid()
|
|
|
|
cid2 := blocks.NewBlock([]byte("more data")).Cid()
|
|
|
|
cid3 := blocks.NewBlock([]byte("a little more data")).Cid()
|
|
|
|
require.NotEqual(t, cid1, cid2) // sanity check
|
|
|
|
require.NotEqual(t, cid2, cid3) // sanity check
|
|
|
|
|
|
|
|
// nil slice; let StorageKey allocate for us.
|
|
|
|
k1 := bbs.StorageKey(nil, cid1)
|
|
|
|
require.Len(t, k1, 55)
|
|
|
|
require.True(t, cap(k1) == len(k1))
|
|
|
|
|
|
|
|
// k1's backing array is reused.
|
|
|
|
k2 := bbs.StorageKey(k1, cid2)
|
|
|
|
require.Len(t, k2, 55)
|
|
|
|
require.True(t, cap(k2) == len(k1))
|
|
|
|
|
|
|
|
// bring k2 to len=0, and verify that its backing array gets reused
|
|
|
|
// (i.e. k1 and k2 are overwritten)
|
|
|
|
k3 := bbs.StorageKey(k2[:0], cid3)
|
|
|
|
require.Len(t, k3, 55)
|
|
|
|
require.True(t, cap(k3) == len(k3))
|
|
|
|
|
|
|
|
// backing array of k1 and k2 has been modified, i.e. memory is shared.
|
|
|
|
require.Equal(t, k3, k1)
|
|
|
|
require.Equal(t, k3, k2)
|
|
|
|
}
|
|
|
|
|
2021-02-28 23:02:36 +00:00
|
|
|
func newBlockstore(optsSupplier func(path string) Options) func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
|
|
|
|
return func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
|
2020-11-01 12:55:43 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
path, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := Open(optsSupplier(path))
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tb.Cleanup(func() {
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
})
|
|
|
|
|
|
|
|
return db, path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 23:02:36 +00:00
|
|
|
func openBlockstore(optsSupplier func(path string) Options) func(tb testing.TB, path string) (bs blockstore.BasicBlockstore, err error) {
|
|
|
|
return func(tb testing.TB, path string) (bs blockstore.BasicBlockstore, err error) {
|
2020-11-01 12:55:43 +00:00
|
|
|
tb.Helper()
|
|
|
|
return Open(optsSupplier(path))
|
|
|
|
}
|
|
|
|
}
|