diff --git a/engine/src/main/java/io/camunda/zeebe/process/test/engine/db/InMemoryDbTransaction.java b/engine/src/main/java/io/camunda/zeebe/process/test/engine/db/InMemoryDbTransaction.java index 3891846fd..467b2d8e9 100644 --- a/engine/src/main/java/io/camunda/zeebe/process/test/engine/db/InMemoryDbTransaction.java +++ b/engine/src/main/java/io/camunda/zeebe/process/test/engine/db/InMemoryDbTransaction.java @@ -73,6 +73,10 @@ public void put(final FullyQualifiedKey fullyQualifiedKey, final DbValue value) public byte[] get(final FullyQualifiedKey fullyQualifiedKey) { final Bytes key = fullyQualifiedKey.getKeyBytes(); + if (deletedKeys.contains(key)) { + return null; + } + final Bytes valueInCache = transactionCache.get(key); if (valueInCache != null) { @@ -108,6 +112,7 @@ public InMemoryDbIterator newIterator() { @Override public boolean contains(final FullyQualifiedKey fullyQualifiedKey) { final Bytes keyBytes = fullyQualifiedKey.getKeyBytes(); - return transactionCache.containsKey(keyBytes) || database.containsKey(keyBytes); + return !deletedKeys.contains(keyBytes) + && (transactionCache.containsKey(keyBytes) || database.containsKey(keyBytes)); } } diff --git a/engine/src/test/java/io/camunda/zeebe/process/test/engine/db/InMemoryZeebeDbTransactionTest.java b/engine/src/test/java/io/camunda/zeebe/process/test/engine/db/InMemoryZeebeDbTransactionTest.java index 3d15101d7..4220267b7 100644 --- a/engine/src/test/java/io/camunda/zeebe/process/test/engine/db/InMemoryZeebeDbTransactionTest.java +++ b/engine/src/test/java/io/camunda/zeebe/process/test/engine/db/InMemoryZeebeDbTransactionTest.java @@ -440,6 +440,49 @@ void shouldWriteAndDeleteSameKeyValuePairInTransaction() { assertThat(oneColumnFamily.exists(oneKey)).isFalse(); } + @Test + void shouldAllowDeleteAndInsertInTransaction() throws Exception { + // given + oneKey.wrapLong(1); + oneValue.wrapLong(-1L); + twoValue.wrapLong(-2L); + oneColumnFamily.insert(oneKey, oneValue); + transactionContext.getCurrentTransaction().commit(); + + // when + transactionContext.runInTransaction( + () -> { + oneColumnFamily.deleteExisting(oneKey); + oneColumnFamily.insert(oneKey, twoValue); + }); + + // then + assertThat(oneColumnFamily.get(oneKey).getValue()).isEqualTo(twoValue.getValue()); + } + + @Test + void shouldNotGetByKeyIfDeletedInTransaction() throws Exception { + // given + oneKey.wrapLong(1); + oneValue.wrapLong(-1L); + twoValue.wrapLong(-2L); + oneColumnFamily.insert(oneKey, oneValue); + transactionContext.getCurrentTransaction().commit(); + + // when + transactionContext.runInTransaction( + () -> { + oneColumnFamily.deleteExisting(oneKey); + + if (oneColumnFamily.get(oneKey) != null) { + fail("Should not be able to get deleted key."); + } + }); + + // then + assertThat(oneColumnFamily.get(oneKey)).isNull(); + } + @Test void shouldNotCommitOnError() { // given