From ced64e76483b89e23408d3466b1c0e5734e41f6d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 30 Nov 2020 16:54:11 +0000 Subject: [PATCH] replace ResetTestRoot with SetupTestConfiguration in mempool and state --- mempool/bench_test.go | 6 ++-- mempool/cache_test.go | 3 +- mempool/clist_mempool_test.go | 56 ++++++++++++----------------------- mempool/reactor_test.go | 20 ++++++------- state/store_test.go | 4 +-- 5 files changed, 32 insertions(+), 57 deletions(-) diff --git a/mempool/bench_test.go b/mempool/bench_test.go index 779110b62..e57b3e598 100644 --- a/mempool/bench_test.go +++ b/mempool/bench_test.go @@ -11,8 +11,7 @@ import ( func BenchmarkReap(b *testing.B) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(b, cc) size := 10000 for i := 0; i < size; i++ { @@ -31,8 +30,7 @@ func BenchmarkReap(b *testing.B) { func BenchmarkCheckTx(b *testing.B) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(b, cc) for i := 0; i < b.N; i++ { tx := make([]byte, 8) diff --git a/mempool/cache_test.go b/mempool/cache_test.go index d9a53f475..009cb71ef 100644 --- a/mempool/cache_test.go +++ b/mempool/cache_test.go @@ -39,8 +39,7 @@ func TestCacheRemove(t *testing.T) { func TestCacheAfterUpdate(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) // reAddIndices & txsInCache can have elements > numTxsToCreate // also assumes max index is 255 for convenience diff --git a/mempool/clist_mempool_test.go b/mempool/clist_mempool_test.go index 1d4068198..d588a03dd 100644 --- a/mempool/clist_mempool_test.go +++ b/mempool/clist_mempool_test.go @@ -8,7 +8,6 @@ import ( "fmt" "io/ioutil" mrand "math/rand" - "os" "path/filepath" "testing" "time" @@ -30,24 +29,19 @@ import ( "github.com/tendermint/tendermint/types" ) -// A cleanupFunc cleans up any config / test files created for a particular -// test. -type cleanupFunc func() - -func newMempoolWithApp(cc proxy.ClientCreator) (*CListMempool, cleanupFunc) { - return newMempoolWithAppAndConfig(cc, cfg.ResetTestRoot("mempool_test")) +func newMempoolWithApp(t testing.TB, cc proxy.ClientCreator) *CListMempool { + return newMempoolWithAppAndConfig(t, cc, cfg.SetupTestConfiguration(t)) } -func newMempoolWithAppAndConfig(cc proxy.ClientCreator, config *cfg.Config) (*CListMempool, cleanupFunc) { +func newMempoolWithAppAndConfig(t testing.TB, cc proxy.ClientCreator, config *cfg.Config) *CListMempool { appConnMem, _ := cc.NewABCIClient() appConnMem.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "mempool")) - err := appConnMem.Start() - if err != nil { - panic(err) + if err := appConnMem.Start(); err != nil { + t.Fatal(err) } mempool := NewCListMempool(config.Mempool, appConnMem, 0) mempool.SetLogger(log.TestingLogger()) - return mempool, func() { os.RemoveAll(config.RootDir) } + return mempool } func ensureNoFire(t *testing.T, ch <-chan struct{}, timeoutMS int) { @@ -94,8 +88,7 @@ func checkTxs(t *testing.T, mempool Mempool, count int, peerID uint16) types.Txs func TestReapMaxBytesMaxGas(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) // Ensure gas calculation behaves as expected checkTxs(t, mempool, 1, UnknownPeerID) @@ -143,8 +136,7 @@ func TestReapMaxBytesMaxGas(t *testing.T) { func TestMempoolFilters(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) emptyTxArr := []types.Tx{[]byte{}} nopPreFilter := func(tx types.Tx) error { return nil } @@ -182,8 +174,7 @@ func TestMempoolFilters(t *testing.T) { func TestMempoolUpdate(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) // 1. Adds valid txs to the cache { @@ -220,8 +211,7 @@ func TestMempoolUpdate(t *testing.T) { func TestTxsAvailable(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) mempool.EnableTxsAvailable() timeoutMS := 500 @@ -265,8 +255,7 @@ func TestSerialReap(t *testing.T) { app := counter.NewApplication(true) cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() + mempool := newMempoolWithApp(t, cc) appConnCon, _ := cc.NewABCIClient() appConnCon.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "consensus")) @@ -387,8 +376,7 @@ func TestMempoolCloseWAL(t *testing.T) { wcfg.Mempool.RootDir = rootDir app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithAppAndConfig(cc, wcfg) - defer cleanup() + mempool := newMempoolWithAppAndConfig(t, cc, wcfg) mempool.height = 10 err = mempool.InitWAL() require.NoError(t, err) @@ -424,11 +412,8 @@ func TestMempoolCloseWAL(t *testing.T) { func TestMempool_CheckTxChecksTxSize(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempl, cleanup := newMempoolWithApp(cc) - defer cleanup() - + mempl := newMempoolWithApp(t, cc) maxTxSize := mempl.config.MaxTxBytes - testCases := []struct { len int err bool @@ -466,10 +451,9 @@ func TestMempool_CheckTxChecksTxSize(t *testing.T) { func TestMempoolTxsBytes(t *testing.T) { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - config := cfg.ResetTestRoot("mempool_test") + config := cfg.SetupTestConfiguration(t) config.Mempool.MaxTxsBytes = 10 - mempool, cleanup := newMempoolWithAppAndConfig(cc, config) - defer cleanup() + mempool := newMempoolWithAppAndConfig(t, cc, config) // 1. zero by default assert.EqualValues(t, 0, mempool.TxsBytes()) @@ -503,10 +487,9 @@ func TestMempoolTxsBytes(t *testing.T) { // 6. zero after tx is rechecked and removed due to not being valid anymore app2 := counter.NewApplication(true) cc = proxy.NewLocalClientCreator(app2) - mempool, cleanup = newMempoolWithApp(cc) - defer cleanup() - + mempool = newMempoolWithApp(t, cc) txBytes := make([]byte, 8) + binary.BigEndian.PutUint64(txBytes, uint64(0)) err = mempool.CheckTx(txBytes, nil, TxInfo{}) @@ -559,9 +542,8 @@ func TestMempoolRemoteAppConcurrency(t *testing.T) { t.Error(err) } }) - config := cfg.ResetTestRoot("mempool_test") - mempool, cleanup := newMempoolWithAppAndConfig(cc, config) - defer cleanup() + config := cfg.SetupTestConfiguration(t) + mempool := newMempoolWithAppAndConfig(t, cc, config) // generate small number of txs nTxs := 10 diff --git a/mempool/reactor_test.go b/mempool/reactor_test.go index d9e67d166..f9b0f9776 100644 --- a/mempool/reactor_test.go +++ b/mempool/reactor_test.go @@ -47,7 +47,7 @@ func TestReactorBroadcastTxsMessage(t *testing.T) { // replace Connect2Switches (full mesh) with a func, which connects first // reactor to others and nothing else, this test should also pass with >2 reactors. const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -69,7 +69,7 @@ func TestReactorBroadcastTxsMessage(t *testing.T) { func TestReactorConcurrency(t *testing.T) { config := cfg.TestConfig() const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -130,7 +130,7 @@ func TestReactorConcurrency(t *testing.T) { func TestReactorNoBroadcastToSender(t *testing.T) { config := cfg.TestConfig() const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -154,7 +154,7 @@ func TestReactor_MaxBatchBytes(t *testing.T) { config.Mempool.MaxBatchBytes = 1024 const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -196,7 +196,7 @@ func TestBroadcastTxForPeerStopsWhenPeerStops(t *testing.T) { config := cfg.TestConfig() const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -221,7 +221,7 @@ func TestBroadcastTxForPeerStopsWhenReactorStops(t *testing.T) { config := cfg.TestConfig() const N = 2 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) // stop reactors for _, r := range reactors { @@ -271,7 +271,7 @@ func TestMempoolIDsPanicsIfNodeRequestsOvermaxActiveIDs(t *testing.T) { func TestDontExhaustMaxActiveIDs(t *testing.T) { config := cfg.TestConfig() const N = 1 - reactors := makeAndConnectReactors(config, N) + reactors := makeAndConnectReactors(t, config, N) defer func() { for _, r := range reactors { if err := r.Stop(); err != nil { @@ -302,15 +302,13 @@ func mempoolLogger() log.Logger { } // connect N mempool reactors through N switches -func makeAndConnectReactors(config *cfg.Config, n int) []*Reactor { +func makeAndConnectReactors(t *testing.T, config *cfg.Config, n int) []*Reactor { reactors := make([]*Reactor, n) logger := mempoolLogger() for i := 0; i < n; i++ { app := kvstore.NewApplication() cc := proxy.NewLocalClientCreator(app) - mempool, cleanup := newMempoolWithApp(cc) - defer cleanup() - + mempool := newMempoolWithApp(t, cc) reactors[i] = NewReactor(config.Mempool, mempool) // so we dont start the consensus states reactors[i].SetLogger(logger.With("validator", i)) } diff --git a/state/store_test.go b/state/store_test.go index e43921519..f3444ae6b 100644 --- a/state/store_test.go +++ b/state/store_test.go @@ -2,7 +2,6 @@ package state_test import ( "fmt" - "os" "testing" "github.com/stretchr/testify/assert" @@ -49,8 +48,7 @@ func TestStoreLoadValidators(t *testing.T) { func BenchmarkLoadValidators(b *testing.B) { const valSetSize = 100 - config := cfg.ResetTestRoot("state_") - defer os.RemoveAll(config.RootDir) + config := cfg.SetupTestConfiguration(b) dbType := dbm.BackendType(config.DBBackend) stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) require.NoError(b, err)