Skip to content

Apache Lucene open-source search software

Release default LRUQueryCache as early as possible in tests (#16740)

Luca Cavanna8557a5c29cf3
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCaseParent.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCaseParent.java
index 0a2a784b2be..b1cee33890f 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCaseParent.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCaseParent.java
@@ -165,6 +165,7 @@ import org.apache.lucene.util.automaton.Operations;
import org.apache.lucene.util.automaton.RegExp;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
@@ -2536,36 +2537,52 @@ public abstract sealed class LuceneTestCaseParent extends Assert
private static final QueryCache DEFAULT_QUERY_CACHE = IndexSearcher.getDefaultQueryCache();
private static final QueryCachingPolicy DEFAULT_CACHING_POLICY =
IndexSearcher.getDefaultQueryCachingPolicy();
- private static final List<LRUQueryCache> queryCacheList = new ArrayList<>();
+ private static LRUQueryCache classQueryCache;
- @Before
- public void overrideTestDefaultQueryCache() {
- // Make sure each test method has its own cache
- overrideDefaultQueryCache();
+ private static LRUQueryCache createAndOverrideDefaultQueryCache() {
+ LRUQueryCache lruQueryCache =
+ new LRUQueryCache(10000, 1 << 25, _ -> true, Float.POSITIVE_INFINITY);
+ IndexSearcher.setDefaultQueryCache(lruQueryCache);
+ IndexSearcher.setDefaultQueryCachingPolicy(MAYBE_CACHE_POLICY);
+ return lruQueryCache;
}
@BeforeClass
- public static void overrideDefaultQueryCache() {
+ public static void overrideClassDefaultQueryCache() {
// we need to reset the query cache in an @BeforeClass so that tests that
// instantiate an IndexSearcher in an @BeforeClass method use a fresh new cache
- LRUQueryCache queryCacheTemp =
- new LRUQueryCache(10000, 1 << 25, _ -> true, Float.POSITIVE_INFINITY);
- queryCacheList.add(queryCacheTemp);
- IndexSearcher.setDefaultQueryCache(queryCacheTemp);
- IndexSearcher.setDefaultQueryCachingPolicy(MAYBE_CACHE_POLICY);
+ classQueryCache = createAndOverrideDefaultQueryCache();
}
@AfterClass
- public static void resetDefaultQueryCache() {
+ public static void resetClassDefaultQueryCache() throws IOException {
IndexSearcher.setDefaultQueryCache(DEFAULT_QUERY_CACHE);
IndexSearcher.setDefaultQueryCachingPolicy(DEFAULT_CACHING_POLICY);
- for (int i = 0; i < queryCacheList.size(); i++) {
- try {
- queryCacheList.get(i).close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ if (classQueryCache != null) {
+ classQueryCache.clear();
+ }
+ IOUtils.close(classQueryCache);
+ classQueryCache = null;
+ }
+
+ private LRUQueryCache methodQueryCache;
+
+ @Before
+ public void overrideTestDefaultQueryCache() {
+ methodQueryCache = createAndOverrideDefaultQueryCache();
+ }
+
+ @After
+ public void resetTestDefaultQueryCache() throws IOException {
+ // Restore the class-level cache so TestRules and @AfterClass still see a
+ // test-scoped cache rather than the process-wide production default.
+ IndexSearcher.setDefaultQueryCache(classQueryCache);
+ IndexSearcher.setDefaultQueryCachingPolicy(MAYBE_CACHE_POLICY);
+ if (methodQueryCache != null) {
+ methodQueryCache.clear();
}
+ IOUtils.close(methodQueryCache);
+ methodQueryCache = null;
}
@BeforeClass