Skip to content

Commit fbd0eba

Browse files
committed
refactor: use unittest.WithLock in TestBlockIndexByHeightAndRetrieve for first two lock operations
1 parent c79bbf8 commit fbd0eba

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

storage/store/blocks_test.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package store_test
33
import (
44
"testing"
55

6+
"github.com/jordanschalm/lockctx"
67
"github.com/stretchr/testify/require"
78

89
"github.com/onflow/flow-go/module/metrics"
@@ -63,26 +64,18 @@ func TestBlockIndexByHeightAndRetrieve(t *testing.T) {
6364
prop := unittest.ProposalFromBlock(block)
6465

6566
// First store the block
66-
lctx := lockManager.NewContext()
67-
err := lctx.AcquireLock(storage.LockInsertBlock)
68-
require.NoError(t, err)
69-
70-
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
71-
return blocks.BatchStore(lctx, rw, prop)
67+
unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
68+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
69+
return blocks.BatchStore(lctx, rw, prop)
70+
})
7271
})
73-
require.NoError(t, err)
74-
lctx.Release()
7572

7673
// Now index the block by height (requires LockFinalizeBlock)
77-
lctx2 := lockManager.NewContext()
78-
err = lctx2.AcquireLock(storage.LockFinalizeBlock)
79-
require.NoError(t, err)
80-
81-
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
82-
return operation.IndexFinalizedBlockByHeight(lctx2, rw, block.Height, block.ID())
74+
unittest.WithLock(t, lockManager, storage.LockFinalizeBlock, func(lctx2 lockctx.Context) error {
75+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
76+
return operation.IndexFinalizedBlockByHeight(lctx2, rw, block.Height, block.ID())
77+
})
8378
})
84-
require.NoError(t, err)
85-
lctx2.Release()
8679

8780
// Verify we can retrieve the block by height
8881
retrievedByHeight, err := blocks.ByHeight(block.Height)

0 commit comments

Comments
 (0)