LUCENE-5635: speed up and improve test

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1591726 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-05-01 18:35:55 +00:00
parent 07eac25dd0
commit 178b6b3f5e
4 changed files with 30 additions and 10 deletions

View File

@ -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 debuggable
public 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 reproducible
conf.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 {

View File

@ -66,6 +66,9 @@ class CrankyStoredFieldsFormat extends StoredFieldsFormat {
@Override
public void abort() {
delegate.abort();
if (random.nextInt(100) == 0) {
throw new RuntimeException(new IOException("Fake IOException from StoredFieldsWriter.abort()"));
}
}
@Override

View File

@ -66,6 +66,9 @@ class CrankyTermVectorsFormat extends TermVectorsFormat {
@Override
public void abort() {
delegate.abort();
if (random.nextInt(100) == 0) {
throw new RuntimeException(new IOException("Fake IOException from TermVectorsWriter.abort()"));
}
}
@Override

View File

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