From a7418eb2068380e787b90df6f64cce4ece2382d7 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Tue, 10 May 2011 16:45:57 +0000 Subject: [PATCH] MockDirectoryWrapper should randomly swap in ThrottledIndexOutput git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1101539 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/store/MockDirectoryWrapper.java | 28 +++++++++++++++++-- .../org/apache/lucene/index/Test2BTerms.java | 3 +- .../index/TestFlushByRamOrCountsPolicy.java | 3 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java index 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 openFilesForWrite = new HashSet(); 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 seed this.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; + } } @Override diff --git a/lucene/src/test/org/apache/lucene/index/Test2BTerms.java b/lucene/src/test/org/apache/lucene/index/Test2BTerms.java index 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 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.java index 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);