Skip to content

Commit 669f527

Browse files
code-hygiene: Clean up node shutdown in testing (cosmos#1073)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. --> ## Overview Closes: cosmos#1072 <!-- Please provide an explanation of the PR, including the appropriate context, background, goal, and rationale. If there is an issue with this information, please provide a tl;dr and link the issue. --> ## Checklist <!-- Please complete the checklist to ensure that the PR is ready to be reviewed. IMPORTANT: PRs should be left in Draft until the below checklist is completed. --> - [x] New and updated code has appropriate documentation - [x] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [x] Visual proof for any user facing features like CLI or documentation updates - [x] Linked issues closed with keywords
1 parent a6079b4 commit 669f527

10 files changed

+220
-131
lines changed

block/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (m *Manager) SetFraudProofService(fraudProofServ *fraudserv.ProofService) {
250250
}
251251

252252
func (m *Manager) ProcessFraudProof(ctx context.Context, cancel context.CancelFunc) {
253+
defer cancel()
253254
// subscribe to state fraud proof
254255
sub, err := m.executor.FraudService.Subscribe(types.StateFraudProofType)
255256
if err != nil {
@@ -281,7 +282,6 @@ func (m *Manager) ProcessFraudProof(ctx context.Context, cancel context.CancelFu
281282

282283
// halt chain
283284
m.logger.Info("verified fraud proof, halting chain")
284-
cancel()
285285
}
286286

287287
// SyncLoop is responsible for syncing blocks.

block/manager_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func TestInitialState(t *testing.T) {
3333
NextValidators: types.GetRandomValidatorSet(),
3434
}
3535

36-
ctx := context.Background()
36+
ctx, cancel := context.WithCancel(context.Background())
37+
defer cancel()
3738
es, _ := store.NewDefaultInMemoryKVStore()
3839
emptyStore := store.New(ctx, es)
3940

@@ -79,6 +80,9 @@ func TestInitialState(t *testing.T) {
7980
assert := assert.New(t)
8081
logger := log.TestingLogger()
8182
dalc := getMockDALC(logger)
83+
defer func() {
84+
require.NoError(t, dalc.Stop())
85+
}()
8286
dumbChan := make(chan struct{})
8387
agg, err := NewManager(key, conf, c.genesis, c.store, nil, nil, dalc, nil, logger, dumbChan)
8488
assert.NoError(err)

da/test/da_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ func doTestLifecycle(t *testing.T, dalc da.DataAvailabilityLayerClient) {
7474
err = dalc.Start()
7575
require.NoError(err)
7676

77-
err = dalc.Stop()
78-
require.NoError(err)
77+
defer func() {
78+
require.NoError(dalc.Stop())
79+
}()
7980
}
8081

8182
func TestDALC(t *testing.T) {
@@ -89,7 +90,8 @@ func TestDALC(t *testing.T) {
8990
func doTestDALC(t *testing.T, dalc da.DataAvailabilityLayerClient) {
9091
require := require.New(t)
9192
assert := assert.New(t)
92-
ctx := context.Background()
93+
ctx, cancel := context.WithCancel(context.Background())
94+
defer cancel()
9395

9496
// mock DALC will advance block height every 100ms
9597
conf := []byte{}
@@ -105,6 +107,9 @@ func doTestDALC(t *testing.T, dalc da.DataAvailabilityLayerClient) {
105107

106108
err = dalc.Start()
107109
require.NoError(err)
110+
defer func() {
111+
require.NoError(dalc.Stop())
112+
}()
108113

109114
// wait a bit more than mockDaBlockTime, so mock can "produce" some blocks
110115
time.Sleep(mockDaBlockTime + 20*time.Millisecond)
@@ -151,7 +156,8 @@ func TestRetrieve(t *testing.T) {
151156
}
152157

153158
func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {
154-
ctx := context.Background()
159+
ctx, cancel := context.WithCancel(context.Background())
160+
defer cancel()
155161
require := require.New(t)
156162
assert := assert.New(t)
157163

@@ -169,6 +175,9 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {
169175

170176
err = dalc.Start()
171177
require.NoError(err)
178+
defer func() {
179+
require.NoError(dalc.Stop())
180+
}()
172181

173182
// wait a bit more than mockDaBlockTime, so mock can "produce" some blocks
174183
time.Sleep(mockDaBlockTime + 20*time.Millisecond)

0 commit comments

Comments
 (0)