You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2012/08/15 06:18:28 UTC

svn commit: r1373205 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/core/ lucene/core/src/test/org/apache/lucene/store/ lucene/core/src/test/org/apache/lucene/util/junitcompat/ lucene/test-framework/ lucene/test-framework/src/java/org/apache/lu...

Author: rmuir
Date: Wed Aug 15 04:18:28 2012
New Revision: 1373205

URL: http://svn.apache.org/viewvc?rev=1373205&view=rev
Log:
better failures from MDW if you have unclosed indexwriter, even if you setLockFactory

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/core/   (props changed)
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestFailIfDirectoryNotClosed.java
    lucene/dev/branches/branch_4x/lucene/test-framework/   (props changed)
    lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java?rev=1373205&r1=1373204&r2=1373205&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java Wed Aug 15 04:18:28 2012
@@ -77,9 +77,9 @@ public class TestLockFactory extends Luc
     // exceptions raised:
     // Verify: NoLockFactory allows two IndexWriters
     public void testRAMDirectoryNoLocking() throws IOException {
-        Directory dir = new MockDirectoryWrapper(random(), new RAMDirectory());
+        MockDirectoryWrapper dir = new MockDirectoryWrapper(random(), new RAMDirectory());
         dir.setLockFactory(NoLockFactory.getNoLockFactory());
-
+        dir.setWrapLockFactory(false); // we are gonna explicitly test we get this back
         assertTrue("RAMDirectory.setLockFactory did not take",
                    NoLockFactory.class.isInstance(dir.getLockFactory()));
 

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestFailIfDirectoryNotClosed.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestFailIfDirectoryNotClosed.java?rev=1373205&r1=1373204&r2=1373205&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestFailIfDirectoryNotClosed.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestFailIfDirectoryNotClosed.java Wed Aug 15 04:18:28 2012
@@ -17,7 +17,13 @@ package org.apache.lucene.util.junitcomp
  * limitations under the License.
  */
 
+import java.io.IOException;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.MockDirectoryWrapper;
+import org.apache.lucene.store.SingleInstanceLockFactory;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
@@ -34,10 +40,39 @@ public class TestFailIfDirectoryNotClose
       System.out.println(dir.toString());
     }
   }
+  
+  public static class Nested2 extends WithNestedTests.AbstractNestedTest {
+    public void testDummy() throws IOException {
+      MockDirectoryWrapper dir = newMockDirectory();
+      IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
+      dir.close();
+    }
+  }
+  
+  public static class Nested3 extends WithNestedTests.AbstractNestedTest {
+    public void testDummy() throws IOException {
+      MockDirectoryWrapper dir = newMockDirectory();
+      dir.setLockFactory(new SingleInstanceLockFactory());
+      IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
+      dir.close();
+    }
+  }
 
   @Test
   public void testFailIfDirectoryNotClosed() {
     Result r = JUnitCore.runClasses(Nested1.class);
     Assert.assertEquals(1, r.getFailureCount());
   }
+  
+  @Test
+  public void testFailIfIndexWriterNotClosed() {
+    Result r = JUnitCore.runClasses(Nested2.class);
+    Assert.assertEquals(1, r.getFailureCount());
+  }
+  
+  @Test
+  public void testFailIfIndexWriterNotClosedChangeLockFactory() {
+    Result r = JUnitCore.runClasses(Nested3.class);
+    Assert.assertEquals(1, r.getFailureCount());
+  }
 }

Modified: lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java?rev=1373205&r1=1373204&r2=1373205&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java (original)
+++ lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java Wed Aug 15 04:18:28 2012
@@ -67,6 +67,7 @@ public class MockDirectoryWrapper extend
   boolean noDeleteOpenFile = true;
   boolean preventDoubleWrite = true;
   boolean trackDiskUsage = false;
+  boolean wrapLockFactory = true;
   private Set<String> unSyncedFiles;
   private Set<String> createdFiles;
   private Set<String> openFilesForWrite = new HashSet<String>();
@@ -114,11 +115,7 @@ public class MockDirectoryWrapper extend
     this.throttledOutput = new ThrottledIndexOutput(ThrottledIndexOutput
         .mBitsToBytes(40 + randomState.nextInt(10)), 5 + randomState.nextInt(5), null);
     // force wrapping of lockfactory
-    try {
-      setLockFactory(new MockLockFactoryWrapper(this, delegate.getLockFactory()));
-    } catch (IOException e) {
-      throw new RuntimeException(e);
-    }
+    this.lockFactory = new MockLockFactoryWrapper(this, delegate.getLockFactory());
 
     // 2% of the time use rate limiter
     if (randomState.nextInt(50) == 17) {
@@ -530,6 +527,19 @@ public class MockDirectoryWrapper extend
   public void setAssertNoUnrefencedFilesOnClose(boolean v) {
     assertNoUnreferencedFilesOnClose = v;
   }
+  
+  /**
+   * Set to false if you want to return the pure lockfactory
+   * and not wrap it with MockLockFactoryWrapper.
+   * <p>
+   * Be careful if you turn this off: MockDirectoryWrapper might
+   * no longer be able to detect if you forget to close an IndexWriter,
+   * and spit out horribly scary confusing exceptions instead of
+   * simply telling you that.
+   */
+  public void setWrapLockFactory(boolean v) {
+    this.wrapLockFactory = v;
+  }
 
   @Override
   public synchronized void close() throws IOException {
@@ -699,25 +709,33 @@ public class MockDirectoryWrapper extend
   @Override
   public synchronized Lock makeLock(String name) {
     maybeYield();
-    return delegate.makeLock(name);
+    return getLockFactory().makeLock(name);
   }
 
   @Override
   public synchronized void clearLock(String name) throws IOException {
     maybeYield();
-    delegate.clearLock(name);
+    getLockFactory().clearLock(name);
   }
 
   @Override
   public synchronized void setLockFactory(LockFactory lockFactory) throws IOException {
     maybeYield();
+    // sneaky: we must pass the original this way to the dir, because
+    // some impls (e.g. FSDir) do instanceof here.
     delegate.setLockFactory(lockFactory);
+    // now set our wrapped factory here
+    this.lockFactory = new MockLockFactoryWrapper(this, lockFactory);
   }
 
   @Override
   public synchronized LockFactory getLockFactory() {
     maybeYield();
-    return delegate.getLockFactory();
+    if (wrapLockFactory) {
+      return lockFactory;
+    } else {
+      return delegate.getLockFactory();
+    }
   }
 
   @Override