From dbe0898d63ef48113fac6f75cdbf27f5d5d1c0fb Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 5 Feb 2009 21:53:40 +0000 Subject: [PATCH] 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 --- contrib/db/bdb-je/build.xml | 7 +++ .../apache/lucene/store/je/JEStoreTest.java | 31 +++++++------ contrib/db/bdb/build.xml | 7 +++ .../apache/lucene/store/db/DbStoreTest.java | 23 ++++++---- contrib/db/build.xml | 1 + .../search/trie/TestTrieRangeQuery.java | 10 +++-- .../index/ConcurrentMergeScheduler.java | 3 ++ .../apache/lucene/index/TestAtomicUpdate.java | 4 +- .../apache/lucene/index/TestByteSlices.java | 2 +- .../lucene/index/TestIndexReaderReopen.java | 4 +- .../apache/lucene/index/TestIndexWriter.java | 6 ++- .../org/apache/lucene/index/TestLazyBug.java | 11 ++--- .../org/apache/lucene/index/TestPayloads.java | 12 +++-- .../lucene/index/TestStressIndexing.java | 5 ++- .../lucene/index/TestStressIndexing2.java | 4 +- .../apache/lucene/index/TestTermdocPerf.java | 2 +- .../apache/lucene/index/TestTransactions.java | 5 ++- .../lucene/search/BaseTestRangeFilter.java | 4 +- .../apache/lucene/search/TestBoolean2.java | 4 +- .../search/TestBooleanMinShouldMatch.java | 9 ++-- .../lucene/search/TestCustomSearcherSort.java | 27 ++++++----- .../apache/lucene/search/TestScorerPerf.java | 8 +++- .../org/apache/lucene/search/TestSort.java | 11 ++--- .../apache/lucene/search/TestStressSort.java | 4 +- .../apache/lucene/search/TestThreadSafe.java | 7 +-- .../lucene/store/TestBufferedIndexInput.java | 7 +-- .../apache/lucene/store/TestWindowsMMap.java | 3 +- .../apache/lucene/util/LuceneTestCase.java | 45 +++++++++++++++++++ .../apache/lucene/util/TestOpenBitSet.java | 10 +++-- .../apache/lucene/util/TestPriorityQueue.java | 5 +-- .../apache/lucene/util/TestSmallFloat.java | 3 +- 31 files changed, 190 insertions(+), 94 deletions(-) diff --git a/contrib/db/bdb-je/build.xml b/contrib/db/bdb-je/build.xml index 19ec02ec1cb..782a297a9ea 100644 --- a/contrib/db/bdb-je/build.xml +++ b/contrib/db/bdb-je/build.xml @@ -23,6 +23,13 @@ + + + + + + + + + + + + + + + diff --git a/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java b/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java index e4da7319888..d132bca733f 100644 --- a/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java +++ b/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java @@ -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 diff --git a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java index 4653cd2f77e..0185f17d3a1 100644 --- a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java +++ b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java @@ -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 diff --git a/src/test/org/apache/lucene/index/TestAtomicUpdate.java b/src/test/org/apache/lucene/index/TestAtomicUpdate.java index 31617e80cdc..f03a2c21a87 100644 --- a/src/test/org/apache/lucene/index/TestAtomicUpdate.java +++ b/src/test/org/apache/lucene/index/TestAtomicUpdate.java @@ -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: diff --git a/src/test/org/apache/lucene/index/TestByteSlices.java b/src/test/org/apache/lucene/index/TestByteSlices.java index 14a84485941..903e87e0eb1 100644 --- a/src/test/org/apache/lucene/index/TestByteSlices.java +++ b/src/test/org/apache/lucene/index/TestByteSlices.java @@ -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(); diff --git a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java b/src/test/org/apache/lucene/index/TestIndexReaderReopen.java index 63a854f970b..3781e89ce4d 100644 --- a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java +++ b/src/test/org/apache/lucene/index/TestIndexReaderReopen.java @@ -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()); diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java index 6ac7919a84b..20421e84185 100644 --- a/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -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]; diff --git a/src/test/org/apache/lucene/index/TestLazyBug.java b/src/test/org/apache/lucene/index/TestLazyBug.java index 3f68e7ec80a..c3cbd0ab263 100755 --- a/src/test/org/apache/lucene/index/TestLazyBug.java +++ b/src/test/org/apache/lucene/index/TestLazyBug.java @@ -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(); ) { diff --git a/src/test/org/apache/lucene/index/TestPayloads.java b/src/test/org/apache/lucene/index/TestPayloads.java index f915e6d8aa2..21e73ba8990 100644 --- a/src/test/org/apache/lucene/index/TestPayloads.java +++ b/src/test/org/apache/lucene/index/TestPayloads.java @@ -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; diff --git a/src/test/org/apache/lucene/index/TestStressIndexing.java b/src/test/org/apache/lucene/index/TestStressIndexing.java index 17b6968cf11..a7f730f72bd 100644 --- a/src/test/org/apache/lucene/index/TestStressIndexing.java +++ b/src/test/org/apache/lucene/index/TestStressIndexing.java @@ -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(); diff --git a/src/test/org/apache/lucene/index/TestStressIndexing2.java b/src/test/org/apache/lucene/index/TestStressIndexing2.java index c7932db9a55..0ae023b8c8f 100644 --- a/src/test/org/apache/lucene/index/TestStressIndexing2.java +++ b/src/test/org/apache/lucene/index/TestStressIndexing2.java @@ -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(); diff --git a/src/test/org/apache/lucene/index/TestTermdocPerf.java b/src/test/org/apache/lucene/index/TestTermdocPerf.java index 04b4d20b17a..806e8f1ffb4 100644 --- a/src/test/org/apache/lucene/index/TestTermdocPerf.java +++ b/src/test/org/apache/lucene/index/TestTermdocPerf.java @@ -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() { diff --git a/src/test/org/apache/lucene/index/TestTransactions.java b/src/test/org/apache/lucene/index/TestTransactions.java index 3af46d63040..f623d76e934 100644 --- a/src/test/org/apache/lucene/index/TestTransactions.java +++ b/src/test/org/apache/lucene/index/TestTransactions.java @@ -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); diff --git a/src/test/org/apache/lucene/search/BaseTestRangeFilter.java b/src/test/org/apache/lucene/search/BaseTestRangeFilter.java index 35956e80dcb..f6db5af8ce4 100644 --- a/src/test/org/apache/lucene/search/BaseTestRangeFilter.java +++ b/src/test/org/apache/lucene/search/BaseTestRangeFilter.java @@ -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); } diff --git a/src/test/org/apache/lucene/search/TestBoolean2.java b/src/test/org/apache/lucene/search/TestBoolean2.java index d7ec5cf7705..bd8282b9407 100644 --- a/src/test/org/apache/lucene/search/TestBoolean2.java +++ b/src/test/org/apache/lucene/search/TestBoolean2.java @@ -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. diff --git a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java index 8d4b73ce633..87494d64c48 100644 --- a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java +++ b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java @@ -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); diff --git a/src/test/org/apache/lucene/search/TestCustomSearcherSort.java b/src/test/org/apache/lucene/search/TestCustomSearcherSort.java index 142e6ab025d..02349923f64 100644 --- a/src/test/org/apache/lucene/search/TestCustomSearcherSort.java +++ b/src/test/org/apache/lucene/search/TestCustomSearcherSort.java @@ -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