diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java index f5e72f87afb..50f87ad0a19 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java @@ -92,6 +92,7 @@ import java.util.Random; import java.util.Set; import java.util.TimeZone; import java.util.TreeSet; +import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -1875,6 +1876,32 @@ public abstract class LuceneTestCase extends Assert { System.clearProperty(ConcurrentMergeScheduler.DEFAULT_CPU_CORE_COUNT_PROPERTY); } + private static ExecutorService executor; + + @BeforeClass + public static void setUpExecutorService() { + int threads = TestUtil.nextInt(random(), 1, 2); + executor = + new ThreadPoolExecutor( + threads, + threads, + 0L, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue(), + new NamedThreadFactory("LuceneTestCase")); + // uncomment to intensify LUCENE-3840 + // executor.prestartAllCoreThreads(); + if (VERBOSE) { + System.out.println("NOTE: Created shared ExecutorService with " + threads + " threads"); + } + } + + @AfterClass + public static void shutdownExecutorService() { + TestUtil.shutdownExecutorService(executor); + executor = null; + } + /** Create a new searcher over the reader. This searcher might randomly use threads. */ public static IndexSearcher newSearcher(IndexReader r) { return newSearcher(r, true); @@ -1938,30 +1965,14 @@ public abstract class LuceneTestCase extends Assert { ret.setSimilarity(classEnvRule.similarity); return ret; } else { - int threads = 0; - final ThreadPoolExecutor ex; - if (r.getReaderCacheHelper() == null || random.nextBoolean()) { + final ExecutorService ex; + if (random.nextBoolean()) { ex = null; } else { - threads = TestUtil.nextInt(random, 1, 8); - ex = - new ThreadPoolExecutor( - threads, - threads, - 0L, - TimeUnit.MILLISECONDS, - new LinkedBlockingQueue(), - new NamedThreadFactory("LuceneTestCase")); - // uncomment to intensify LUCENE-3840 - // ex.prestartAllCoreThreads(); - } - if (ex != null) { + ex = executor; if (VERBOSE) { - System.out.println( - "NOTE: newSearcher using ExecutorService with " + threads + " threads"); + System.out.println("NOTE: newSearcher using shared ExecutorService"); } - r.getReaderCacheHelper() - .addClosedListener(cacheKey -> TestUtil.shutdownExecutorService(ex)); } IndexSearcher ret; int maxDocPerSlice = random.nextBoolean() ? 1 : 1 + random.nextInt(1000);