psalagnac commented on code in PR #2619: URL: https://github.com/apache/solr/pull/2619#discussion_r1705724005
########## solr/solrj/src/test/org/apache/solr/common/util/ExecutorUtilTest.java: ########## @@ -108,6 +111,48 @@ public void testExecutorUtilAwaitsTerminationWhenTaskRespectsInterupt() throws E } } + @Test + public void testCMDCAwareCachedThreadPool() { + // 5 threads max, unbounded queue + ExecutorService executor = + ExecutorUtil.newMDCAwareCachedThreadPool( + 5, Integer.MAX_VALUE, new NamedThreadFactory("test")); + + AtomicInteger concurrentTasks = new AtomicInteger(); + AtomicInteger maxConcurrentTasks = new AtomicInteger(); + int taskCount = random().nextInt(100); + + for (int i = 0; i < taskCount; i++) { + String core = "id_" + random().nextLong(); + + Callable<Void> task = + () -> { + // ensure we never have too many concurrent tasks + int concurrent = concurrentTasks.incrementAndGet(); + assertTrue(concurrent <= 5); + maxConcurrentTasks.getAndAccumulate(concurrent, Math::max); + + // assert MDC context is copied from the parent thread that submitted the task + assertEquals(core, MDC.get("core")); + + // Sleep for a couple of millis before considering the task is done + int delay = random().nextInt(10); + Thread.sleep(delay); + concurrentTasks.decrementAndGet(); + return null; + }; + + MDCLoggingContext.setCoreName(core); + executor.submit(task); + } + + ExecutorUtil.shutdownAndAwaitTermination(executor); + + // assert the pool was actually multithreaded. Since we submitted many tasks, + // all the threads should have been started + assertEquals(5, maxConcurrentTasks.get()); Review Comment: Theoretically yes. But that's very unlikely as we submit 1000 tasks. I consider a decent implementation of the thread pool should always start the 5 requested threads. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org