}
@Test
public void testDirectoryReferenceCounter() throws CorruptIndexException, LockObtainFailedException, IOException, InterruptedException {
Directory directory = wrap(new RAMDirectory());
DirectoryReferenceFileGC gc = new DirectoryReferenceFileGC();
gc.init();
IndexInputCloser closer = new IndexInputCloser();
closer.init();
DirectoryReferenceCounter counter = new DirectoryReferenceCounter(directory, gc, closer);
IndexWriterConfig conf = new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer());
IndexWriter writer = new IndexWriter(counter, conf);
int size = 100;
IndexReader[] readers = new IndexReader[size];
for (int i = 0; i < size; i++) {
writer.addDocument(getDoc());
writer.forceMerge(1);
readers[i] = DirectoryReader.open(writer, true);
}
for (int i = 0; i < size; i++) {
assertEquals(i + 1, readers[i].numDocs());
checkReader(readers[i], i);
}
String[] listAll = directory.listAll();
for (int i = 0; i < size - 1; i++) {
readers[i].close();
}
for (int i = 0; i < 1000; i++) {
gc.run();
Thread.sleep(1);
}
IndexReader last = readers[size - 1];
assertEquals(100, last.numDocs());
assertTrue(listAll.length > directory.listAll().length);
last.close();
writer.close();
gc.close();
}