Skip to content

Commit

Permalink
add both versions to badger test
Browse files Browse the repository at this point in the history
  • Loading branch information
snissn committed Jul 31, 2024
1 parent ab99ef0 commit feac779
Showing 1 changed file with 84 additions and 58 deletions.
142 changes: 84 additions & 58 deletions blockstore/badger/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,71 +19,86 @@ import (
versions "github.com/filecoin-project/lotus/blockstore/badger/versions"
)

const BADGER_DEFAULT_VERSION = 2
const (
BADGER_VERSION_2 = 2
BADGER_VERSION_4 = 4
)

var SUPPORTED_BADGER_VERSIONS = []int{BADGER_VERSION_2, BADGER_VERSION_4}

func TestBadgerBlockstore(t *testing.T) {
//stm: @SPLITSTORE_BADGER_PUT_001, @SPLITSTORE_BADGER_POOLED_STORAGE_KEY_001
//stm: @SPLITSTORE_BADGER_OPEN_001, @SPLITSTORE_BADGER_CLOSE_001
(&Suite{
NewBlockstore: newBlockstore(versions.DefaultOptions),
OpenBlockstore: openBlockstore(versions.DefaultOptions),
}).RunTests(t, "non_prefixed")

prefixed := func(path string, _ bool, _ int) versions.Options {
opts := versions.DefaultOptions(path, false, BADGER_DEFAULT_VERSION)
opts.Prefix = "/prefixed/"
return opts
}
for _, version := range SUPPORTED_BADGER_VERSIONS {
t.Run(fmt.Sprintf("non_prefixed_v%d", version), func(t *testing.T) {
(&Suite{
NewBlockstore: newBlockstore(versions.DefaultOptions, version),
OpenBlockstore: openBlockstore(versions.DefaultOptions, version),
}).RunTests(t, "non_prefixed")
})

t.Run(fmt.Sprintf("prefixed_v%d", version), func(t *testing.T) {
prefixed := func(path string, _ bool, _ int) versions.Options {
opts := versions.DefaultOptions(path, false, version)
opts.Prefix = "/prefixed/"
return opts
}

(&Suite{
NewBlockstore: newBlockstore(prefixed),
OpenBlockstore: openBlockstore(prefixed),
}).RunTests(t, "prefixed")
(&Suite{
NewBlockstore: newBlockstore(prefixed, version),
OpenBlockstore: openBlockstore(prefixed, version),
}).RunTests(t, "prefixed")
})
}
}

func TestStorageKey(t *testing.T) {
//stm: @SPLITSTORE_BADGER_OPEN_001, @SPLITSTORE_BADGER_CLOSE_001
//stm: @SPLITSTORE_BADGER_STORAGE_KEY_001
bs, _ := newBlockstore(versions.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)
fmt.Println(cid1)
fmt.Println(k1)
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)
for _, version := range SUPPORTED_BADGER_VERSIONS {
t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
bs, _ := newBlockstore(versions.DefaultOptions, version)(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)
fmt.Println(cid1)
fmt.Println(k1)
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)
})
}
}

func newBlockstore(optsSupplier func(path string, readonly bool, badgerVersion int) versions.Options) func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
func newBlockstore(optsSupplier func(path string, readonly bool, badgerVersion int) versions.Options, version int) func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
return func(tb testing.TB) (bs blockstore.BasicBlockstore, path string) {
tb.Helper()

path = tb.TempDir()

db, err := Open(optsSupplier(path, false, BADGER_DEFAULT_VERSION))
db, err := Open(optsSupplier(path, false, version))
if err != nil {
tb.Fatal(err)
}
Expand All @@ -92,10 +107,10 @@ func newBlockstore(optsSupplier func(path string, readonly bool, badgerVersion i
}
}

func openBlockstore(optsSupplier func(path string, readonly bool, badgerVersion int) versions.Options) func(tb testing.TB, path string) (bs blockstore.BasicBlockstore, err error) {
func openBlockstore(optsSupplier func(path string, readonly bool, badgerVersion int) versions.Options, version int) func(tb testing.TB, path string) (bs blockstore.BasicBlockstore, err error) {
return func(tb testing.TB, path string) (bs blockstore.BasicBlockstore, err error) {
tb.Helper()
return Open(optsSupplier(path, false, BADGER_DEFAULT_VERSION))
return Open(optsSupplier(path, false, version))
}
}

Expand All @@ -105,7 +120,7 @@ func testMove(t *testing.T, optsF func(string, bool, int) versions.Options) {

dbPath := filepath.Join(basePath, "db")

db, err := Open(optsF(dbPath, false, BADGER_DEFAULT_VERSION))
db, err := Open(optsF(dbPath, false, BADGER_VERSION_2))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -248,7 +263,7 @@ func testMove(t *testing.T, optsF func(string, bool, int) versions.Options) {
t.Fatal(err)
}

db, err = Open(optsF(dbPath, false, BADGER_DEFAULT_VERSION))
db, err = Open(optsF(dbPath, false, BADGER_VERSION_2))
if err != nil {
t.Fatal(err)
}
Expand All @@ -262,16 +277,27 @@ func TestMoveNoPrefix(t *testing.T) {
//stm: @SPLITSTORE_BADGER_OPEN_001, @SPLITSTORE_BADGER_CLOSE_001
//stm: @SPLITSTORE_BADGER_PUT_001, @SPLITSTORE_BADGER_POOLED_STORAGE_KEY_001
//stm: @SPLITSTORE_BADGER_DELETE_001, @SPLITSTORE_BADGER_COLLECT_GARBAGE_001
testMove(t, versions.DefaultOptions)
for _, version := range SUPPORTED_BADGER_VERSIONS {
t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
testMove(t, func(path string, readonly bool, badgerVersion int) versions.Options {
return versions.DefaultOptions(path, false, version)
})
})
}
}

func TestMoveWithPrefix(t *testing.T) {
//stm: @SPLITSTORE_BADGER_OPEN_001, @SPLITSTORE_BADGER_CLOSE_001
//stm: @SPLITSTORE_BADGER_PUT_001, @SPLITSTORE_BADGER_POOLED_STORAGE_KEY_001
//stm: @SPLITSTORE_BADGER_DELETE_001, @SPLITSTORE_BADGER_COLLECT_GARBAGE_001
testMove(t, func(path string, readonly bool, badgerVersion int) versions.Options {
opts := versions.DefaultOptions(path, false, badgerVersion)
opts.Prefix = "/prefixed/"
return opts
})
for _, version := range SUPPORTED_BADGER_VERSIONS {
t.Run(fmt.Sprintf("v%d", version), func(t *testing.T) {
testMove(t, func(path string, readonly bool, badgerVersion int) versions.Options {
opts := versions.DefaultOptions(path, false, version)
opts.Prefix = "/prefixed/"
return opts
})
})
}
}

0 comments on commit feac779

Please sign in to comment.