Close index readers in tests (#12544)

There are a few places where tests don't close index readers. This has
not caused problems so far, but it becomes an issue when the reader gets
an executor, because its shutdown happens as a closing listener of the
reader. This has become more evident since we now offload sequential
execution to the executor. If there's an executor, but it's never used,
no threads are created, and no threads are leaked. If we do use the
executor, and the reader is not closed, the test leaks threads.
This commit is contained in:
Luca Cavanna 2023-09-08 14:55:01 +02:00 committed by GitHub
parent ef42af65f2
commit a7202e2e6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 17 deletions

View File

@ -849,11 +849,10 @@ public class TestSort extends LuceneTestCase {
}
public void testRewrite() throws IOException {
try (Directory dir = newDirectory()) {
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
try (Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
DirectoryReader reader = writer.getReader()) {
IndexSearcher searcher = newSearcher(reader);
LongValuesSource longSource = LongValuesSource.constant(1L);
Sort sort = new Sort(longSource.getSortField(false));

View File

@ -30,7 +30,6 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.util.LuceneTestCase;
@ -52,7 +51,7 @@ public class TestForceNoBulkScoringQuery extends LuceneTestCase {
public void testRewrite() throws IOException {
try (Directory dir = new ByteBuffersDirectory();
try (Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(new StandardAnalyzer()))) {
Document doc = new Document();
@ -60,18 +59,18 @@ public class TestForceNoBulkScoringQuery extends LuceneTestCase {
iw.addDocument(doc);
iw.commit();
IndexReader reader = DirectoryReader.open(dir);
try (IndexReader reader = DirectoryReader.open(dir)) {
PrefixQuery pq = new PrefixQuery(new Term("field", "term"));
ForceNoBulkScoringQuery q = new ForceNoBulkScoringQuery(pq);
PrefixQuery pq = new PrefixQuery(new Term("field", "term"));
ForceNoBulkScoringQuery q = new ForceNoBulkScoringQuery(pq);
assertEquals(q.getWrappedQuery(), pq);
assertEquals(q.getWrappedQuery(), pq);
Query rewritten = q.rewrite(newSearcher(reader));
assertTrue(rewritten instanceof ForceNoBulkScoringQuery);
Query rewritten = q.rewrite(newSearcher(reader));
assertTrue(rewritten instanceof ForceNoBulkScoringQuery);
Query inner = ((ForceNoBulkScoringQuery) rewritten).getWrappedQuery();
assertNotEquals(inner, pq);
Query inner = ((ForceNoBulkScoringQuery) rewritten).getWrappedQuery();
assertNotEquals(inner, pq);
}
}
}
}

View File

@ -60,15 +60,22 @@ public class TestPayloadSpans extends LuceneTestCase {
protected IndexReader indexReader;
private IndexReader closeIndexReader;
private Directory directory;
private PayloadHelper helper;
@Override
public void setUp() throws Exception {
super.setUp();
PayloadHelper helper = new PayloadHelper();
helper = new PayloadHelper();
searcher = helper.setUp(random(), similarity, 1000);
indexReader = searcher.getIndexReader();
}
@Override
public void tearDown() throws Exception {
helper.tearDown();
super.tearDown();
}
public void testSpanTermQuery() throws Exception {
SpanTermQuery stq;
Spans spans;