LUCENE-7410: Make TestReaderClosed pass if the IndexSearcher wraps a threadpool.

This commit is contained in:
Adrien Grand 2017-03-01 19:12:14 +01:00
parent 1cfa048643
commit 540a237231
2 changed files with 82 additions and 2 deletions

View File

@ -79,7 +79,9 @@ public class TestReaderClosed extends LuceneTestCase {
assertTrue(reader.getRefCount() > 0); assertTrue(reader.getRefCount() > 0);
LeafReader wrappedReader = new ParallelLeafReader(getOnlyLeafReader(reader)); 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); TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
searcher.search(query, 5); searcher.search(query, 5);
@ -93,7 +95,9 @@ public class TestReaderClosed extends LuceneTestCase {
ace = (AlreadyClosedException) t; 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( assertEquals(
"this IndexReader cannot be used anymore as one of its child readers was closed", "this IndexReader cannot be used anymore as one of its child readers was closed",
ace.getMessage() ace.getMessage()

View File

@ -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<ClosedListener> 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);
}
}
}