diff --git a/lucene/core/src/test/org/apache/lucene/index/TestReaderClosed.java b/lucene/core/src/test/org/apache/lucene/index/TestReaderClosed.java index 981a0ee5f4d..a6b1c653cba 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestReaderClosed.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestReaderClosed.java @@ -79,7 +79,9 @@ public class TestReaderClosed extends LuceneTestCase { assertTrue(reader.getRefCount() > 0); LeafReader wrappedReader = new ParallelLeafReader(getOnlyLeafReader(reader)); - IndexSearcher searcher = newSearcher(wrappedReader); + // We wrap with a OwnCacheKeyMultiReader so that closing the underlying reader + // does not terminate the threadpool (if that index searcher uses one) + IndexSearcher searcher = newSearcher(new OwnCacheKeyMultiReader(wrappedReader)); TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true); searcher.search(query, 5); @@ -93,7 +95,9 @@ public class TestReaderClosed extends LuceneTestCase { ace = (AlreadyClosedException) t; } } - assertNotNull("Query failed, but not due to an AlreadyClosedException", ace); + if (ace == null) { + throw new AssertionError("Query failed, but not due to an AlreadyClosedException", e); + } assertEquals( "this IndexReader cannot be used anymore as one of its child readers was closed", ace.getMessage() diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/OwnCacheKeyMultiReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/OwnCacheKeyMultiReader.java new file mode 100644 index 00000000000..45aabfe2a4b --- /dev/null +++ b/lucene/test-framework/src/java/org/apache/lucene/index/OwnCacheKeyMultiReader.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.index; + +import java.io.IOException; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; + +import org.apache.lucene.util.IOUtils; + +/** + * A {@link MultiReader} that has its own cache key, occasionally useful for + * testing purposes. + */ +public final class OwnCacheKeyMultiReader extends MultiReader { + + private final Set readerClosedListeners = new CopyOnWriteArraySet<>(); + + private final CacheHelper cacheHelper = new CacheHelper() { + private final CacheKey cacheKey = new CacheKey(); + + @Override + public CacheKey getKey() { + return cacheKey; + } + + @Override + public void addClosedListener(ClosedListener listener) { + readerClosedListeners.add(listener); + } + + }; + + /** Sole constructor. */ + public OwnCacheKeyMultiReader(IndexReader... subReaders) throws IOException { + super(subReaders); + } + + @Override + public CacheHelper getReaderCacheHelper() { + return cacheHelper; + } + + @Override + void notifyReaderClosedListeners(Throwable th) throws IOException { + synchronized(readerClosedListeners) { + for(ClosedListener listener : readerClosedListeners) { + try { + listener.onClose(cacheHelper.getKey()); + } catch (Throwable t) { + if (th == null) { + th = t; + } else { + th.addSuppressed(t); + } + } + } + IOUtils.reThrow(th); + } + } + +}