dir1.close();
}
// Stress test reopen during addIndexes
public void testDuringAddIndexes() throws Exception {
MockDirectoryWrapper dir1 = newDirectory();
final IndexWriter writer = new IndexWriter(
dir1,
newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).
setMergePolicy(newLogMergePolicy(2))
);
writer.setInfoStream(infoStream);
((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(2);
// create the index
createIndexNoClose(false, "test", writer);
writer.commit();
final Directory[] dirs = new Directory[10];
for (int i=0;i<10;i++) {
dirs[i] = new MockDirectoryWrapper(random, new RAMDirectory(dir1));
}
IndexReader r = writer.getReader();
final int NUM_THREAD = 5;
final float SECONDS = 0.5f;
final long endTime = (long) (System.currentTimeMillis() + 1000.*SECONDS);
final List<Throwable> excs = Collections.synchronizedList(new ArrayList<Throwable>());
final Thread[] threads = new Thread[NUM_THREAD];
for(int i=0;i<NUM_THREAD;i++) {
threads[i] = new Thread() {
@Override
public void run() {
do {
try {
writer.addIndexes(dirs);
writer.maybeMerge();
} catch (Throwable t) {
excs.add(t);
throw new RuntimeException(t);
}
} while(System.currentTimeMillis() < endTime);
}
};
threads[i].setDaemon(true);
threads[i].start();
}
int lastCount = 0;
while(System.currentTimeMillis() < endTime) {
IndexReader r2 = r.reopen();
if (r2 != r) {
r.close();
r = r2;
}
Query q = new TermQuery(new Term("indexname", "test"));
IndexSearcher searcher = newSearcher(r);
final int count = searcher.search(q, 10).totalHits;
searcher.close();
assertTrue(count >= lastCount);
lastCount = count;
}
for(int i=0;i<NUM_THREAD;i++) {
threads[i].join();
}
// final check
IndexReader r2 = r.reopen();
if (r2 != r) {
r.close();
r = r2;
}
Query q = new TermQuery(new Term("indexname", "test"));
IndexSearcher searcher = newSearcher(r);
final int count = searcher.search(q, 10).totalHits;
searcher.close();
assertTrue(count >= lastCount);
assertEquals(0, excs.size());
r.close();
assertEquals(0, dir1.getOpenDeletedFiles().size());
writer.close();
dir1.close();
}