Skip to content

Commit

Permalink
Replace shutdownEnvironment with t.Cleanup() (#2491)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Jan 19, 2024
1 parent 9b302a5 commit a054afd
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 262 deletions.
48 changes: 11 additions & 37 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func TestBuildBlockBasic(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
Expand Down Expand Up @@ -78,10 +75,7 @@ func TestBuildBlockDoesNotBuildWithEmptyMempool(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

tx, exists := env.mempool.Peek()
require.False(exists)
Expand All @@ -98,10 +92,7 @@ func TestBuildBlockShouldReward(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

var (
now = env.backend.Clk.Time()
Expand Down Expand Up @@ -198,10 +189,7 @@ func TestBuildBlockAdvanceTime(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

var (
now = env.backend.Clk.Time()
Expand Down Expand Up @@ -234,10 +222,7 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
Expand Down Expand Up @@ -291,10 +276,7 @@ func TestBuildBlockDropExpiredStakerTxs(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

// The [StartTime] in a staker tx is only validated pre-Durango.
// TODO: Delete this test post-Durango activation.
Expand Down Expand Up @@ -397,10 +379,7 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

// Post-Durango, [StartTime] is no longer validated. Staking durations are
// based on the current chain timestamp and must be validated.
Expand Down Expand Up @@ -476,11 +455,7 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
env.ctx.Lock.Lock()
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
Expand Down Expand Up @@ -509,6 +484,7 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) {
env.ctx.Lock.Unlock()
err = env.network.IssueTx(context.Background(), tx)
require.ErrorIs(err, errTestingDropped)
env.ctx.Lock.Lock()
_, ok := env.mempool.Get(txID)
require.False(ok)

Expand All @@ -522,11 +498,9 @@ func TestNoErrorOnUnexpectedSetPreferenceDuringBootstrapping(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

env.isBootstrapped.Set(false)
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()

require.True(env.blkManager.SetPreference(ids.GenerateTestID())) // should not panic
}
Expand Down
38 changes: 18 additions & 20 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ func newEnvironment(t *testing.T) *environment {
res.blkManager.SetPreference(genesisID)
addSubnet(t, res)

t.Cleanup(func() {
res.ctx.Lock.Lock()
defer res.ctx.Lock.Unlock()

res.Builder.ShutdownBlockTimer()

if res.isBootstrapped.Get() {
validatorIDs := res.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID)

require.NoError(res.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID))

require.NoError(res.state.Commit())
}

require.NoError(res.state.Close())
require.NoError(res.baseDB.Close())
})

return res
}

Expand Down Expand Up @@ -398,23 +416,3 @@ func buildGenesisTest(t *testing.T, ctx *snow.Context) []byte {

return genesisBytes
}

func shutdownEnvironment(env *environment) error {
env.Builder.ShutdownBlockTimer()

if env.isBootstrapped.Get() {
validatorIDs := env.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID)

if err := env.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID); err != nil {
return err
}
if err := env.state.Commit(); err != nil {
return err
}
}

return utils.Err(
env.state.Close(),
env.baseDB.Close(),
)
}
5 changes: 1 addition & 4 deletions vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func TestAtomicTxImports(t *testing.T) {

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
defer env.ctx.Lock.Unlock()

utxoID := avax.UTXOID{
TxID: ids.Empty.Prefix(1),
Expand Down
54 changes: 27 additions & 27 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/stretchr/testify/require"

"go.uber.org/mock/gomock"

"github.com/ava-labs/avalanchego/chains"
Expand Down Expand Up @@ -220,6 +222,31 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
// whatever we need
}

t.Cleanup(func() {
res.ctx.Lock.Lock()
defer res.ctx.Lock.Unlock()

if res.mockedState != nil {
// state is mocked, nothing to do here
return
}

require := require.New(t)

if res.isBootstrapped.Get() {
validatorIDs := res.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID)

require.NoError(res.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID))
require.NoError(res.state.Commit())
}

if res.state != nil {
require.NoError(res.state.Close())
}

require.NoError(res.baseDB.Close())
})

return res
}

Expand Down Expand Up @@ -424,33 +451,6 @@ func buildGenesisTest(ctx *snow.Context) []byte {
return genesisBytes
}

func shutdownEnvironment(t *environment) error {
if t.mockedState != nil {
// state is mocked, nothing to do here
return nil
}

if t.isBootstrapped.Get() {
validatorIDs := t.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID)

if err := t.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID); err != nil {
return err
}
if err := t.state.Commit(); err != nil {
return err
}
}

var err error
if t.state != nil {
err = t.state.Close()
}
return utils.Err(
err,
t.baseDB.Close(),
)
}

func addPendingValidator(
env *environment,
startTime time.Time,
Expand Down
24 changes: 0 additions & 24 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
defer func() {
require.NoError(shutdownEnvironment(env))
}()

// create apricotParentBlk. It's a standard one for simplicity
parentHeight := uint64(2022)
Expand Down Expand Up @@ -148,9 +145,6 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.clk.Set(defaultGenesisTime)
env.config.BanffTime = time.Time{} // activate Banff
env.config.DurangoTime = mockable.MaxTime // deactivate Durango
Expand Down Expand Up @@ -569,9 +563,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -722,9 +713,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -865,9 +853,6 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
t.Run(fmt.Sprintf("tracked %t", tracked), func(ts *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -971,9 +956,6 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down Expand Up @@ -1156,9 +1138,6 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down Expand Up @@ -1341,9 +1320,6 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
func TestAddValidatorProposalBlock(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
defer func() {
require.NoError(shutdownEnvironment(env))
}()
env.config.BanffTime = time.Time{} // activate Banff
env.config.DurangoTime = time.Time{} // activate Durango

Expand Down
Loading

0 comments on commit a054afd

Please sign in to comment.