From 5468b98e94c225c2707a0fef760404bc782671e2 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 24 Aug 2010 13:56:41 +0000 Subject: [PATCH] LUCENE-2598: more fixing tests to run with -Dtests.directory=SimpleFSDirectory git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@988543 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/store/FSDirectory.java | 6 +- .../apache/lucene/index/TestIndexWriter.java | 58 +++++++----------- .../lucene/store/MockDirectoryWrapper.java | 5 ++ .../lucene/store/MockIndexOutputWrapper.java | 60 +++---------------- .../org/apache/lucene/util/_TestUtil.java | 9 +++ 5 files changed, 45 insertions(+), 93 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/src/java/org/apache/lucene/store/FSDirectory.java index ca1a44fad8e..d04b77d9a61 100644 --- a/lucene/src/java/org/apache/lucene/store/FSDirectory.java +++ b/lucene/src/java/org/apache/lucene/store/FSDirectory.java @@ -274,10 +274,12 @@ public abstract class FSDirectory extends Directory { public long fileLength(String name) throws IOException { ensureOpen(); File file = new File(directory, name); - if (!file.exists()) { + final long len = file.length(); + if (len == 0 && !file.exists()) { throw new FileNotFoundException(name); + } else { + return len; } - return file.length(); } /** Removes an existing file in the directory. */ diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java index c6db561f78f..f2fa5636c43 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -314,6 +314,7 @@ public class TestIndexWriter extends LuceneTestCase { if (VERBOSE) System.out.println("\ncycle: " + testName); + dir.setTrackDiskUsage(true); dir.setMaxSizeInBytes(thisDiskFree); dir.setRandomIOExceptionRate(rate, diskFree); @@ -519,7 +520,7 @@ public class TestIndexWriter extends LuceneTestCase { //_TestUtil.syncConcurrentMerges(ms); - if (dir.listAll().length > 0) { + if (_TestUtil.anyFilesExceptWriteLock(dir)) { assertNoUnreferencedFiles(dir, "after disk full during addDocument"); // Make sure reader can open the index: @@ -661,6 +662,8 @@ public class TestIndexWriter extends LuceneTestCase { } dir.resetMaxUsedSizeInBytes(); + dir.setTrackDiskUsage(true); + writer = new IndexWriter(dir, newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer()).setOpenMode(OpenMode.APPEND)); writer.optimize(); writer.close(); @@ -1027,6 +1030,7 @@ public class TestIndexWriter extends LuceneTestCase { writer.close(); dir.resetMaxUsedSizeInBytes(); + dir.setTrackDiskUsage(true); long startDiskUsage = dir.getMaxUsedSizeInBytes(); writer = new IndexWriter(dir, newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer()) .setOpenMode(OpenMode.APPEND).setMaxBufferedDocs(10).setMergeScheduler( @@ -4377,7 +4381,6 @@ public class TestIndexWriter extends LuceneTestCase { dir = newDirectory(random); } catch (IOException e) { throw new RuntimeException(e); } IndexWriter w = null; - boolean first = true; while(!finish) { try { @@ -4386,40 +4389,32 @@ public class TestIndexWriter extends LuceneTestCase { w.close(); } IndexWriterConfig conf = newIndexWriterConfig(random, - TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2); + TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(2); ((LogMergePolicy) conf.getMergePolicy()).setMergeFactor(2); w = new IndexWriter(dir, conf); - //((ConcurrentMergeScheduler) w.getMergeScheduler()).setSuppressExceptions(); - if (!first && !allowInterrupt) { - // tell main thread it can interrupt us any time, - // starting now - allowInterrupt = true; - } - Document doc = new Document(); doc.add(new Field("field", "some text contents", Field.Store.YES, Field.Index.ANALYZED)); for(int i=0;i<100;i++) { w.addDocument(doc); - w.commit(); + if (i%10 == 0) { + w.commit(); + } } w.close(); _TestUtil.checkIndex(dir); IndexReader.open(dir, true).close(); - if (first && !allowInterrupt) { - // Strangely, if we interrupt a thread before - // all classes are loaded, the class loader - // seems to do scary things with the interrupt - // status. In java 1.5, it'll throw an - // incorrect ClassNotFoundException. In java - // 1.6, it'll silently clear the interrupt. - // So, on first iteration through here we - // don't open ourselves up for interrupts - // until we've done the above loop. - allowInterrupt = true; - first = false; - } + // Strangely, if we interrupt a thread before + // all classes are loaded, the class loader + // seems to do scary things with the interrupt + // status. In java 1.5, it'll throw an + // incorrect ClassNotFoundException. In java + // 1.6, it'll silently clear the interrupt. + // So, on first iteration through here we + // don't open ourselves up for interrupts + // until we've done the above loop. + allowInterrupt = true; } } catch (ThreadInterruptedException re) { Throwable e = re.getCause(); @@ -4427,16 +4422,6 @@ public class TestIndexWriter extends LuceneTestCase { if (finish) { break; } - - // Make sure IW cleared the interrupted bit - // TODO: remove that false once test is fixed for real - if (false && interrupted()) { - System.out.println("FAILED; InterruptedException hit but thread.interrupted() was true"); - e.printStackTrace(System.out); - failed = true; - break; - } - } catch (Throwable t) { System.out.println("FAILED; unexpected exception"); t.printStackTrace(System.out); @@ -4487,18 +4472,15 @@ public class TestIndexWriter extends LuceneTestCase { // issue 100 interrupts to child thread int i = 0; while(i < 100) { - Thread.sleep(1); - + Thread.sleep(10); if (t.allowInterrupt) { i++; - t.allowInterrupt = false; t.interrupt(); } if (!t.isAlive()) { break; } } - t.allowInterrupt = false; t.finish = true; t.join(); assertFalse(t.failed); diff --git a/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java index 2b5fe0fcbb5..3da793f7de2 100644 --- a/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java +++ b/lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java @@ -44,6 +44,7 @@ public class MockDirectoryWrapper extends Directory { Random randomState; boolean noDeleteOpenFile = true; boolean preventDoubleWrite = true; + boolean trackDiskUsage = false; private Set unSyncedFiles; private Set createdFiles; volatile boolean crashed; @@ -68,6 +69,10 @@ public class MockDirectoryWrapper extends Directory { init(); } + public void setTrackDiskUsage(boolean v) { + trackDiskUsage = v; + } + /** If set to true, we throw an IOException if the same * file is opened by createOutput, ever. */ public void setPreventDoubleWrite(boolean value) { diff --git a/lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java b/lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java index ef99801d574..a6e0907aebd 100644 --- a/lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java +++ b/lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java @@ -18,7 +18,6 @@ package org.apache.lucene.store; */ import java.io.IOException; -import java.util.Map; /** * Used by MockRAMDirectory to create an output stream that @@ -46,11 +45,13 @@ public class MockIndexOutputWrapper extends IndexOutput { public void close() throws IOException { dir.maybeThrowDeterministicException(); delegate.close(); - // Now compute actual disk usage & track the maxUsedSize - // in the MockDirectoryWrapper: - long size = dir.getRecomputedActualSizeInBytes(); - if (size > dir.maxUsedSize) { - dir.maxUsedSize = size; + if (dir.trackDiskUsage) { + // Now compute actual disk usage & track the maxUsedSize + // in the MockDirectoryWrapper: + long size = dir.getRecomputedActualSizeInBytes(); + if (size > dir.maxUsedSize) { + dir.maxUsedSize = size; + } } } @@ -127,53 +128,6 @@ public class MockIndexOutputWrapper extends IndexOutput { delegate.setLength(length); } - /* - @Override - public void writeBytes(byte[] b, int length) throws IOException { - delegate.writeBytes(b, length); - } - - @Override - public void writeInt(int i) throws IOException { - delegate.writeInt(i); - } - - @Override - public void writeVInt(int i) throws IOException { - delegate.writeVInt(i); - } - - @Override - public void writeLong(long i) throws IOException { - delegate.writeLong(i); - } - - @Override - public void writeVLong(long i) throws IOException { - delegate.writeVLong(i); - } - - @Override - public void writeString(String s) throws IOException { - delegate.writeString(s); - } - - @Override - public void writeChars(String s, int start, int length) throws IOException { - delegate.writeChars(s, start, length); - } - - @Override - public void writeChars(char[] s, int start, int length) throws IOException { - delegate.writeChars(s, start, length); - } - - @Override - public void writeStringStringMap(Map map) throws IOException { - delegate.writeStringStringMap(map); - } - */ - @Override public void copyBytes(DataInput input, long numBytes) throws IOException { delegate.copyBytes(input, numBytes); diff --git a/lucene/src/test/org/apache/lucene/util/_TestUtil.java b/lucene/src/test/org/apache/lucene/util/_TestUtil.java index 982808e7fa4..a77dabfc430 100644 --- a/lucene/src/test/org/apache/lucene/util/_TestUtil.java +++ b/lucene/src/test/org/apache/lucene/util/_TestUtil.java @@ -210,4 +210,13 @@ public class _TestUtil { public static CodecProvider alwaysCodec(final String codec) { return alwaysCodec(CodecProvider.getDefault().lookup(codec)); } + + public static boolean anyFilesExceptWriteLock(Directory dir) throws IOException { + String[] files = dir.listAll(); + if (files.length > 1 || (files.length == 1 && !files[0].equals("write.lock"))) { + return true; + } else { + return false; + } + } }