mirror of https://github.com/apache/lucene.git
use try-w-resources so Directory is closed even on assumption violation
This commit is contained in:
parent
0e9307bb84
commit
7ff31daeb0
|
@ -34,51 +34,51 @@ public abstract class BaseMergePolicyTestCase extends LuceneTestCase {
|
|||
protected abstract MergePolicy mergePolicy();
|
||||
|
||||
public void testForceMergeNotNeeded() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
final AtomicBoolean mayMerge = new AtomicBoolean(true);
|
||||
final MergeScheduler mergeScheduler = new SerialMergeScheduler() {
|
||||
@Override
|
||||
synchronized public void merge(IndexWriter writer, MergeTrigger trigger, boolean newMergesFound) throws IOException {
|
||||
if (mayMerge.get() == false) {
|
||||
MergePolicy.OneMerge merge = writer.getNextMerge();
|
||||
if (merge != null) {
|
||||
System.out.println("TEST: we should not need any merging, yet merge policy returned merge " + merge);
|
||||
throw new AssertionError();
|
||||
try (Directory dir = newDirectory()) {
|
||||
final AtomicBoolean mayMerge = new AtomicBoolean(true);
|
||||
final MergeScheduler mergeScheduler = new SerialMergeScheduler() {
|
||||
@Override
|
||||
synchronized public void merge(IndexWriter writer, MergeTrigger trigger, boolean newMergesFound) throws IOException {
|
||||
if (mayMerge.get() == false) {
|
||||
MergePolicy.OneMerge merge = writer.getNextMerge();
|
||||
if (merge != null) {
|
||||
System.out.println("TEST: we should not need any merging, yet merge policy returned merge " + merge);
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
super.merge(writer, trigger, newMergesFound);
|
||||
}
|
||||
};
|
||||
|
||||
MergePolicy mp = mergePolicy();
|
||||
assumeFalse("this test cannot tolerate random forceMerges", mp.toString().contains("MockRandomMergePolicy"));
|
||||
mp.setNoCFSRatio(random().nextBoolean() ? 0 : 1);
|
||||
|
||||
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
|
||||
iwc.setMergeScheduler(mergeScheduler);
|
||||
iwc.setMergePolicy(mp);
|
||||
|
||||
IndexWriter writer = new IndexWriter(dir, iwc);
|
||||
final int numSegments = TestUtil.nextInt(random(), 2, 20);
|
||||
for (int i = 0; i < numSegments; ++i) {
|
||||
final int numDocs = TestUtil.nextInt(random(), 1, 5);
|
||||
for (int j = 0; j < numDocs; ++j) {
|
||||
writer.addDocument(new Document());
|
||||
}
|
||||
|
||||
super.merge(writer, trigger, newMergesFound);
|
||||
writer.getReader().close();
|
||||
}
|
||||
};
|
||||
|
||||
MergePolicy mp = mergePolicy();
|
||||
assumeFalse("this test cannot tolerate random forceMerges", mp.toString().contains("MockRandomMergePolicy"));
|
||||
mp.setNoCFSRatio(random().nextBoolean() ? 0 : 1);
|
||||
|
||||
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
|
||||
iwc.setMergeScheduler(mergeScheduler);
|
||||
iwc.setMergePolicy(mp);
|
||||
|
||||
IndexWriter writer = new IndexWriter(dir, iwc);
|
||||
final int numSegments = TestUtil.nextInt(random(), 2, 20);
|
||||
for (int i = 0; i < numSegments; ++i) {
|
||||
final int numDocs = TestUtil.nextInt(random(), 1, 5);
|
||||
for (int j = 0; j < numDocs; ++j) {
|
||||
writer.addDocument(new Document());
|
||||
for (int i = 5; i >= 0; --i) {
|
||||
final int segmentCount = writer.getSegmentCount();
|
||||
final int maxNumSegments = i == 0 ? 1 : TestUtil.nextInt(random(), 1, 10);
|
||||
mayMerge.set(segmentCount > maxNumSegments);
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now forceMerge(maxNumSegments=" + maxNumSegments + ") vs segmentCount=" + segmentCount);
|
||||
}
|
||||
writer.forceMerge(maxNumSegments);
|
||||
}
|
||||
writer.getReader().close();
|
||||
writer.close();
|
||||
}
|
||||
for (int i = 5; i >= 0; --i) {
|
||||
final int segmentCount = writer.getSegmentCount();
|
||||
final int maxNumSegments = i == 0 ? 1 : TestUtil.nextInt(random(), 1, 10);
|
||||
mayMerge.set(segmentCount > maxNumSegments);
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now forceMerge(maxNumSegments=" + maxNumSegments + ") vs segmentCount=" + segmentCount);
|
||||
}
|
||||
writer.forceMerge(maxNumSegments);
|
||||
}
|
||||
writer.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue