Skip to content

Commit

Permalink
tests: auto-close temporal db (#11160)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 16, 2024
1 parent f8abd99 commit 5878b14
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
9 changes: 4 additions & 5 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ type SimulatedBackend struct {

// NewSimulatedBackend creates a new binding backend using a simulated blockchain
// for testing purposes.
func NewSimulatedBackendWithConfig(alloc types.GenesisAlloc, config *chain.Config, gasLimit uint64) *SimulatedBackend {
func NewSimulatedBackendWithConfig(t *testing.T, alloc types.GenesisAlloc, config *chain.Config, gasLimit uint64) *SimulatedBackend {
genesis := types.Genesis{Config: config, GasLimit: gasLimit, Alloc: alloc}
engine := ethash.NewFaker()
checkStateRoot := true
m := mock.MockWithGenesisEngine(nil, &genesis, engine, false, checkStateRoot)
m := mock.MockWithGenesisEngine(t, &genesis, engine, false, checkStateRoot)
backend := &SimulatedBackend{
m: m,
prependBlock: m.Genesis,
Expand All @@ -119,13 +119,12 @@ func NewSimulatedBackendWithConfig(alloc types.GenesisAlloc, config *chain.Confi

// A simulated backend always uses chainID 1337.
func NewSimulatedBackend(t *testing.T, alloc types.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
b := NewSimulatedBackendWithConfig(alloc, params.TestChainConfig, gasLimit)
t.Cleanup(b.Close)
b := NewTestSimulatedBackendWithConfig(t, alloc, params.TestChainConfig, gasLimit)
return b
}

func NewTestSimulatedBackendWithConfig(t *testing.T, alloc types.GenesisAlloc, config *chain.Config, gasLimit uint64) *SimulatedBackend {
b := NewSimulatedBackendWithConfig(alloc, config, gasLimit)
b := NewSimulatedBackendWithConfig(t, alloc, config, gasLimit)
t.Cleanup(b.Close)
return b
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ func Main(ctx *cli.Context) error {
return h
}

db, _ := temporaltest.NewTestDB(nil, datadir.New(""))
db, agg := temporaltest.NewTestDB(nil, datadir.New(""))
defer db.Close()
defer agg.Close()

tx, err := db.BeginRw(context.Background())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func initialState1() error {
m := mock.MockWithGenesis(nil, gspec, key, false)
defer m.DB.Close()

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
contractBackend := backends.NewSimulatedBackendWithConfig(nil, gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions erigon-lib/kv/temporal/temporaltest/kv_temporal_testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func NewTestDB(tb testing.TB, dirs datadir.Dirs) (db kv.RwDB, agg *state.Aggrega
if tb != nil {
tb.Helper()
}
logger := log.New()

if tb != nil {
db = memdb.NewTestDB(tb)
Expand All @@ -43,17 +42,23 @@ func NewTestDB(tb testing.TB, dirs datadir.Dirs) (db kv.RwDB, agg *state.Aggrega
}

var err error
agg, err = state.NewAggregator(context.Background(), dirs, config3.HistoryV3AggregationStep, db, nil, logger)
agg, err = state.NewAggregator(context.Background(), dirs, config3.HistoryV3AggregationStep, db, nil, log.New())
if err != nil {
panic(err)
}
if err := agg.OpenFolder(); err != nil {
panic(err)
}
if tb != nil {
tb.Cleanup(agg.Close)
}

db, err = temporal.New(db, agg)
if err != nil {
panic(err)
}
if tb != nil {
tb.Cleanup(agg.Close)
}
return db, agg
}
8 changes: 7 additions & 1 deletion tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,15 @@ func (tm *testMatcher) runTestFile(t *testing.T, path, name string, runTest inte
if len(keys) == 1 {
runTestFunc(runTest, t, name, m, keys[0])
} else {
i := 0
for _, key := range keys {
i++
name := name + "/" + key
t.Run(key, func(t *testing.T) {
subTestName := key
if len(subTestName) > 32 {
subTestName = fmt.Sprintf("%s_%s_%d", key[:20], key[len(key)-20:], i)
}
t.Run(subTestName, func(t *testing.T) {
if r, _ := tm.findSkip(name); r != "" {
t.Skip(r)
}
Expand Down
5 changes: 4 additions & 1 deletion turbo/stages/mock/mock_sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
engine consensus.Engine, blockBufferSize int, withTxPool, withPosDownloader, checkStateRoot bool,
) *MockSentry {
tmpdir := os.TempDir()
if tb != nil {
tmpdir = tb.TempDir()
}
ctrl := gomock.NewController(tb)
dirs := datadir.New(tmpdir)
var err error
Expand All @@ -279,7 +282,7 @@ func MockWithEverything(tb testing.TB, gspec *types.Genesis, key *ecdsa.PrivateK
logger.SetHandler(log.LvlFilterHandler(log.LvlError, log.StderrHandler))

ctx, ctxCancel := context.WithCancel(context.Background())
db, agg := temporaltest.NewTestDB(nil, dirs)
db, agg := temporaltest.NewTestDB(tb, dirs)

erigonGrpcServeer := remotedbserver.NewKvServer(ctx, db, nil, nil, nil, logger)
allSnapshots := freezeblocks.NewRoSnapshots(ethconfig.Defaults.Snapshot, dirs.Snap, 0, logger)
Expand Down

0 comments on commit 5878b14

Please sign in to comment.