1616
1717package 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 ;
2620import java .util .ArrayList ;
2721import java .util .concurrent .Callable ;
2822import java .util .concurrent .CountDownLatch ;
3428import java .util .concurrent .TimeUnit ;
3529import 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+
3738import static org .junit .Assert .assertArrayEquals ;
3839import static org .junit .Assert .assertEquals ;
3940import 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