18
18
19
19
import io .objectbox .exception .DbException ;
20
20
import org .junit .Test ;
21
+ import org .junit .function .ThrowingRunnable ;
21
22
22
23
import java .io .File ;
23
24
import java .util .concurrent .Callable ;
25
+ import java .util .concurrent .RejectedExecutionException ;
24
26
25
27
import static org .junit .Assert .assertEquals ;
26
28
import static org .junit .Assert .assertFalse ;
27
29
import static org .junit .Assert .assertNotNull ;
28
30
import static org .junit .Assert .assertNotSame ;
29
31
import static org .junit .Assert .assertSame ;
32
+ import static org .junit .Assert .assertThrows ;
30
33
import static org .junit .Assert .assertTrue ;
31
34
import static org .junit .Assert .fail ;
32
35
@@ -39,12 +42,72 @@ public void testUnalignedMemoryAccess() {
39
42
40
43
@ Test
41
44
public void testClose () {
45
+ BoxStore store = this .store ;
42
46
assertFalse (store .isClosed ());
43
47
store .close ();
44
48
assertTrue (store .isClosed ());
45
49
46
50
// Double close should be fine
47
51
store .close ();
52
+
53
+ // Internal thread pool is shut down.
54
+ assertTrue (store .internalThreadPool ().isShutdown ());
55
+ assertTrue (store .internalThreadPool ().isTerminated ());
56
+
57
+ // Can still obtain a box (but not use it).
58
+ store .boxFor (TestEntity .class );
59
+ store .closeThreadResources ();
60
+ //noinspection ResultOfMethodCallIgnored
61
+ store .getObjectBrowserPort ();
62
+ store .isObjectBrowserRunning ();
63
+ //noinspection ResultOfMethodCallIgnored
64
+ store .isDebugRelations ();
65
+ store .internalQueryAttempts ();
66
+ store .internalFailedReadTxAttemptCallback ();
67
+ //noinspection ResultOfMethodCallIgnored
68
+ store .getSyncClient ();
69
+ store .setSyncClient (null );
70
+
71
+ // Methods using the native store should throw.
72
+ assertThrowsStoreIsClosed (store ::sizeOnDisk );
73
+ assertThrowsStoreIsClosed (store ::beginTx );
74
+ assertThrowsStoreIsClosed (store ::beginReadTx );
75
+ assertThrowsStoreIsClosed (store ::isReadOnly );
76
+ assertThrowsStoreIsClosed (store ::removeAllObjects );
77
+ assertThrowsStoreIsClosed (() -> store .runInTx (() -> {
78
+ }));
79
+ assertThrowsStoreIsClosed (() -> store .runInReadTx (() -> {
80
+ }));
81
+ assertThrowsStoreIsClosed (() -> store .callInReadTxWithRetry (() -> null ,
82
+ 3 , 1 , true ));
83
+ assertThrowsStoreIsClosed (() -> store .callInReadTx (() -> null ));
84
+ assertThrowsStoreIsClosed (() -> store .callInTx (() -> null ));
85
+ // callInTxNoException wraps in RuntimeException
86
+ RuntimeException runtimeException = assertThrows (RuntimeException .class , () -> store .callInTxNoException (() -> null ));
87
+ assertEquals ("java.lang.IllegalStateException: Store is closed" , runtimeException .getMessage ());
88
+ // Internal thread pool is shut down as part of closing store, should no longer accept new work.
89
+ assertThrows (RejectedExecutionException .class , () -> store .runInTxAsync (() -> {}, null ));
90
+ assertThrows (RejectedExecutionException .class , () -> store .callInTxAsync (() -> null , null ));
91
+ assertThrowsStoreIsClosed (store ::diagnose );
92
+ assertThrowsStoreIsClosed (() -> store .validate (0 , false ));
93
+ assertThrowsStoreIsClosed (store ::cleanStaleReadTransactions );
94
+ assertThrowsStoreIsClosed (store ::subscribe );
95
+ assertThrowsStoreIsClosed (() -> store .subscribe (TestEntity .class ));
96
+ assertThrowsStoreIsClosed (store ::startObjectBrowser );
97
+ assertThrowsStoreIsClosed (() -> store .startObjectBrowser (12345 ));
98
+ assertThrowsStoreIsClosed (() -> store .startObjectBrowser ("" ));
99
+ // assertThrowsStoreIsClosed(store::stopObjectBrowser); // Requires mocking, not testing for now.
100
+ assertThrowsStoreIsClosed (() -> store .setDbExceptionListener (null ));
101
+ // Internal thread pool is shut down as part of closing store, should no longer accept new work.
102
+ assertThrows (RejectedExecutionException .class , () -> store .internalScheduleThread (() -> {}));
103
+ assertThrowsStoreIsClosed (() -> store .setDebugFlags (0 ));
104
+ assertThrowsStoreIsClosed (() -> store .panicModeRemoveAllObjects (TestEntity_ .__ENTITY_ID ));
105
+ assertThrowsStoreIsClosed (store ::getNativeStore );
106
+ }
107
+
108
+ private void assertThrowsStoreIsClosed (ThrowingRunnable runnable ) {
109
+ IllegalStateException ex = assertThrows (IllegalStateException .class , runnable );
110
+ assertEquals ("Store is closed" , ex .getMessage ());
48
111
}
49
112
50
113
@ Test
0 commit comments