Skip to content

Commit 6b48302

Browse files
TransactionTest: expand runInReadTx closes cursor test, add for callInReadTx
1 parent 1690622 commit 6b48302

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,41 @@ public void testCallInReadTx() {
422422
}
423423

424424
@Test
425-
public void testRunInReadTxAndThenPut() {
425+
public void testRunInReadTx_closesActiveTxCursor() {
426426
final Box<TestEntity> box = getTestEntityBox();
427-
store.runInReadTx(box::count);
428-
// Verify that box does not hang on to the read-only TX by doing a put
427+
store.runInReadTx(box::count); // Call Box API that creates a reader/activeTxCursor
428+
// Verify that box does not hang on to the read-only TX: if it would, count() would re-use the cursor/tx from
429+
// above and not see the put object.
430+
putTestEntityAndExpectCount(1);
431+
432+
// Verify the same in case the runnable throws
433+
assertThrows(IllegalStateException.class, () -> store.runInReadTx(() -> {
434+
box.count();
435+
throw new IllegalStateException("Throw in transaction");
436+
}));
437+
putTestEntityAndExpectCount(2);
438+
}
439+
440+
@Test
441+
public void testCallInReadTx_closesActiveTxCursor() {
442+
final Box<TestEntity> box = getTestEntityBox();
443+
store.callInReadTx(box::count); // Call Box API that creates a reader/activeTxCursor
444+
// Verify that box does not hang on to the read-only TX: if it would, count() would re-use the cursor/tx from
445+
// above and not see the put object.
446+
putTestEntityAndExpectCount(1);
447+
448+
// Verify the same in case the callable throws
449+
assertThrows(IllegalStateException.class, () -> store.callInReadTx(() -> {
450+
box.count();
451+
throw new IllegalStateException("Throw in transaction");
452+
}));
453+
putTestEntityAndExpectCount(2);
454+
}
455+
456+
private void putTestEntityAndExpectCount(int expectedCount) {
457+
Box<TestEntity> box = getTestEntityBox();
429458
box.put(new TestEntity());
430-
assertEquals(1, box.count());
459+
assertEquals(expectedCount, box.count());
431460
}
432461

433462
@Test

0 commit comments

Comments
 (0)