LUCENE-1535: use random seed for randomness, but print the seed on failure for reproducibility

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@741311 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-02-05 21:53:40 +00:00
parent 396cfd9fe1
commit dbe0898d63
31 changed files with 190 additions and 94 deletions

View File

@ -23,6 +23,13 @@
<import file="../../contrib-build.xml" />
<path id="test.classpath">
<path refid="classpath"/>
<pathelement location="../../../build/classes/test/"/>
<path refid="junit-path"/>
<pathelement location="${build.dir}/classes/java"/>
</path>
<target name="get-je-jar" unless="je.jar.exists">
<mkdir dir="lib" />
<get src="http://download.oracle.com/berkeley-db/je-${je.version}.zip"

View File

@ -23,11 +23,10 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import junit.framework.TestCase;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.LuceneTestCase;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
@ -46,7 +45,7 @@ import com.sleepycat.je.Transaction;
* Adapted from Andi Vajda's org.apache.lucene.db.DbStoreTest.
*
*/
public class JEStoreTest extends TestCase {
public class JEStoreTest extends LuceneTestCase {
protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");
protected Environment env;
@ -112,7 +111,9 @@ public class JEStoreTest extends TestCase {
final int count = 250;
final int LENGTH_MASK = 0xffff;
Random gen = new Random(1251971);
Random r = newRandom();
final long seed = r.nextLong();
Random gen = new Random(seed);
int totalLength = 0;
int duration;
Date end;
@ -171,7 +172,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -220,7 +221,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -257,7 +258,9 @@ public class JEStoreTest extends TestCase {
final int count = 250;
final int LENGTH_MASK = 0xffff;
Random gen = new Random(1251971);
Random r = newRandom();
final long seed = r.nextLong();
Random gen = new Random(seed);
int totalLength = 0;
int duration;
Date end;
@ -316,7 +319,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -357,7 +360,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -412,7 +415,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -486,7 +489,9 @@ public class JEStoreTest extends TestCase {
final int count = 250;
final int LENGTH_MASK = 0xffff;
Random gen = new Random(1251971);
Random r = newRandom();
final long seed = r.nextLong();
Random gen = new Random(seed);
int totalLength = 0;
int duration;
Date end;
@ -543,7 +548,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -593,7 +598,7 @@ public class JEStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new JEDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
String name = i + ".dat";

View File

@ -23,6 +23,13 @@
<import file="../../contrib-build.xml" />
<path id="test.classpath">
<path refid="classpath"/>
<pathelement location="../../../build/classes/test/"/>
<path refid="junit-path"/>
<pathelement location="${build.dir}/classes/java"/>
</path>
<target name="get-db-jar" unless="db.jar.exists">
<mkdir dir="lib" />
<get src="http://downloads.osafoundation.org/db/db-${db.version}.jar"

View File

@ -24,8 +24,6 @@ import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.Environment;
import com.sleepycat.db.Transaction;
@ -37,13 +35,14 @@ import com.sleepycat.db.DatabaseException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.LuceneTestCase;
/**
* Tests {@link DbDirectory}.
*
* Adapted from org.apache.lucene.StoreTest with larger files and random bytes.
*/
public class DbStoreTest extends TestCase {
public class DbStoreTest extends LuceneTestCase {
protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");
protected Environment env;
protected Database index, blocks;
@ -117,7 +116,10 @@ public class DbStoreTest extends TestCase {
final int count = 250;
final int LENGTH_MASK = 0xffff;
Random gen = new Random(1251971);
Random r = newRandom();
final long seed = r.nextLong();
Random gen = new Random(seed);
int totalLength = 0;
int duration;
Date end;
@ -177,7 +179,7 @@ public class DbStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new DbDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -227,7 +229,7 @@ public class DbStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new DbDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -267,7 +269,10 @@ public class DbStoreTest extends TestCase {
final int count = 250;
final int LENGTH_MASK = 0xffff;
Random gen = new Random(1251971);
Random r = newRandom();
final long seed = r.nextLong();
Random gen = new Random(seed);
int totalLength = 0;
int duration;
Date end;
@ -325,7 +330,7 @@ public class DbStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new DbDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {
@ -376,7 +381,7 @@ public class DbStoreTest extends TestCase {
txn = env.beginTransaction(null, null);
store = new DbDirectory(txn, index, blocks);
gen = new Random(1251971);
gen = new Random(seed);
start = new Date();
for (int i = 0; i < count; i++) {

View File

@ -64,5 +64,6 @@
<ant dir="bdb-je" target="javadocs" />
</target>
</project>

View File

@ -36,11 +36,10 @@ public class TestTrieRangeQuery extends LuceneTestCase
{
private static final long distance=66666;
private static Random rnd=new Random();
private static RAMDirectory directory;
private static IndexSearcher searcher;
private static final RAMDirectory directory;
private static final IndexSearcher searcher;
static {
try {
try {
directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
true, MaxFieldLength.UNLIMITED);
@ -136,6 +135,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
}
private void testRandomTrieAndClassicRangeQuery(final TrieUtils variant) throws Exception {
final Random rnd=newRandom();
String field="field"+variant.TRIE_BITS;
// 50 random tests, the tests may also return 0 results, if min>max, but this is ok
for (int i=0; i<50; i++) {
@ -185,6 +185,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
}
private void testRangeSplit(final TrieUtils variant) throws Exception {
final Random rnd=newRandom();
String field="ascfield"+variant.TRIE_BITS;
// 50 random tests
for (int i=0; i<50; i++) {
@ -225,6 +226,7 @@ public class TestTrieRangeQuery extends LuceneTestCase
}
private void testSorting(final TrieUtils variant) throws Exception {
final Random rnd=newRandom();
String field="field"+variant.TRIE_BITS;
// 10 random tests, the index order is ascending,
// so using a reverse sort field should retun descending documents

View File

@ -329,6 +329,9 @@ public class ConcurrentMergeScheduler extends MergeScheduler {
/** Used for testing */
public static boolean anyUnhandledExceptions() {
if (allInstances == null) {
throw new RuntimeException("setTestMode() was not called; often this is because your test case's setUp method fails to call super.setUp in LuceneTestCase");
}
synchronized(allInstances) {
final int count = allInstances.size();
// Make sure all outstanding threads are done so we see

View File

@ -29,7 +29,7 @@ import java.io.IOException;
public class TestAtomicUpdate extends LuceneTestCase {
private static final Analyzer ANALYZER = new SimpleAnalyzer();
private static final Random RANDOM = new Random();
private Random RANDOM;
public class MockIndexWriter extends IndexWriter {
@ -179,7 +179,7 @@ public class TestAtomicUpdate extends LuceneTestCase {
FSDirectory.
*/
public void testAtomicUpdates() throws Exception {
RANDOM = newRandom();
Directory directory;
// First in a RAM directory:

View File

@ -52,7 +52,7 @@ public class TestByteSlices extends LuceneTestCase {
int[] uptos = new int[NUM_STREAM];
int[] counters = new int[NUM_STREAM];
Random r = new Random(1);
Random r = newRandom();
ByteSliceReader reader = new ByteSliceReader();

View File

@ -710,7 +710,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
final List readers = Collections.synchronizedList(new ArrayList());
IndexReader firstReader = IndexReader.open(dir);
IndexReader reader = firstReader;
final Random rnd = new Random();
final Random rnd = newRandom();
ReaderThread[] threads = new ReaderThread[n];
final Set readersToClose = Collections.synchronizedSet(new HashSet());
@ -1113,7 +1113,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
IndexReader lastReader = IndexReader.open(indexDir2);
Random r = new Random(42);
Random r = newRandom();
for(int i=0;i<10;i++) {
int mod = r.nextInt(5);
modifyIndex(mod, lastReader.directory());

View File

@ -1411,7 +1411,7 @@ public class TestIndexWriter extends LuceneTestCase
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
writer.setRAMBufferSizeMB(0.5);
Random rand = new Random(31415);
Random rand = newRandom();
for(int i=0;i<3;i++) {
// First, docs where every term is unique (heavy on
// Posting instances)
@ -3442,7 +3442,7 @@ public class TestIndexWriter extends LuceneTestCase
}
}
Random r = new Random();
Random r;
private int nextInt(int lim) {
return r.nextInt(lim);
@ -3498,6 +3498,7 @@ public class TestIndexWriter extends LuceneTestCase
// LUCENE-510
public void testRandomUnicodeStrings() throws Throwable {
r = newRandom();
char[] buffer = new char[20];
char[] expected = new char[20];
@ -3525,6 +3526,7 @@ public class TestIndexWriter extends LuceneTestCase
// LUCENE-510
public void testIncrementalUnicodeStrings() throws Throwable {
r = newRandom();
char[] buffer = new char[20];
char[] expected = new char[20];

View File

@ -25,7 +25,6 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.util.*;
import java.lang.reflect.Array;
/**
@ -34,8 +33,6 @@ import java.lang.reflect.Array;
*/
public class TestLazyBug extends LuceneTestCase {
public static int BASE_SEED = 13;
public static int NUM_DOCS = 500;
public static int NUM_FIELDS = 100;
@ -62,10 +59,10 @@ public class TestLazyBug extends LuceneTestCase {
}
};
private static Directory makeIndex() throws RuntimeException {
private Directory makeIndex() throws RuntimeException {
Directory dir = new RAMDirectory();
try {
Random r = new Random(BASE_SEED + 42) ;
Random r = newRandom();
Analyzer analyzer = new SimpleAnalyzer();
IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
@ -89,12 +86,12 @@ public class TestLazyBug extends LuceneTestCase {
return dir;
}
public static void doTest(int[] docs) throws Exception {
public void doTest(int[] docs) throws Exception {
Directory dir = makeIndex();
IndexReader reader = IndexReader.open(dir);
for (int i = 0; i < docs.length; i++) {
Document d = reader.document(docs[i], SELECTOR);
String trash = d.get(MAGIC_FIELD);
d.get(MAGIC_FIELD);
List fields = d.getFields();
for (Iterator fi = fields.iterator(); fi.hasNext(); ) {

View File

@ -47,6 +47,7 @@ public class TestPayloads extends LuceneTestCase {
// Simple tests to test the Payload class
public void testPayload() throws Exception {
rnd = newRandom();
byte[] testData = "This is a test!".getBytes();
Payload payload = new Payload(testData);
assertEquals("Wrong payload length.", testData.length, payload.length());
@ -95,6 +96,7 @@ public class TestPayloads extends LuceneTestCase {
// Tests whether the DocumentWriter and SegmentMerger correctly enable the
// payload bit in the FieldInfo
public void testPayloadFieldBit() throws Exception {
rnd = newRandom();
Directory ram = new RAMDirectory();
PayloadAnalyzer analyzer = new PayloadAnalyzer();
IndexWriter writer = new IndexWriter(ram, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
@ -151,6 +153,7 @@ public class TestPayloads extends LuceneTestCase {
// Tests if payloads are correctly stored and loaded using both RamDirectory and FSDirectory
public void testPayloadsEncoding() throws Exception {
rnd = newRandom();
// first perform the test using a RAMDirectory
Directory dir = new RAMDirectory();
performTest(dir);
@ -333,13 +336,13 @@ public class TestPayloads extends LuceneTestCase {
}
private static Random rnd = new Random();
private Random rnd;
private static void generateRandomData(byte[] data) {
private void generateRandomData(byte[] data) {
rnd.nextBytes(data);
}
private static byte[] generateRandomData(int n) {
private byte[] generateRandomData(int n) {
byte[] data = new byte[n];
generateRandomData(data);
return data;
@ -473,6 +476,7 @@ public class TestPayloads extends LuceneTestCase {
}
public void testThreadSafety() throws IOException {
rnd = newRandom();
final int numThreads = 5;
final int numDocs = 50;
final ByteArrayPool pool = new ByteArrayPool(numThreads, 5);
@ -525,7 +529,7 @@ public class TestPayloads extends LuceneTestCase {
assertEquals(pool.size(), numThreads);
}
private static class PoolingPayloadTokenStream extends TokenStream {
private class PoolingPayloadTokenStream extends TokenStream {
private byte[] payload;
private boolean first;
private ByteArrayPool pool;

View File

@ -28,7 +28,7 @@ import java.io.File;
public class TestStressIndexing extends LuceneTestCase {
private static final Analyzer ANALYZER = new SimpleAnalyzer();
private static final Random RANDOM = new Random();
private Random RANDOM;
private static abstract class TimedThread extends Thread {
boolean failed;
@ -67,7 +67,7 @@ public class TestStressIndexing extends LuceneTestCase {
}
}
private static class IndexerThread extends TimedThread {
private class IndexerThread extends TimedThread {
IndexWriter writer;
public int count;
int nextID;
@ -164,6 +164,7 @@ public class TestStressIndexing extends LuceneTestCase {
FSDirectory.
*/
public void testStressIndexAndSearching() throws Exception {
RANDOM = newRandom();
// RAMDir
Directory directory = new MockRAMDirectory();

View File

@ -36,7 +36,7 @@ public class TestStressIndexing2 extends LuceneTestCase {
static int maxBufferedDocs=3;
static int seed=0;
static Random r = new Random(0);
Random r;
public class MockIndexWriter extends IndexWriter {
@ -53,6 +53,7 @@ public class TestStressIndexing2 extends LuceneTestCase {
}
public void testRandom() throws Throwable {
r = newRandom();
Directory dir1 = new MockRAMDirectory();
// dir1 = FSDirectory.getDirectory("foofoofoo");
Directory dir2 = new MockRAMDirectory();
@ -69,6 +70,7 @@ public class TestStressIndexing2 extends LuceneTestCase {
public void testMultiConfig() throws Throwable {
// test lots of smaller different params together
r = newRandom();
for (int i=0; i<100; i++) { // increase iterations for better testing
sameFieldOrder=r.nextBoolean();
autoCommit=r.nextBoolean();

View File

@ -58,7 +58,7 @@ class RepeatingTokenStream extends TokenStream {
public class TestTermdocPerf extends LuceneTestCase {
void addDocs(Directory dir, final int ndocs, String field, final String val, final int maxTF, final float percentDocs) throws IOException {
final Random random = new Random(0);
final Random random = newRandom();
final RepeatingTokenStream ts = new RepeatingTokenStream(val);
Analyzer analyzer = new Analyzer() {

View File

@ -26,7 +26,7 @@ import org.apache.lucene.document.*;
public class TestTransactions extends LuceneTestCase
{
private static final Random RANDOM = new Random();
private Random RANDOM;
private static volatile boolean doFail;
private class RandomFailure extends MockRAMDirectory.Failure {
@ -68,7 +68,7 @@ public class TestTransactions extends LuceneTestCase
}
}
private static class IndexerThread extends TimedThread {
private class IndexerThread extends TimedThread {
Directory dir1;
Directory dir2;
Object lock;
@ -183,6 +183,7 @@ public class TestTransactions extends LuceneTestCase
}
public void testTransactions() throws Throwable {
RANDOM = newRandom();
MockRAMDirectory dir1 = new MockRAMDirectory();
MockRAMDirectory dir2 = new MockRAMDirectory();
dir1.setPreventDoubleWrite(false);

View File

@ -32,7 +32,7 @@ public class BaseTestRangeFilter extends LuceneTestCase {
public static final boolean F = false;
public static final boolean T = true;
Random rand = new Random(101); // use a set seed to test is deterministic
protected Random rand;
/**
* Collation interacts badly with hyphens -- collation produces different
@ -83,10 +83,12 @@ public class BaseTestRangeFilter extends LuceneTestCase {
public BaseTestRangeFilter(String name) {
super(name);
rand = newRandom();
build(signedIndex);
build(unsignedIndex);
}
public BaseTestRangeFilter() {
rand = newRandom();
build(signedIndex);
build(unsignedIndex);
}

View File

@ -152,7 +152,7 @@ public class TestBoolean2 extends LuceneTestCase {
}
public void testRandomQueries() throws Exception {
Random rnd = new Random(0);
Random rnd = newRandom();
String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
@ -163,7 +163,7 @@ public class TestBoolean2 extends LuceneTestCase {
// increase number of iterations for more complete testing
for (int i=0; i<1000; i++) {
int level = rnd.nextInt(3);
BooleanQuery q1 = randBoolQuery(new Random(i), level, field, vals, null);
BooleanQuery q1 = randBoolQuery(new Random(rnd.nextLong()), level, field, vals, null);
// Can't sort by relevance since floating point numbers may not quite
// match up.

View File

@ -296,7 +296,7 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
}
public void testRandomQueries() throws Exception {
final Random rnd = new Random(0);
final Random rnd = newRandom();
String field="data";
String[] vals = {"1","2","3","4","5","6","A","Z","B","Y","Z","X","foo"};
@ -319,9 +319,10 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {
// increase number of iterations for more complete testing
for (int i=0; i<1000; i++) {
int lev = rnd.nextInt(maxLev);
BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, null);
// BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, minNrCB);
BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, null);
final long seed = rnd.nextLong();
BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);
// BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB);
BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);
// only set minimumNumberShouldMatch on the top level query since setting
// at a lower level can change the score.
minNrCB.postCreate(q2);

View File

@ -25,7 +25,6 @@ import java.util.Random;
import java.util.TreeMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
@ -38,6 +37,7 @@ import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
/**
* Unit test for sorting code.
@ -45,7 +45,7 @@ import org.apache.lucene.store.RAMDirectory;
*/
public class TestCustomSearcherSort
extends TestCase
extends LuceneTestCase
implements Serializable {
private Directory index = null;
@ -71,7 +71,7 @@ implements Serializable {
throws IOException {
RAMDirectory indexStore = new RAMDirectory ();
IndexWriter writer = new IndexWriter (indexStore, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
RandomGen random = new RandomGen();
RandomGen random = new RandomGen(newRandom());
for (int i=0; i<INDEX_SIZE; ++i) { // don't decrease; if to low the problem doesn't show up
Document doc = new Document();
if((i%5)!=0) { // some documents must not have an entry in the first sort field
@ -93,8 +93,9 @@ implements Serializable {
* Create index and query for test cases.
*/
public void setUp() throws Exception {
index = getIndex();
query = new TermQuery( new Term("content", "test"));
super.setUp();
index = getIndex();
query = new TermQuery( new Term("content", "test"));
}
/**
@ -164,7 +165,6 @@ implements Serializable {
ScoreDoc[] resultSort = searcher.search (query, null, 1000, sort).scoreDocs;
checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates
String lf = System.getProperty("line.separator", "\n");
// besides the sorting both sets of hits must be identical
for(int hitid=0;hitid<resultSort.length; ++hitid) {
Integer idHitDate = new Integer(resultSort[hitid].doc); // document ID from sorted search
@ -263,12 +263,15 @@ implements Serializable {
}
}
private class RandomGen {
private Random random = new Random(0); // to generate some arbitrary contents
private Calendar base = new GregorianCalendar(1980, 1, 1);
RandomGen(Random random) {
this.random = random;
}
private Random random;
private Calendar base = new GregorianCalendar(1980, 1, 1);
// Just to generate some different Lucene Date strings
private String getLuceneDate() {
return DateTools.timeToString(base.getTimeInMillis() + random.nextInt() - Integer.MIN_VALUE, DateTools.Resolution.DAY);
}
// Just to generate some different Lucene Date strings
private String getLuceneDate() {
return DateTools.timeToString(base.getTimeInMillis() + random.nextInt() - Integer.MIN_VALUE, DateTools.Resolution.DAY);
}
}
}

View File

@ -38,7 +38,7 @@ import org.apache.lucene.document.Field;
* @version $Id$
*/
public class TestScorerPerf extends LuceneTestCase {
Random r = new Random(0);
Random r;
boolean validate = true; // set to false when doing performance testing
BitSet[] sets;
@ -306,6 +306,7 @@ public class TestScorerPerf extends LuceneTestCase {
public void testConjunctions() throws Exception {
// test many small sets... the bugs will be found on boundary conditions
r = newRandom();
createDummySearcher();
validate=true;
sets=randBitSets(1000,10);
@ -318,6 +319,7 @@ public class TestScorerPerf extends LuceneTestCase {
int bigIter=10;
public void testConjunctionPerf() throws Exception {
r = newRandom();
createDummySearcher();
validate=false;
sets=randBitSets(32,1000000);
@ -331,6 +333,7 @@ public class TestScorerPerf extends LuceneTestCase {
}
public void testNestedConjunctionPerf() throws Exception {
r = newRandom();
createDummySearcher();
validate=false;
sets=randBitSets(32,1000000);
@ -345,6 +348,7 @@ public class TestScorerPerf extends LuceneTestCase {
public void testConjunctionTerms() throws Exception {
r = newRandom();
validate=false;
RAMDirectory dir = new RAMDirectory();
System.out.println("Creating index");
@ -361,6 +365,7 @@ public class TestScorerPerf extends LuceneTestCase {
}
public void testNestedConjunctionTerms() throws Exception {
r = newRandom();
validate=false;
RAMDirectory dir = new RAMDirectory();
System.out.println("Creating index");
@ -378,6 +383,7 @@ public class TestScorerPerf extends LuceneTestCase {
public void testSloppyPhrasePerf() throws Exception {
r = newRandom();
validate=false;
RAMDirectory dir = new RAMDirectory();
System.out.println("Creating index");

View File

@ -180,7 +180,7 @@ implements Serializable {
return new IndexSearcher (indexStore);
}
public static String getRandomNumberString(int num, int low, int high) {
public String getRandomNumberString(int num, int low, int high) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++) {
sb.append(getRandomNumber(low, high));
@ -188,11 +188,11 @@ implements Serializable {
return sb.toString();
}
public static String getRandomCharString(int num) {
public String getRandomCharString(int num) {
return getRandomCharString(num, 48, 122);
}
public static String getRandomCharString(int num, int start, int end) {
public String getRandomCharString(int num, int start, int end) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++) {
sb.append(new Character((char) getRandomNumber(start, end)));
@ -200,9 +200,9 @@ implements Serializable {
return sb.toString();
}
static Random r = new Random();
Random r;
public static int getRandomNumber(final int low, final int high) {
public int getRandomNumber(final int low, final int high) {
int randInt = (Math.abs(r.nextInt()) % (high - low)) + low;
@ -284,6 +284,7 @@ implements Serializable {
* Test String sorting: small queue to many matches, multi field sort, reverse sort
*/
public void testStringSort() throws IOException, ParseException {
r = newRandom();
ScoreDoc[] result = null;
IndexSearcher searcher = getFullStrings();
sort.setSort(new SortField[] {

View File

@ -35,7 +35,7 @@ public class TestStressSort extends LuceneTestCase {
// NOTE: put seed in here to make failures
// deterministic, but do not commit with a seed (to
// better test):
private final Random r = new Random();
private Random r;
private Directory dir, dir2, dir3;
private IndexSearcher searcherMultiSegment;
private IndexSearcher searcherFewSegment;
@ -67,7 +67,6 @@ public class TestStressSort extends LuceneTestCase {
// NOTE: put seed in here to make failures
// deterministic, but do not commit with a seed (to
// better test):
final Random r = new Random();
dir = new MockRAMDirectory();
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
writer.setMaxBufferedDocs(17);
@ -180,6 +179,7 @@ public class TestStressSort extends LuceneTestCase {
}
public void testSort() throws Throwable {
r = newRandom();
// reverse & not
// all types

View File

@ -34,7 +34,7 @@ import java.io.IOException;
* @version $Id$
*/
public class TestThreadSafe extends LuceneTestCase {
Random r = new Random();
Random r;
Directory dir1;
Directory dir2;
@ -48,7 +48,7 @@ public class TestThreadSafe extends LuceneTestCase {
final int iter;
final Random rand;
// pass in random in case we want to make things reproducable
public Thr(int iter, Random rand, int level) {
public Thr(int iter, Random rand) {
this.iter = iter;
this.rand = rand;
}
@ -132,7 +132,7 @@ public class TestThreadSafe extends LuceneTestCase {
void doTest(int iter, int nThreads) throws Exception {
Thr[] tarr = new Thr[nThreads];
for (int i=0; i<nThreads; i++) {
tarr[i] = new Thr(iter, new Random(), 1);
tarr[i] = new Thr(iter, new Random(r.nextLong()));
tarr[i].start();
}
for (int i=0; i<nThreads; i++) {
@ -144,6 +144,7 @@ public class TestThreadSafe extends LuceneTestCase {
}
public void testLazyLoadThreadSafety() throws Exception{
r = newRandom();
dir1 = new RAMDirectory();
// test w/ field sizes bigger than the buffer of an index input
buildDir(dir1, 15, 5, 2000);

View File

@ -157,7 +157,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {
public void testSetBufferSize() throws IOException {
File indexDir = new File(System.getProperty("tempDir"), "testSetBufferSize");
MockFSDirectory dir = new MockFSDirectory(indexDir);
MockFSDirectory dir = new MockFSDirectory(indexDir, newRandom());
try {
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
writer.setUseCompoundFile(false);
@ -205,11 +205,12 @@ public class TestBufferedIndexInput extends LuceneTestCase {
List allIndexInputs = new ArrayList();
Random rand = new Random(788);
Random rand;
private Directory dir;
public MockFSDirectory(File path) throws IOException {
public MockFSDirectory(File path, Random rand) throws IOException {
this.rand = rand;
lockFactory = new NoLockFactory();
dir = FSDirectory.getDirectory(path);
}

View File

@ -28,7 +28,6 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.FSDirectory;
public class TestWindowsMMap extends LuceneTestCase {
@ -37,7 +36,7 @@ public class TestWindowsMMap extends LuceneTestCase {
public void setUp() throws Exception {
super.setUp();
random = new Random();
random = newRandom();
System.setProperty("org.apache.lucene.FSDirectory.class", "org.apache.lucene.store.MMapDirectory");
}

View File

@ -20,6 +20,7 @@ package org.apache.lucene.util;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.index.ConcurrentMergeScheduler;
import junit.framework.TestCase;
import java.util.Random;
/** Base class for all Lucene unit tests. Currently the
* only added functionality over JUnit's TestCase is
@ -54,4 +55,48 @@ public abstract class LuceneTestCase extends TestCase {
fail("ConcurrentMergeScheduler hit unhandled exceptions");
}
}
/**
* Returns a {@link Random} instance for generating random numbers during the test.
* The random seed is logged during test execution and printed to System.out on any failure
* for reproducing the test using {@link #newRandom(long)} with the recorded seed
*.
*/
public Random newRandom() {
if (seed != null) {
throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test");
}
return newRandom(seedRnd.nextLong());
}
/**
* Returns a {@link Random} instance for generating random numbers during the test.
* If an error occurs in the test that is not reproducible, you can use this method to
* initialize the number generator with the seed that was printed out during the failing test.
*/
public Random newRandom(long seed) {
if (this.seed != null) {
throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test");
}
this.seed = new Long(seed);
return new Random(seed);
}
protected void runTest() throws Throwable {
try {
seed = null;
super.runTest();
} catch (Throwable e) {
if (seed != null) {
System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed);
}
throw e;
}
}
// recorded seed
protected Long seed = null;
// static members
private static final Random seedRnd = new Random();
}

View File

@ -17,16 +17,14 @@
package org.apache.lucene.util;
import junit.framework.TestCase;
import java.util.Random;
import java.util.BitSet;
/**
* @version $Id$
*/
public class TestOpenBitSet extends TestCase {
static Random rand = new Random();
public class TestOpenBitSet extends LuceneTestCase {
Random rand;
void doGet(BitSet a, OpenBitSet b) {
int max = a.size();
@ -184,17 +182,20 @@ public class TestOpenBitSet extends TestCase {
// large enough to flush obvious bugs, small enough to run in <.5 sec as part of a
// larger testsuite.
public void testSmall() {
rand = newRandom();
doRandomSets(1200,1000, 1);
doRandomSets(1200,1000, 2);
}
public void testBig() {
// uncomment to run a bigger test (~2 minutes).
// rand = newRandom();
// doRandomSets(2000,200000, 1);
// doRandomSets(2000,200000, 2);
}
public void testEquals() {
rand = newRandom();
OpenBitSet b1 = new OpenBitSet(1111);
OpenBitSet b2 = new OpenBitSet(2222);
assertTrue(b1.equals(b2));
@ -218,6 +219,7 @@ public class TestOpenBitSet extends TestCase {
public void testBitUtils()
{
rand = newRandom();
long num = 100000;
assertEquals( 5, BitUtil.ntz(num) );
assertEquals( 5, BitUtil.ntz2(num) );

View File

@ -45,13 +45,12 @@ public class TestPriorityQueue
public void testPQ()
throws Exception
{
testPQ(10000);
testPQ(10000, newRandom());
}
public static void testPQ(int count)
public static void testPQ(int count, Random gen)
{
PriorityQueue pq = new IntegerQueue(count);
Random gen = new Random();
int sum = 0, sum2 = 0;
for (int i = 0; i < count; i++)

View File

@ -16,7 +16,6 @@ package org.apache.lucene.util;
* limitations under the License.
*/
import org.apache.lucene.util.LuceneTestCase;
import java.util.Random;
/**
@ -74,7 +73,7 @@ public class TestSmallFloat extends LuceneTestCase {
}
public void testFloatToByte() {
Random rand = new Random(0);
Random rand = newRandom();
// up iterations for more exhaustive test after changing something
for (int i=0; i<100000; i++) {
float f = Float.intBitsToFloat(rand.nextInt());