Skip to content

Commit 8f80445

Browse files
TransactionTest: assert run/callInTx forward exceptions in callback.
1 parent 45c517c commit 8f80445

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

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

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
package io.objectbox;
1818

19-
import io.objectbox.exception.DbException;
20-
import io.objectbox.exception.DbExceptionListener;
21-
import io.objectbox.exception.DbMaxReadersExceededException;
22-
import org.junit.Ignore;
23-
import org.junit.Test;
24-
import org.junit.function.ThrowingRunnable;
25-
19+
import java.io.IOException;
2620
import java.util.ArrayList;
2721
import java.util.concurrent.Callable;
2822
import java.util.concurrent.CountDownLatch;
@@ -34,6 +28,13 @@
3428
import java.util.concurrent.TimeUnit;
3529
import java.util.concurrent.atomic.AtomicInteger;
3630

31+
import io.objectbox.exception.DbException;
32+
import io.objectbox.exception.DbExceptionListener;
33+
import io.objectbox.exception.DbMaxReadersExceededException;
34+
import org.junit.Ignore;
35+
import org.junit.Test;
36+
import org.junit.function.ThrowingRunnable;
37+
3738
import static org.junit.Assert.assertArrayEquals;
3839
import static org.junit.Assert.assertEquals;
3940
import static org.junit.Assert.assertFalse;
@@ -534,4 +535,66 @@ private void runThreadPoolReaderTest(Runnable runnable) throws Exception {
534535
txTask.get(1, TimeUnit.MINUTES); // 1s would be enough for normally, but use 1 min to allow debug sessions
535536
}
536537
}
538+
539+
@Test
540+
public void runInTx_forwardsException() {
541+
// Exception from callback is forwarded.
542+
RuntimeException e = assertThrows(
543+
RuntimeException.class,
544+
() -> store.runInTx(() -> {
545+
throw new RuntimeException("Thrown inside callback");
546+
})
547+
);
548+
assertEquals("Thrown inside callback", e.getMessage());
549+
550+
// Can create a new transaction afterward.
551+
store.runInTx(() -> store.boxFor(TestEntity.class).count());
552+
}
553+
554+
@Test
555+
public void runInReadTx_forwardsException() {
556+
// Exception from callback is forwarded.
557+
RuntimeException e = assertThrows(
558+
RuntimeException.class,
559+
() -> store.runInReadTx(() -> {
560+
throw new RuntimeException("Thrown inside callback");
561+
})
562+
);
563+
assertEquals("Thrown inside callback", e.getMessage());
564+
565+
// Can create a new transaction afterward.
566+
store.runInReadTx(() -> store.boxFor(TestEntity.class).count());
567+
}
568+
569+
@Test
570+
public void callInTx_forwardsException() throws Exception {
571+
// Exception from callback is forwarded.
572+
Exception e = assertThrows(
573+
Exception.class,
574+
() -> store.callInTx(() -> {
575+
throw new Exception("Thrown inside callback");
576+
})
577+
);
578+
assertEquals("Thrown inside callback", e.getMessage());
579+
580+
// Can create a new transaction afterward.
581+
store.callInTx(() -> store.boxFor(TestEntity.class).count());
582+
}
583+
584+
@Test
585+
public void callInReadTx_forwardsException() {
586+
// Exception from callback is forwarded, but wrapped inside a RuntimeException.
587+
RuntimeException e = assertThrows(
588+
RuntimeException.class,
589+
() -> store.callInReadTx(() -> {
590+
throw new IOException("Thrown inside callback");
591+
})
592+
);
593+
assertEquals("Callable threw exception", e.getMessage());
594+
assertTrue(e.getCause() instanceof IOException);
595+
assertEquals("Thrown inside callback", e.getCause().getMessage());
596+
597+
// Can create a new transaction afterward.
598+
store.callInReadTx(() -> store.boxFor(TestEntity.class).count());
599+
}
537600
}

0 commit comments

Comments
 (0)