16
16
17
17
package io .objectbox ;
18
18
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 ;
26
20
import java .util .ArrayList ;
27
21
import java .util .concurrent .Callable ;
28
22
import java .util .concurrent .CountDownLatch ;
34
28
import java .util .concurrent .TimeUnit ;
35
29
import java .util .concurrent .atomic .AtomicInteger ;
36
30
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
+
37
38
import static org .junit .Assert .assertArrayEquals ;
38
39
import static org .junit .Assert .assertEquals ;
39
40
import static org .junit .Assert .assertFalse ;
@@ -534,4 +535,66 @@ private void runThreadPoolReaderTest(Runnable runnable) throws Exception {
534
535
txTask .get (1 , TimeUnit .MINUTES ); // 1s would be enough for normally, but use 1 min to allow debug sessions
535
536
}
536
537
}
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
+ }
537
600
}
0 commit comments