From 11d152929ba6835bf6a82e7fc49c408078d7ea23 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Thu, 20 May 2021 13:40:25 +0200 Subject: [PATCH] node tests: Use in memory badger in unit tests --- libs/db/badgerdb/db.go | 16 ++++++++++++++++ node/node.go | 5 +++++ node/node_test.go | 37 ++++++++++++++++++++++++++++++------- rpc/test/helpers.go | 2 +- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/libs/db/badgerdb/db.go b/libs/db/badgerdb/db.go index 94a727dc46..c51f4d4b8b 100644 --- a/libs/db/badgerdb/db.go +++ b/libs/db/badgerdb/db.go @@ -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" ) @@ -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 } diff --git a/node/node.go b/node/node.go index f3391f92e9..1235c65b2d 100644 --- a/node/node.go +++ b/node/node.go @@ -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. diff --git a/node/node_test.go b/node/node_test.go index b8b448871c..3063825387 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -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) @@ -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) @@ -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 @@ -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()) } @@ -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) } @@ -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()) } @@ -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(), diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index de6d79825d..5a8b8e747a 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -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,