Skip to content

Commit 0950fb3

Browse files
committed
refactor: use unittest.WithLock in TestBlockIndexByViewAndRetrieve
1 parent a0eacf4 commit 0950fb3

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

storage/store/blocks_test.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,17 @@ func TestBlockIndexByViewAndRetrieve(t *testing.T) {
116116
block := unittest.FullBlockFixture()
117117
prop := unittest.ProposalFromBlock(block)
118118

119-
// First store the block
120-
lctx := lockManager.NewContext()
121-
err := lctx.AcquireLock(storage.LockInsertBlock)
122-
require.NoError(t, err)
123-
124-
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
125-
err := blocks.BatchStore(lctx, rw, prop)
126-
if err != nil {
127-
return err
128-
}
129-
// Now index the block by view (requires LockInsertBlock)
130-
return operation.IndexCertifiedBlockByView(lctx, rw, block.View, block.ID())
119+
// First store the block and index by view
120+
unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
121+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
122+
err := blocks.BatchStore(lctx, rw, prop)
123+
if err != nil {
124+
return err
125+
}
126+
// Now index the block by view (requires LockInsertBlock)
127+
return operation.IndexCertifiedBlockByView(lctx, rw, block.View, block.ID())
128+
})
131129
})
132-
require.NoError(t, err)
133-
lctx.Release()
134130

135131
// Verify we can retrieve the block by view
136132
retrievedByView, err := blocks.ByView(block.View)
@@ -143,15 +139,13 @@ func TestBlockIndexByViewAndRetrieve(t *testing.T) {
143139
require.Equal(t, *prop, *retrievedProposalByView)
144140

145141
// Test that indexing the same view again returns ErrAlreadyExists
146-
lctx2 := lockManager.NewContext()
147-
err = lctx2.AcquireLock(storage.LockInsertBlock)
148-
require.NoError(t, err)
149-
150-
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
151-
return operation.IndexCertifiedBlockByView(lctx2, rw, block.View, block.ID())
142+
unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
143+
err := db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
144+
return operation.IndexCertifiedBlockByView(lctx, rw, block.View, block.ID())
145+
})
146+
require.ErrorIs(t, err, storage.ErrAlreadyExists)
147+
return nil
152148
})
153-
require.ErrorIs(t, err, storage.ErrAlreadyExists)
154-
lctx2.Release()
155149

156150
// Test that retrieving by non-existent view returns ErrNotFound
157151
_, err = blocks.ByView(block.View + 1000)

0 commit comments

Comments
 (0)