Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

add warnings to tests that arent really randomized

Robert Muira540f48f0707
diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
index c8a016ff481..f73e69eb539 100644
--- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
@@ -352,6 +352,7 @@ public abstract class LuceneTestCase extends Assert {
state = State.INITIAL;
staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1;
random.setSeed(staticSeed);
+ random.initialized = true;
tempDirs.clear();
stores = Collections.synchronizedMap(new IdentityHashMap<MockDirectoryWrapper,StackTraceElement[]>());
@@ -494,6 +495,8 @@ public abstract class LuceneTestCase extends Assert {
}
}
}
+ random.setSeed(0L);
+ random.initialized = false;
}
private static boolean testsFailed; /* true if any tests failed */
@@ -1366,7 +1369,26 @@ public abstract class LuceneTestCase extends Assert {
private long seed;
private static final Random seedRand = new Random();
- protected static final Random random = new Random(0);
+ protected static final SmartRandom random = new SmartRandom(0);
+
+ public static class SmartRandom extends Random {
+ boolean initialized;
+
+ SmartRandom(long seed) {
+ super(seed);
+ }
+
+ @Override
+ protected int next(int bits) {
+ if (!initialized) {
+ System.err.println("!!! WARNING: test is using random from static initializer !!!");
+ Thread.dumpStack();
+ // I wish, but it causes JRE crashes
+ // throw new IllegalStateException("you cannot use this random from a static initializer in your test");
+ }
+ return super.next(bits);
+ }
+ }
private String name = "<unknown>";
diff --git a/lucene/src/test/org/apache/lucene/index/TestCodecs.java b/lucene/src/test/org/apache/lucene/index/TestCodecs.java
index d0b3395abd1..ca4d89b57b3 100644
--- a/lucene/src/test/org/apache/lucene/index/TestCodecs.java
+++ b/lucene/src/test/org/apache/lucene/index/TestCodecs.java
@@ -46,6 +46,7 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.Version;
import org.apache.lucene.util._TestUtil;
+import org.junit.BeforeClass;
// TODO: test multiple codecs here?
@@ -67,13 +68,18 @@ import org.apache.lucene.util._TestUtil;
public class TestCodecs extends LuceneTestCase {
private static String[] fieldNames = new String[] {"one", "two", "three", "four"};
- private final static int NUM_TEST_ITER = atLeast(20);
+ private static int NUM_TEST_ITER;
private final static int NUM_TEST_THREADS = 3;
private final static int NUM_FIELDS = 4;
private final static int NUM_TERMS_RAND = 50; // must be > 16 to test skipping
private final static int DOC_FREQ_RAND = 500; // must be > 16 to test skipping
private final static int TERM_DOC_FREQ_RAND = 20;
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ NUM_TEST_ITER = atLeast(20);
+ }
+
class FieldData implements Comparable {
final FieldInfo fieldInfo;
final TermData[] terms;