Skip to content

Commit 40aa3f8

Browse files
TransactionTest: simplify reader thread pool test.
Also cover reader via Box API.
1 parent 88c4dcb commit 40aa3f8

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@
1616

1717
package io.objectbox;
1818

19+
import io.objectbox.exception.DbException;
20+
import io.objectbox.exception.DbExceptionListener;
21+
import io.objectbox.exception.DbMaxReadersExceededException;
22+
import io.objectbox.internal.ObjectBoxThreadPool;
1923
import org.junit.Ignore;
2024
import org.junit.Test;
2125

2226
import java.util.ArrayList;
2327
import java.util.concurrent.Callable;
2428
import java.util.concurrent.CountDownLatch;
2529
import java.util.concurrent.ExecutorService;
26-
import java.util.concurrent.Executors;
2730
import java.util.concurrent.Future;
2831
import java.util.concurrent.LinkedBlockingQueue;
2932
import java.util.concurrent.TimeUnit;
3033
import java.util.concurrent.atomic.AtomicInteger;
3134

32-
import io.objectbox.exception.DbException;
33-
import io.objectbox.exception.DbExceptionListener;
34-
import io.objectbox.exception.DbMaxReadersExceededException;
35-
import io.objectbox.internal.ObjectBoxThreadPool;
36-
37-
import static org.junit.Assert.*;
35+
import static org.junit.Assert.assertArrayEquals;
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertFalse;
38+
import static org.junit.Assert.assertNotNull;
39+
import static org.junit.Assert.assertNotSame;
40+
import static org.junit.Assert.assertSame;
41+
import static org.junit.Assert.assertTrue;
42+
import static org.junit.Assert.fail;
3843

3944
public class TransactionTest extends AbstractObjectBoxTest {
4045

@@ -443,40 +448,55 @@ public void testCallInTxAsync_Error() throws InterruptedException {
443448
}
444449

445450
@Test
446-
public void transactionsOnUnboundedThreadPool() throws Exception {
447-
//Silence the unnecessary debug output and set the max readers
448-
resetBoxStoreWithoutDebugFlags(100);
449-
450-
runThreadPoolTransactionTest(new ObjectBoxThreadPool(store));
451+
public void runInReadTx_unboundedThreadPool() throws Exception {
452+
runThreadPoolReaderTest(
453+
() -> store.runInReadTx(() -> {
454+
})
455+
);
451456
}
452457

453458
@Test
454-
public void transactionsOnBoundedThreadPool() throws Exception {
455-
//Silence the unnecessary debug output and set the max readers
456-
int maxReaders = 100;
457-
resetBoxStoreWithoutDebugFlags(maxReaders);
459+
public void callInReadTx_unboundedThreadPool() throws Exception {
460+
runThreadPoolReaderTest(
461+
() -> store.callInReadTx(() -> 1)
462+
);
463+
}
458464

459-
runThreadPoolTransactionTest(Executors.newFixedThreadPool(maxReaders));
465+
@Test
466+
public void boxReader_unboundedThreadPool() throws Exception {
467+
runThreadPoolReaderTest(
468+
() -> {
469+
store.boxFor(TestEntity.class).count();
470+
store.closeThreadResources();
471+
}
472+
);
460473
}
461474

462-
private void resetBoxStoreWithoutDebugFlags(int maxReaders) {
463-
// Remove existing store
475+
/**
476+
* Tests that a reader is available again after a transaction is closed on a thread.
477+
* To not exceed max readers this test simply does not allow any two threads
478+
* to have an active transaction at the same time, e.g. there should always be only one active reader.
479+
*/
480+
private void runThreadPoolReaderTest(Runnable runnable) throws Exception {
481+
// Replace default store: transaction logging disabled and specific max readers.
464482
tearDown();
483+
store = createBoxStoreBuilder(null)
484+
.maxReaders(100)
485+
.debugFlags(0)
486+
.build();
465487

466-
BoxStoreBuilder builder = createBoxStoreBuilder(false);
467-
builder.maxReaders = maxReaders;
468-
builder.debugFlags = 0;
469-
store = builder.build();
470-
}
488+
// Unbounded thread pool so number of threads run exceeds max readers.
489+
ExecutorService pool = new ObjectBoxThreadPool(store);
471490

472-
private void runThreadPoolTransactionTest(ExecutorService pool) throws Exception {
473-
//Create a bunch of transactions on a thread pool. We can even run them synchronously.
474491
ArrayList<Future<Integer>> txTasks = new ArrayList<>(10000);
492+
final Object lock = new Object();
475493
for (int i = 0; i < 10000; i++) {
476494
final int txNumber = i;
477495
txTasks.add(pool.submit(() -> {
478-
synchronized (store) {
479-
return store.callInReadTx(() -> txNumber);
496+
// Lock to ensure no two threads have an active transaction at the same time.
497+
synchronized (lock) {
498+
runnable.run();
499+
return txNumber;
480500
}
481501
}));
482502
}

0 commit comments

Comments
 (0)