Skip to content

Apache Lucene open-source search software

fix false random test failure; make private random instance for RandomIndexWriter and MockDirWrapper so tests are reproducible from seeds when using eg CMS

Michael McCandlessef914b348ac5
diff --git a/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java b/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java
index c864e1e478e..3033bdbc946 100644
--- a/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java
+++ b/lucene/src/test/org/apache/lucene/index/RandomIndexWriter.java
@@ -50,7 +50,10 @@ public class RandomIndexWriter implements Closeable {
public MockIndexWriter(Random r,Directory dir, IndexWriterConfig conf) throws IOException {
super(dir, conf);
- this.r = r;
+ // must make a private random since our methods are
+ // called from different threads; else test failures may
+ // not be reproducible from the original seed
+ this.r = new Random(r.nextInt());
}
@Override
diff --git a/lucene/src/test/org/apache/lucene/search/BaseTestRangeFilter.java b/lucene/src/test/org/apache/lucene/search/BaseTestRangeFilter.java
index ae03273366e..75833c2683e 100644
--- a/lucene/src/test/org/apache/lucene/search/BaseTestRangeFilter.java
+++ b/lucene/src/test/org/apache/lucene/search/BaseTestRangeFilter.java
@@ -127,20 +127,43 @@ public class BaseTestRangeFilter extends LuceneTestCase {
doc.add(idField);
doc.add(randField);
doc.add(bodyField);
-
- for (int d = minId; d <= maxId; d++) {
- idField.setValue(pad(d));
- int r = index.allowNegativeRandomInts ? random.nextInt() : random
+
+ int minCount = 0;
+ int maxCount = 0;
+
+ while(true) {
+
+ for (int d = minId; d <= maxId; d++) {
+ idField.setValue(pad(d));
+ int r = index.allowNegativeRandomInts ? random.nextInt() : random
.nextInt(Integer.MAX_VALUE);
- if (index.maxR < r) {
- index.maxR = r;
+ if (index.maxR < r) {
+ index.maxR = r;
+ maxCount = 1;
+ } else if (index.maxR == r) {
+ maxCount++;
+ }
+
+ if (r < index.minR) {
+ index.minR = r;
+ minCount = 1;
+ } else if (r == index.minR) {
+ minCount++;
+ }
+ randField.setValue(pad(r));
+ bodyField.setValue("body");
+ writer.addDocument(doc);
}
- if (r < index.minR) {
- index.minR = r;
+
+ if (minCount == 1 && maxCount == 1) {
+ // our subclasses rely on only 1 doc having the min or
+ // max, so, we loop until we satisfy that. it should be
+ // exceedingly rare (Yonik calculates 1 in ~429,000)
+ // times) that this loop requires more than one try:
+ break;
}
- randField.setValue(pad(r));
- bodyField.setValue("body");
- writer.addDocument(doc);
+
+ // try again
}
IndexReader ir = writer.getReader();
diff --git a/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java
index fd9f895e3a5..bea2c6eded4 100644
--- a/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java
+++ b/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java
@@ -80,7 +80,10 @@ public class MockDirectoryWrapper extends Directory {
public MockDirectoryWrapper(Random random, Directory delegate) {
this.delegate = delegate;
- this.randomState = random;
+ // must make a private random since our methods are
+ // called from different threads; else test failures may
+ // not be reproducible from the original seed
+ this.randomState = new Random(random.nextInt());
init();
}