1818
1919import io .objectbox .exception .DbException ;
2020import org .junit .Test ;
21+ import org .junit .function .ThrowingRunnable ;
2122
2223import java .io .File ;
2324import java .util .concurrent .Callable ;
25+ import java .util .concurrent .RejectedExecutionException ;
2426
2527import static org .junit .Assert .assertEquals ;
2628import static org .junit .Assert .assertFalse ;
2729import static org .junit .Assert .assertNotNull ;
2830import static org .junit .Assert .assertNotSame ;
2931import static org .junit .Assert .assertSame ;
32+ import static org .junit .Assert .assertThrows ;
3033import static org .junit .Assert .assertTrue ;
3134import static org .junit .Assert .fail ;
3235
@@ -39,12 +42,72 @@ public void testUnalignedMemoryAccess() {
3942
4043 @ Test
4144 public void testClose () {
45+ BoxStore store = this .store ;
4246 assertFalse (store .isClosed ());
4347 store .close ();
4448 assertTrue (store .isClosed ());
4549
4650 // Double close should be fine
4751 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 ());
48111 }
49112
50113 @ Test
0 commit comments