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

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1040290 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2010-11-29 21:01:19 +00:00
parent 7f443f67ea
commit ef914b348a
3 changed files with 42 additions and 13 deletions

View File

@ -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

View File

@ -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();

View File

@ -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();
}