Skip to content

Commit

Permalink
node tests: Use in memory badger in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed May 20, 2021
1 parent a55bf10 commit 11d1529
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
16 changes: 16 additions & 0 deletions libs/db/badgerdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v3/options"

"github.com/lazyledger/lazyledger-core/libs/db"
)
Expand Down Expand Up @@ -37,6 +38,21 @@ func NewDBWithOptions(opts badger.Options) (*BadgerDB, error) {
return &BadgerDB{db: db}, nil
}

// NewInMemoryDB creates a light weight in-memory BadgerDB.
// Mainly useful for unit-tests.
func NewInMemoryDB() (*BadgerDB, error) {
opts := badger.DefaultOptions("")
opts.InMemory = true
opts.NumCompactors = 2 // minimize number of go-routines
opts.Compression = options.None // this is supposed to be short-lived
opts.ZSTDCompressionLevel = 0 // this is supposed to be short-lived
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
return &BadgerDB{db: db}, nil
}

type BadgerDB struct {
db *badger.DB
}
Expand Down
5 changes: 5 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
return badgerdb.NewDB(ctx.ID, ctx.Config.DBDir())
}

// InMemDBProvider provides an in-memory DB.
func InMemDBProvider(ctx *DBContext) (dbm.DB, error) {
return badgerdb.NewInMemoryDB()
}

// GenesisDocProvider returns a GenesisDoc.
// It allows the GenesisDoc to be pulled from sources other than the
// filesystem, for instance from a distributed key-value store cluster.
Expand Down
37 changes: 30 additions & 7 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,35 @@ import (
tmtime "github.com/lazyledger/lazyledger-core/types/time"
)

func defaultNewTestNode(config *cfg.Config, logger log.Logger) (*Node, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err)
}

pval, err := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
if err != nil {
return nil, err
}

return NewNode(config,
pval,
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
InMemDBProvider,
ipfs.Mock(),
DefaultMetricsProvider(config.Instrumentation),
logger,
)
}

func TestNodeStartStop(t *testing.T) {
config := cfg.ResetTestRoot("node_node_test")
defer os.RemoveAll(config.RootDir)

// create & start node
n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger())
n, err := defaultNewTestNode(config, log.TestingLogger())
require.NoError(t, err)
err = n.Start()
require.NoError(t, err)
Expand Down Expand Up @@ -103,7 +126,7 @@ func TestNodeDelayedStart(t *testing.T) {
now := tmtime.Now()

// create & start node
n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger())
n, err := defaultNewTestNode(config, log.TestingLogger())
n.GenesisDoc().GenesisTime = now.Add(2 * time.Second)
require.NoError(t, err)

Expand All @@ -120,7 +143,7 @@ func TestNodeSetAppVersion(t *testing.T) {
defer os.RemoveAll(config.RootDir)

// create & start node
n, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger())
n, err := defaultNewTestNode(config, log.TestingLogger())
require.NoError(t, err)

// default config uses the kvstore app
Expand Down Expand Up @@ -164,7 +187,7 @@ func TestNodeSetPrivValTCP(t *testing.T) {
defer signerServer.Stop() //nolint:errcheck // ignore for tests

logger := log.TestingLogger()
n, err := DefaultNewNode(config, ipfs.Mock(), logger)
n, err := defaultNewTestNode(config, logger)
require.NoError(t, err)
assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
}
Expand All @@ -177,7 +200,7 @@ func TestPrivValidatorListenAddrNoProtocol(t *testing.T) {
defer os.RemoveAll(config.RootDir)
config.BaseConfig.PrivValidatorListenAddr = addrNoPrefix

_, err := DefaultNewNode(config, ipfs.Mock(), log.TestingLogger())
_, err := defaultNewTestNode(config, log.TestingLogger())
assert.Error(t, err)
}

Expand Down Expand Up @@ -209,7 +232,7 @@ func TestNodeSetPrivValIPC(t *testing.T) {
defer pvsc.Stop() //nolint:errcheck // ignore for tests

logger := log.TestingLogger()
n, err := DefaultNewNode(config, ipfs.Mock(), logger)
n, err := defaultNewTestNode(config, logger)
require.NoError(t, err)
assert.IsType(t, &privval.RetrySignerClient{}, n.PrivValidator())
}
Expand Down Expand Up @@ -514,7 +537,7 @@ func TestNodeNewNodeCustomReactors(t *testing.T) {
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
DefaultDBProvider,
InMemDBProvider,
ipfs.Mock(),
DefaultMetricsProvider(config.Instrumentation),
log.TestingLogger(),
Expand Down
2 changes: 1 addition & 1 deletion rpc/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func NewTendermint(app abci.Application, opts *Options) *nm.Node {

node, err := nm.NewNode(config, pv, nodeKey, papp,
nm.DefaultGenesisDocProviderFunc(config),
nm.DefaultDBProvider,
nm.InMemDBProvider,
ipfs.Mock(),
nm.DefaultMetricsProvider(config.Instrumentation),
logger,
Expand Down

0 comments on commit 11d1529

Please sign in to comment.