Skip to content

Commit e66064b

Browse files
committed
add test case for verifying wrong lock is held
1 parent 072904d commit e66064b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

storage/operation/protocol_kv_store_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,45 @@ func TestIndexProtocolKVStore_MissingLock(t *testing.T) {
180180
})
181181
require.Error(t, err)
182182
require.Contains(t, err.Error(), storage.LockInsertBlock)
183+
})
184+
}
185+
186+
// TestInsertProtocolKVStore_WrongLock tests that InsertProtocolKVStore fails when holding wrong locks.
187+
func TestInsertProtocolKVStore_WrongLock(t *testing.T) {
188+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
189+
lockManager := storage.NewTestingLockManager()
190+
expected := &flow.PSKeyValueStoreData{
191+
Version: 2,
192+
Data: unittest.RandomBytes(32),
193+
}
194+
195+
kvStoreStateID := unittest.IdentifierFixture()
183196

197+
// Test with LockFinalizeBlock (wrong lock)
198+
err := unittest.WithLock(t, lockManager, storage.LockFinalizeBlock, func(lctx lockctx.Context) error {
199+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
200+
return operation.InsertProtocolKVStore(lctx, rw, kvStoreStateID, expected)
201+
})
202+
})
203+
require.Error(t, err)
204+
require.Contains(t, err.Error(), storage.LockInsertBlock)
205+
})
206+
}
207+
208+
// TestIndexProtocolKVStore_WrongLock tests that IndexProtocolKVStore fails when holding wrong locks.
209+
func TestIndexProtocolKVStore_WrongLock(t *testing.T) {
210+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
211+
lockManager := storage.NewTestingLockManager()
212+
kvStoreStateID := unittest.IdentifierFixture()
213+
blockID := unittest.IdentifierFixture()
214+
215+
// Test with LockFinalizeBlock (wrong lock)
216+
err := unittest.WithLock(t, lockManager, storage.LockFinalizeBlock, func(lctx lockctx.Context) error {
217+
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
218+
return operation.IndexProtocolKVStore(lctx, rw, blockID, kvStoreStateID)
219+
})
220+
})
221+
require.Error(t, err)
222+
require.Contains(t, err.Error(), storage.LockInsertBlock)
184223
})
185224
}

0 commit comments

Comments
 (0)