Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment in-memory badger in tests #200

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/testutil/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GenericNode(t testing.TB, hub *stub.Hub, identity *flow.Identity, participa
log := unittest.Logger().With().Int("index", i).Hex("node_id", identity.NodeID[:]).Str("role", identity.Role.String()).Logger()

dbDir := unittest.TempDir(t)
db := unittest.BadgerDB(t, dbDir)
db := unittest.BadgerDB(t, dbDir, true)

metrics := metrics.NewNoopCollector()
tracer, err := trace.NewTracer(log, "test")
Expand Down
14 changes: 3 additions & 11 deletions module/builder/collection/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package collection_test

import (
"math/rand"
"os"
"testing"
"time"

Expand Down Expand Up @@ -31,8 +30,7 @@ var noopSetter = func(*flow.Header) error { return nil }

type BuilderSuite struct {
suite.Suite
db *badger.DB
dbdir string
db *badger.DB

genesis *model.Block
chainID flow.ChainID
Expand Down Expand Up @@ -63,8 +61,7 @@ func (suite *BuilderSuite) SetupTest() {

suite.pool = stdmap.NewTransactions(1000)

suite.dbdir = unittest.TempDir(suite.T())
suite.db = unittest.BadgerDB(suite.T(), suite.dbdir)
suite.db = unittest.BadgerDB(suite.T(), "", true)

metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
Expand All @@ -88,8 +85,6 @@ func (suite *BuilderSuite) SetupTest() {
func (suite *BuilderSuite) TearDownTest() {
err := suite.db.Close()
suite.Assert().Nil(err)
err = os.RemoveAll(suite.dbdir)
suite.Assert().Nil(err)
}

func (suite *BuilderSuite) Bootstrap() {
Expand Down Expand Up @@ -824,13 +819,10 @@ func benchmarkBuildOn(b *testing.B, size int) {

suite.pool = stdmap.NewTransactions(1000)

suite.dbdir = unittest.TempDir(b)
suite.db = unittest.BadgerDB(b, suite.dbdir)
suite.db = unittest.BadgerDB(b, "", true)
defer func() {
err = suite.db.Close()
assert.Nil(b, err)
err = os.RemoveAll(suite.dbdir)
assert.Nil(b, err)
}()

metrics := metrics.NewNoopCollector()
Expand Down
9 changes: 2 additions & 7 deletions state/cluster/badger/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"math"
"math/rand"
"os"
"testing"
"time"

Expand All @@ -28,8 +27,7 @@ import (

type MutatorSuite struct {
suite.Suite
db *badger.DB
dbdir string
db *badger.DB

genesis *model.Block
chainID flow.ChainID
Expand All @@ -52,8 +50,7 @@ func (suite *MutatorSuite) SetupTest() {
suite.genesis = model.Genesis()
suite.chainID = suite.genesis.Header.ChainID

suite.dbdir = unittest.TempDir(suite.T())
suite.db = unittest.BadgerDB(suite.T(), suite.dbdir)
suite.db = unittest.BadgerDB(suite.T(), "", true)

metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
Expand All @@ -73,8 +70,6 @@ func (suite *MutatorSuite) SetupTest() {
func (suite *MutatorSuite) TearDownTest() {
err := suite.db.Close()
suite.Assert().Nil(err)
err = os.RemoveAll(suite.dbdir)
suite.Assert().Nil(err)
}

// Bootstrap bootstraps the cluster chain. Useful for conciseness in test cases
Expand Down
9 changes: 2 additions & 7 deletions state/cluster/badger/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package badger
import (
"math"
"math/rand"
"os"
"testing"
"time"

Expand All @@ -27,8 +26,7 @@ import (

type SnapshotSuite struct {
suite.Suite
db *badger.DB
dbdir string
db *badger.DB

genesis *model.Block
chainID flow.ChainID
Expand All @@ -49,8 +47,7 @@ func (suite *SnapshotSuite) SetupTest() {
suite.genesis = model.Genesis()
suite.chainID = suite.genesis.Header.ChainID

suite.dbdir = unittest.TempDir(suite.T())
suite.db = unittest.BadgerDB(suite.T(), suite.dbdir)
suite.db = unittest.BadgerDB(suite.T(), "", true)

metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
Expand Down Expand Up @@ -78,8 +75,6 @@ func (suite *SnapshotSuite) SetupTest() {
func (suite *SnapshotSuite) TearDownTest() {
err := suite.db.Close()
suite.Assert().Nil(err)
err = os.RemoveAll(suite.dbdir)
suite.Assert().Nil(err)
}

func (suite *SnapshotSuite) Bootstrap() {
Expand Down
13 changes: 6 additions & 7 deletions utils/unittest/unittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,25 @@ func RunWithTempDir(t testing.TB, f func(string)) {
f(dbDir)
}

func BadgerDB(t testing.TB, dir string) *badger.DB {
func BadgerDB(t testing.TB, dir string, inmemory bool) *badger.DB {
opts := badger.
DefaultOptions(dir).
WithKeepL0InMemory(true).
WithInMemory(inmemory).
WithLogger(nil)
db, err := badger.Open(opts)
require.NoError(t, err)
return db
}

func RunWithBadgerDB(t testing.TB, f func(*badger.DB)) {
RunWithTempDir(t, func(dir string) {
db := BadgerDB(t, dir)
defer db.Close()
f(db)
})
db := BadgerDB(t, "", true)
defer db.Close()
f(db)
}

func TempBadgerDB(t testing.TB) (*badger.DB, string) {
dir := TempDir(t)
db := BadgerDB(t, dir)
db := BadgerDB(t, dir, false)
return db, dir
}