Skip to content

Commit e7971ab

Browse files
Justin Breitfellergreenrobot-team
authored andcommitted
Add a test that ensures object box transactions still succeed when used in rapid succession, when synchronized, on a bunch of thread pool threads. Currently, in ObjectBox 2.9.1, this fails.
1 parent e31e08c commit e7971ab

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
import org.junit.Ignore;
2020
import org.junit.Test;
2121

22+
import java.util.ArrayList;
2223
import java.util.concurrent.Callable;
2324
import java.util.concurrent.CountDownLatch;
25+
import java.util.concurrent.Future;
2426
import java.util.concurrent.LinkedBlockingQueue;
2527
import java.util.concurrent.TimeUnit;
2628
import java.util.concurrent.atomic.AtomicInteger;
2729

28-
import javax.annotation.Nullable;
29-
3030
import io.objectbox.exception.DbException;
3131
import io.objectbox.exception.DbExceptionListener;
3232
import io.objectbox.exception.DbMaxReadersExceededException;
33+
import io.objectbox.internal.ObjectBoxThreadPool;
3334

3435
import static org.junit.Assert.*;
3536

@@ -439,5 +440,23 @@ public void testCallInTxAsync_Error() throws InterruptedException {
439440
assertNotNull(result);
440441
}
441442

443+
@Test
444+
public void transactionsOnLargeThreadPool() throws Exception {
445+
//Create a bunch of transactions on a thread pool. We can even run them synchronously.
446+
ObjectBoxThreadPool pool = new ObjectBoxThreadPool(store);
447+
ArrayList<Future<Integer>> txTasks = new ArrayList<>(10000);
448+
for (int i = 0; i < 10000; i++) {
449+
final int txNumber = i;
450+
txTasks.add(pool.submit(() -> {
451+
synchronized (store) {
452+
return store.callInReadTx(() -> txNumber);
453+
}
454+
}));
455+
}
442456

457+
//Iterate through all the txTasks and make sure all transactions succeeded.
458+
for (Future<Integer> txTask : txTasks) {
459+
txTask.get(1, TimeUnit.MILLISECONDS);
460+
}
461+
}
443462
}

0 commit comments

Comments
 (0)