Apache Solr open-source search software
LUCENE-5635: speed up and improve test
Robert Muir178b6b3f5eb9
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.javaindex b0ba3673356..f3320691b48 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions2.java@@ -26,11 +26,13 @@ import org.apache.lucene.analysis.CrankyTokenFilter;import org.apache.lucene.analysis.MockTokenizer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.codecs.Codec;+import org.apache.lucene.codecs.asserting.AssertingCodec;import org.apache.lucene.codecs.cranky.CrankyCodec;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.NumericDocValuesField;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.MockDirectoryWrapper;import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -40,15 +42,16 @@ import org.apache.lucene.util.Rethrow;* Causes a bunch of non-aborting and aborting exceptions and checks that* no index corruption is ever created*/-// TODO: not sure which fails are test bugs or real bugs yet...-// reproduce with: ant test -Dtestcase=TestIndexWriterExceptions2 -Dtests.method=testSimple -Dtests.seed=9D05AC6DFF3CC9A4 -Dtests.multiplier=10 -Dtests.locale=fi_FI -Dtests.timezone=Canada/Pacific -Dtests.file.encoding=ISO-8859-1-// also sometimes when it fails, the exception-stream printing doesnt seem to be working yet-//public class TestIndexWriterExceptions2 extends LuceneTestCase {// just one thread, serial merge policy, hopefully debuggablepublic void testBasics() throws Exception {+ // disable slow things: we don't rely upon sleeps here.Directory dir = newDirectory();+ if (dir instanceof MockDirectoryWrapper) {+ ((MockDirectoryWrapper)dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);+ ((MockDirectoryWrapper)dir).setUseSlowOpenClosers(false);+ }// log all exceptions we hit, in case we fail (for debugging)ByteArrayOutputStream exceptionLog = new ByteArrayOutputStream();@@ -68,15 +71,16 @@ public class TestIndexWriterExceptions2 extends LuceneTestCase {};// create lots of aborting exceptions with a broken codec- Codec codec = new CrankyCodec(Codec.getDefault(), new Random(random().nextLong()));+ // we don't need a random codec, as we aren't trying to find bugs in the codec here.+ Codec inner = RANDOM_MULTIPLIER > 1 ? Codec.getDefault() : new AssertingCodec();+ Codec codec = new CrankyCodec(inner, new Random(random().nextLong()));IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);// just for now, try to keep this test reproducibleconf.setMergeScheduler(new SerialMergeScheduler());conf.setCodec(codec);- // TODO: too much?- int numDocs = RANDOM_MULTIPLIER * 1000;+ int numDocs = atLeast(2500);IndexWriter iw = new IndexWriter(dir, conf);try {diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.javaindex e330d30f018..b7e86cae80a 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyStoredFieldsFormat.java@@ -66,6 +66,9 @@ class CrankyStoredFieldsFormat extends StoredFieldsFormat {@Overridepublic void abort() {delegate.abort();+ if (random.nextInt(100) == 0) {+ throw new RuntimeException(new IOException("Fake IOException from StoredFieldsWriter.abort()"));+ }}@Overridediff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.javaindex e0a792d1965..fff475e12a9 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/cranky/CrankyTermVectorsFormat.java@@ -66,6 +66,9 @@ class CrankyTermVectorsFormat extends TermVectorsFormat {@Overridepublic void abort() {delegate.abort();+ if (random.nextInt(100) == 0) {+ throw new RuntimeException(new IOException("Fake IOException from TermVectorsWriter.abort()"));+ }}@Overridediff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.javaindex 048179eb5e2..9323631ba86 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java@@ -74,6 +74,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {boolean preventDoubleWrite = true;boolean trackDiskUsage = false;boolean wrapLockFactory = true;+ boolean useSlowOpenClosers = true;boolean allowRandomFileNotFoundException = true;boolean allowReadingFilesStillOpenForWrite = false;private Set<String> unSyncedFiles;@@ -172,6 +173,15 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {public void setThrottling(Throttling throttling) {this.throttling = throttling;}++ /**+ * By default, opening and closing has a rare small sleep to catch race conditions+ * <p>+ * You can disable this if you dont need it+ */+ public void setUseSlowOpenClosers(boolean v) {+ useSlowOpenClosers = v;+ }/*** Returns true if {@link #in} must sync its files.@@ -524,7 +534,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {// throttling REALLY slows down tests, so don't do it very often for SOMETIMES.if (throttling == Throttling.ALWAYS ||- (throttling == Throttling.SOMETIMES && randomState.nextInt(50) == 0) && !(in instanceof RateLimitedDirectoryWrapper)) {+ (throttling == Throttling.SOMETIMES && randomState.nextInt(200) == 0) && !(in instanceof RateLimitedDirectoryWrapper)) {if (LuceneTestCase.VERBOSE) {System.out.println("MockDirectoryWrapper: throttling indexOutput (" + name + ")");}@@ -578,12 +588,12 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {final IndexInput ii;int randomInt = randomState.nextInt(500);- if (randomInt == 0) {+ if (useSlowOpenClosers && randomInt == 0) {if (LuceneTestCase.VERBOSE) {System.out.println("MockDirectoryWrapper: using SlowClosingMockIndexInputWrapper for file " + name);}ii = new SlowClosingMockIndexInputWrapper(this, name, delegateInput);- } else if (randomInt == 1) {+ } else if (useSlowOpenClosers && randomInt == 1) {if (LuceneTestCase.VERBOSE) {System.out.println("MockDirectoryWrapper: using SlowOpeningMockIndexInputWrapper for file " + name);}