Apache Lucene open-source search software
MockDirectoryWrapper should randomly swap in ThrottledIndexOutput
Robert Muira7418eb20683
diff --git a/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.javaindex 17b62a6f94d..c7b0d036dd1 100644--- a/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java+++ b/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java@@ -71,6 +71,7 @@ public class MockDirectoryWrapper extends Directory {Set<String> openFilesForWrite = new HashSet<String>();volatile boolean crashed;private ThrottledIndexOutput throttledOutput;+ private Throttling throttling = Throttling.SOMETIMES;// use this for tracking files for crash.// additionally: provides debugging information in case you leave one open@@ -104,6 +105,8 @@ public class MockDirectoryWrapper extends Directory {// called from different threads; else test failures may// not be reproducible from the original seedthis.randomState = new Random(random.nextInt());+ this.throttledOutput = new ThrottledIndexOutput(ThrottledIndexOutput+ .mBitsToBytes(40 + randomState.nextInt(10)), 5 + randomState.nextInt(5), null);init();}@@ -117,8 +120,17 @@ public class MockDirectoryWrapper extends Directory {preventDoubleWrite = value;}- public void setThrottledIndexOutput(ThrottledIndexOutput throttledOutput) {- this.throttledOutput = throttledOutput;+ public static enum Throttling {+ /** always emulate a slow hard disk. could be very slow! */+ ALWAYS,+ /** sometimes (2% of the time) emulate a slow hard disk. */+ SOMETIMES,+ /** never throttle output */+ NEVER+ };++ public void setThrottling(Throttling throttling) {+ this.throttling = throttling;}@Override@@ -354,7 +366,17 @@ public class MockDirectoryWrapper extends Directory {IndexOutput io = new MockIndexOutputWrapper(this, delegate.createOutput(name), name);openFileHandles.put(io, new RuntimeException("unclosed IndexOutput"));openFilesForWrite.add(name);- return throttledOutput == null ? io : throttledOutput.newFromDelegate(io);++ // 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)) {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockDirectoryWrapper: throttling indexOutput");+ }+ return throttledOutput.newFromDelegate(io);+ } else {+ return io;+ }}@Overridediff --git a/lucene/src/test/org/apache/lucene/index/Test2BTerms.java b/lucene/src/test/org/apache/lucene/index/Test2BTerms.javaindex 25cf0c4d987..6fffc48664a 100644--- a/lucene/src/test/org/apache/lucene/index/Test2BTerms.java+++ b/lucene/src/test/org/apache/lucene/index/Test2BTerms.java@@ -153,7 +153,8 @@ public class Test2BTerms extends LuceneTestCase {List<BytesRef> savedTerms = null;- Directory dir = newFSDirectory(_TestUtil.getTempDir("2BTerms"));+ MockDirectoryWrapper dir = newFSDirectory(_TestUtil.getTempDir("2BTerms"));+ dir.setThrottling(MockDirectoryWrapper.Throttling.NEVER);//Directory dir = newFSDirectory(new File("/p/lucene/indices/2bindex"));if (true) {diff --git a/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java b/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.javaindex 7eb72130d3f..c6d69079fcf 100644--- a/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java+++ b/lucene/src/test/org/apache/lucene/index/TestFlushByRamOrCountsPolicy.java@@ -233,8 +233,7 @@ public class TestFlushByRamOrCountsPolicy extends LuceneTestCase {AtomicInteger numDocs = new AtomicInteger(numDocumentsToIndex);MockDirectoryWrapper dir = newDirectory();// mock a very slow harddisk here so that flushing is very slow- dir.setThrottledIndexOutput(new ThrottledIndexOutput(ThrottledIndexOutput- .mBitsToBytes(40 + random.nextInt(10)), 5 + random.nextInt(5), null));+ dir.setThrottling(MockDirectoryWrapper.Throttling.ALWAYS);IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT,new MockAnalyzer(random));iwc.setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH);