diff --git a/tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java b/tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java index 17431b64..503623db 100644 --- a/tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java +++ b/tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java @@ -19,17 +19,20 @@ import org.junit.Ignore; import org.junit.Test; +import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.Nullable; - import io.objectbox.exception.DbException; import io.objectbox.exception.DbExceptionListener; import io.objectbox.exception.DbMaxReadersExceededException; +import io.objectbox.internal.ObjectBoxThreadPool; import static org.junit.Assert.*; @@ -439,5 +442,48 @@ public void testCallInTxAsync_Error() throws InterruptedException { assertNotNull(result); } + @Test + public void transactionsOnUnboundedThreadPool() throws Exception { + //Silence the unnecessary debug output and set the max readers + resetBoxStoreWithoutDebugFlags(100); + + runThreadPoolTransactionTest(new ObjectBoxThreadPool(store)); + } + + @Test + public void transactionsOnBoundedThreadPool() throws Exception { + //Silence the unnecessary debug output and set the max readers + int maxReaders = 100; + resetBoxStoreWithoutDebugFlags(maxReaders); + + runThreadPoolTransactionTest(Executors.newFixedThreadPool(maxReaders)); + } + + private void resetBoxStoreWithoutDebugFlags(int maxReaders) { + // Remove existing store + tearDown(); + + BoxStoreBuilder builder = createBoxStoreBuilder(false); + builder.maxReaders = maxReaders; + builder.debugFlags = 0; + store = builder.build(); + } + + private void runThreadPoolTransactionTest(ExecutorService pool) throws Exception { + //Create a bunch of transactions on a thread pool. We can even run them synchronously. + ArrayList> txTasks = new ArrayList<>(10000); + for (int i = 0; i < 10000; i++) { + final int txNumber = i; + txTasks.add(pool.submit(() -> { + synchronized (store) { + return store.callInReadTx(() -> txNumber); + } + })); + } + //Iterate through all the txTasks and make sure all transactions succeeded. + for (Future txTask : txTasks) { + txTask.get(1, TimeUnit.SECONDS); + } + } } \ No newline at end of file