Skip to content

Commit

Permalink
Replace closeFn with t.Cleanup (#2638)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
  • Loading branch information
dhrubabasu and StephenButtolph committed Jan 23, 2024
1 parent ad516fc commit bb2357e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
20 changes: 6 additions & 14 deletions database/rpcdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import (
)

type testDatabase struct {
client *DatabaseClient
server *memdb.Database
closeFn func()
client *DatabaseClient
server *memdb.Database
}

func setupDB(t testing.TB) *testDatabase {
Expand All @@ -45,11 +44,13 @@ func setupDB(t testing.TB) *testDatabase {
require.NoError(err)

db.client = NewClient(rpcdbpb.NewDatabaseClient(conn))
db.closeFn = func() {

t.Cleanup(func() {
serverCloser.Stop()
_ = conn.Close()
_ = listener.Close()
}
})

return db
}

Expand All @@ -58,31 +59,23 @@ func TestInterface(t *testing.T) {
t.Run(name, func(t *testing.T) {
db := setupDB(t)
test(t, db.client)

db.closeFn()
})
}
}

func FuzzKeyValue(f *testing.F) {
db := setupDB(f)
database.FuzzKeyValue(f, db.client)

db.closeFn()
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
db := setupDB(f)
database.FuzzNewIteratorWithPrefix(f, db.client)

db.closeFn()
}

func FuzzNewIteratorWithStartAndPrefix(f *testing.F) {
db := setupDB(f)
database.FuzzNewIteratorWithStartAndPrefix(f, db.client)

db.closeFn()
}

func BenchmarkInterface(b *testing.B) {
Expand All @@ -92,7 +85,6 @@ func BenchmarkInterface(b *testing.B) {
b.Run(fmt.Sprintf("rpcdb_%d_pairs_%d_keys_%d_values_%s", size[0], size[1], size[2], name), func(b *testing.B) {
db := setupDB(b)
bench(b, db.client, keys, values)
db.closeFn()
})
}
}
Expand Down
18 changes: 6 additions & 12 deletions snow/validators/gvalidators/validator_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
var errCustom = errors.New("custom")

type testState struct {
client *Client
server *validators.MockState
closeFn func()
client *Client
server *validators.MockState
}

func setupState(t testing.TB, ctrl *gomock.Controller) *testState {
Expand All @@ -52,11 +51,13 @@ func setupState(t testing.TB, ctrl *gomock.Controller) *testState {
require.NoError(err)

state.client = NewClient(pb.NewValidatorStateClient(conn))
state.closeFn = func() {

t.Cleanup(func() {
serverCloser.Stop()
_ = conn.Close()
_ = listener.Close()
}
})

return state
}

Expand All @@ -65,7 +66,6 @@ func TestGetMinimumHeight(t *testing.T) {
ctrl := gomock.NewController(t)

state := setupState(t, ctrl)
defer state.closeFn()

// Happy path
expectedHeight := uint64(1337)
Expand All @@ -88,7 +88,6 @@ func TestGetCurrentHeight(t *testing.T) {
ctrl := gomock.NewController(t)

state := setupState(t, ctrl)
defer state.closeFn()

// Happy path
expectedHeight := uint64(1337)
Expand All @@ -111,7 +110,6 @@ func TestGetSubnetID(t *testing.T) {
ctrl := gomock.NewController(t)

state := setupState(t, ctrl)
defer state.closeFn()

// Happy path
chainID := ids.GenerateTestID()
Expand All @@ -135,7 +133,6 @@ func TestGetValidatorSet(t *testing.T) {
ctrl := gomock.NewController(t)

state := setupState(t, ctrl)
defer state.closeFn()

// Happy path
sk0, err := bls.NewSecretKey()
Expand Down Expand Up @@ -209,9 +206,6 @@ func benchmarkGetValidatorSet(b *testing.B, vs map[ids.NodeID]*validators.GetVal
require := require.New(b)
ctrl := gomock.NewController(b)
state := setupState(b, ctrl)
defer func() {
state.closeFn()
}()

height := uint64(1337)
subnetID := ids.GenerateTestID()
Expand Down
8 changes: 4 additions & 4 deletions vms/platformvm/warp/gwarp/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type testSigner struct {
sk *bls.SecretKey
networkID uint32
chainID ids.ID
closeFn func()
}

func setupSigner(t testing.TB) *testSigner {
Expand Down Expand Up @@ -55,11 +54,13 @@ func setupSigner(t testing.TB) *testSigner {
require.NoError(err)

s.client = NewClient(pb.NewSignerClient(conn))
s.closeFn = func() {

t.Cleanup(func() {
serverCloser.Stop()
_ = conn.Close()
_ = listener.Close()
}
})

return s
}

Expand All @@ -68,7 +69,6 @@ func TestInterface(t *testing.T) {
t.Run(name, func(t *testing.T) {
s := setupSigner(t)
test(t, s.client, s.sk, s.networkID, s.chainID)
s.closeFn()
})
}
}

0 comments on commit bb2357e

Please sign in to comment.