GeezerVison is showing github.com — TransactionTest: throttle unbound thread pool to limit resource consu… · hehuelet/objectbox-java@def827d
File tree
Expand file treeCollapse file tree
Open diff view settings
tests/objectbox-java-test/src/test/java/io/objectbox
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/TestUtils.java +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
33 | 33 |
|
34 | 34 | public class TestUtils { |
35 | 35 |
|
| 36 | + public static boolean isWindows() { |
| 37 | + final String osName = System.getProperty("os.name").toLowerCase(); |
| 38 | + return osName.contains("windows"); |
| 39 | + } |
| 40 | + |
36 | 41 | public static String loadFile(String filename) { |
37 | 42 | try { |
38 | 43 | InputStream in = openInputStream("/" + filename); |
|
Collapse file
Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/TransactionTest.java +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
29 | 29 | import java.util.concurrent.Executors; |
30 | 30 | import java.util.concurrent.Future; |
31 | 31 | import java.util.concurrent.LinkedBlockingQueue; |
| 32 | +import java.util.concurrent.ThreadPoolExecutor; |
32 | 33 | import java.util.concurrent.TimeUnit; |
33 | 34 | import java.util.concurrent.atomic.AtomicInteger; |
34 | 35 |
|
@@ -496,7 +497,8 @@ private void runThreadPoolReaderTest(Runnable runnable) throws Exception { |
496 | 497 | .noReaderThreadLocals() // This is the essential flag to make this test work |
497 | 498 | .build(); |
498 | 499 |
|
499 | | - // Unbounded thread pool so number of threads run exceeds max readers. |
| 500 | + // Unbounded (but throttled) thread pool so number of threads run exceeds max readers. |
| 501 | + int numThreads = TestUtils.isWindows() ? 300 : 1000; // Less on Windows; had some resource issues on Windows CI |
500 | 502 | ExecutorService pool = Executors.newCachedThreadPool(); |
501 | 503 |
|
502 | 504 | ArrayList<Future<Integer>> txTasks = new ArrayList<>(10000); |
@@ -510,6 +512,9 @@ private void runThreadPoolReaderTest(Runnable runnable) throws Exception { |
510 | 512 | return txNumber; |
511 | 513 | } |
512 | 514 | })); |
| 515 | + if (pool instanceof ThreadPoolExecutor && ((ThreadPoolExecutor) pool).getActiveCount() > numThreads) { |
| 516 | + Thread.sleep(1); // Throttle processing to limit thread resources |
| 517 | + } |
513 | 518 | } |
514 | 519 |
|
515 | 520 | //Iterate through all the txTasks and make sure all transactions succeeded. |
|