|
1 | 1 | /*
|
2 |
| - * Copyright 2017 ObjectBox Ltd. All rights reserved. |
| 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package io.objectbox;
|
18 | 18 |
|
| 19 | +import org.junit.Ignore; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.function.ThrowingRunnable; |
| 22 | + |
19 | 23 | import java.io.IOException;
|
20 | 24 | import java.util.ArrayList;
|
21 | 25 | import java.util.concurrent.Callable;
|
|
27 | 31 | import java.util.concurrent.ThreadPoolExecutor;
|
28 | 32 | import java.util.concurrent.TimeUnit;
|
29 | 33 | import java.util.concurrent.atomic.AtomicInteger;
|
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
30 | 35 |
|
31 | 36 | import io.objectbox.exception.DbException;
|
32 | 37 | import io.objectbox.exception.DbExceptionListener;
|
33 | 38 | import io.objectbox.exception.DbMaxReadersExceededException;
|
34 |
| -import org.junit.Ignore; |
35 |
| -import org.junit.Test; |
36 |
| -import org.junit.function.ThrowingRunnable; |
| 39 | +import io.objectbox.query.InternalQueryAccess; |
| 40 | +import io.objectbox.query.Query; |
| 41 | + |
37 | 42 |
|
38 | 43 | import static org.junit.Assert.assertArrayEquals;
|
39 | 44 | import static org.junit.Assert.assertEquals;
|
|
44 | 49 | import static org.junit.Assert.assertThrows;
|
45 | 50 | import static org.junit.Assert.assertTrue;
|
46 | 51 | import static org.junit.Assert.fail;
|
| 52 | +import static org.junit.Assume.assumeFalse; |
47 | 53 |
|
48 | 54 | public class TransactionTest extends AbstractObjectBoxTest {
|
49 | 55 |
|
@@ -315,6 +321,55 @@ private void assertThrowsTxClosed(ThrowingRunnable runnable) {
|
315 | 321 | assertEquals("Transaction is closed", ex.getMessage());
|
316 | 322 | }
|
317 | 323 |
|
| 324 | + @Test |
| 325 | + public void nativeCallInTx_storeIsClosed_throws() throws InterruptedException { |
| 326 | + // Ignore test on Windows, it was observed to crash with EXCEPTION_ACCESS_VIOLATION |
| 327 | + assumeFalse(TestUtils.isWindows()); |
| 328 | + |
| 329 | + System.out.println("NOTE This test will cause \"Transaction is still active\" and \"Irrecoverable memory error\" error logs!"); |
| 330 | + |
| 331 | + CountDownLatch callableIsReady = new CountDownLatch(1); |
| 332 | + CountDownLatch storeIsClosed = new CountDownLatch(1); |
| 333 | + CountDownLatch callableIsDone = new CountDownLatch(1); |
| 334 | + AtomicReference<Exception> callableException = new AtomicReference<>(); |
| 335 | + |
| 336 | + // Goal: be just passed closed checks on the Java side, about to call a native query API. |
| 337 | + // Then close the Store, then call the native API. The native API call should not crash the VM. |
| 338 | + Callable<Void> waitingCallable = () -> { |
| 339 | + Box<TestEntity> box = store.boxFor(TestEntity.class); |
| 340 | + Query<TestEntity> query = box.query().build(); |
| 341 | + // Obtain Cursor handle before closing the Store as getActiveTxCursor() has a closed check |
| 342 | + long cursorHandle = io.objectbox.InternalAccess.getActiveTxCursorHandle(box); |
| 343 | + |
| 344 | + callableIsReady.countDown(); |
| 345 | + try { |
| 346 | + if (!storeIsClosed.await(5, TimeUnit.SECONDS)) { |
| 347 | + throw new IllegalStateException("Store did not close within 5 seconds"); |
| 348 | + } |
| 349 | + // Call native query API within the transaction (opened by callInReadTx below) |
| 350 | + InternalQueryAccess.nativeFindFirst(query, cursorHandle); |
| 351 | + query.close(); |
| 352 | + } catch (Exception e) { |
| 353 | + callableException.set(e); |
| 354 | + } |
| 355 | + callableIsDone.countDown(); |
| 356 | + return null; |
| 357 | + }; |
| 358 | + new Thread(() -> store.callInReadTx(waitingCallable)).start(); |
| 359 | + |
| 360 | + callableIsReady.await(); |
| 361 | + store.close(); |
| 362 | + storeIsClosed.countDown(); |
| 363 | + |
| 364 | + if (!callableIsDone.await(10, TimeUnit.SECONDS)) { |
| 365 | + fail("Callable did not finish within 10 seconds"); |
| 366 | + } |
| 367 | + Exception exception = callableException.get(); |
| 368 | + assertTrue(exception instanceof IllegalStateException); |
| 369 | + // Note: the "State" at the end of the message may be different depending on platform, so only assert prefix |
| 370 | + assertTrue(exception.getMessage().startsWith("Illegal Store instance detected! This is a severe usage error that must be fixed.")); |
| 371 | + } |
| 372 | + |
318 | 373 | @Test
|
319 | 374 | public void testRunInTxRecursive() {
|
320 | 375 | final Box<TestEntity> box = getTestEntityBox();
|
|
0 commit comments