You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2014/04/04 20:10:55 UTC

svn commit: r1584843 - in /lucene/dev/branches/solr5914/lucene: ./ core/src/test/org/apache/lucene/util/junitcompat/ test-framework/src/java/org/apache/lucene/util/

Author: dweiss
Date: Fri Apr  4 18:10:55 2014
New Revision: 1584843

URL: http://svn.apache.org/r1584843
Log:
Modified the rule to attempt to remove temporary files, but report only (not throw an exception). Added tests.

Modified:
    lucene/dev/branches/solr5914/lucene/CHANGES.txt
    lucene/dev/branches/solr5914/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
    lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
    lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java

Modified: lucene/dev/branches/solr5914/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/solr5914/lucene/CHANGES.txt?rev=1584843&r1=1584842&r2=1584843&view=diff
==============================================================================
--- lucene/dev/branches/solr5914/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/solr5914/lucene/CHANGES.txt Fri Apr  4 18:10:55 2014
@@ -233,6 +233,9 @@ Bug fixes
 
 Test Framework
 
+* LUCENE-5577: Temporary folder and file management (and cleanup facilities)
+  (Mark Miller, Uwe Schindler, Dawid Weiss)
+
 * LUCENE-5567: When a suite fails with zombie threads failure marker and count 
   is not propagated properly. (Dawid Weiss)
 

Modified: lucene/dev/branches/solr5914/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/solr5914/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java?rev=1584843&r1=1584842&r2=1584843&view=diff
==============================================================================
--- lucene/dev/branches/solr5914/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java (original)
+++ lucene/dev/branches/solr5914/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java Fri Apr  4 18:10:55 2014
@@ -18,13 +18,19 @@ package org.apache.lucene.util.junitcomp
  */
 
 import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
 
+import org.apache.lucene.util.Constants;
+import org.apache.lucene.util.LuceneTestCase;
 import org.apache.lucene.util.TestUtil;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
 
+import com.carrotsearch.randomizedtesting.RandomizedTest;
+
 public class TestLeaveFilesIfTestFails extends WithNestedTests {
   public TestLeaveFilesIfTestFails() {
     super(true);
@@ -34,7 +40,6 @@ public class TestLeaveFilesIfTestFails e
     static File file;
     public void testDummy() {
       file = createTempDir("leftover");
-      file.mkdirs();
       fail();
     }
   }
@@ -43,7 +48,33 @@ public class TestLeaveFilesIfTestFails e
   public void testLeaveFilesIfTestFails() {
     Result r = JUnitCore.runClasses(Nested1.class);
     Assert.assertEquals(1, r.getFailureCount());
-    Assert.assertTrue(Nested1.file.exists());
+    Assert.assertTrue(Nested1.file != null && Nested1.file.exists());
     Nested1.file.delete();
   }
+  
+  public static class Nested2 extends WithNestedTests.AbstractNestedTest {
+    static File file;
+    static File parent;
+    static RandomAccessFile openFile;
+
+    @SuppressWarnings("deprecation")
+    public void testDummy() throws Exception {
+      file = new File(createTempDir("leftover"), "child.locked");
+      openFile = new RandomAccessFile(file, "rw");
+
+      parent = LuceneTestCase.getTempDirBase();
+    }
+  }
+
+  @Test
+  public void testWindowsUnremovableFile() throws IOException {
+    RandomizedTest.assumeTrue("Requires Windows.", Constants.WINDOWS);
+
+    Result r = JUnitCore.runClasses(Nested2.class);
+    super.prevSysErr.println(r.getFailures().get(0).getMessage());
+    Assert.assertEquals(1, r.getFailureCount());
+
+    Nested2.openFile.close();
+    TestUtil.rm(Nested2.parent);
+  }  
 }

Modified: lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java?rev=1584843&r1=1584842&r2=1584843&view=diff
==============================================================================
--- lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java Fri Apr  4 18:10:55 2014
@@ -2196,8 +2196,11 @@ public abstract class LuceneTestCase ext
   private static final int TEMP_NAME_RETRY_THRESHOLD = 9999;
 
   /**
+   * This method is deprecated for a reason. Do not use it. Call {@link #createTempDir()}
+   * or {@link #createTempDir(String)} or {@link #createTempFile(String, String)}.
    */
-  private static File getTempDirBase() {
+  @Deprecated
+  public static File getTempDirBase() {
     synchronized (LuceneTestCase.class) {
       if (tempDirBase == null) {
         File directory = new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir")));
@@ -2208,8 +2211,8 @@ public abstract class LuceneTestCase ext
         RandomizedContext ctx = RandomizedContext.current();
         Class<?> clazz = ctx.getTargetClass();
         String prefix = clazz.getName();
-        prefix = prefix.replaceFirst("^org.apache.lucene.", "oa.lucene.");
-        prefix = prefix.replaceFirst("^org.apache.solr.", "oa.solr.");
+        prefix = prefix.replaceFirst("^org.apache.lucene.", "lucene.");
+        prefix = prefix.replaceFirst("^org.apache.solr.", "solr.");
 
         int attempt = 0;
         File f;
@@ -2220,7 +2223,7 @@ public abstract class LuceneTestCase ext
                   + directory.getAbsolutePath());            
           }
           f = new File(directory, prefix + "-" + ctx.getRunnerSeedAsString() 
-                + "-" + String.format(Locale.ENGLISH, "%3d", attempt));
+                + "-" + String.format(Locale.ENGLISH, "%03d", attempt));
         } while (!f.mkdirs());
 
         tempDirBase = f;
@@ -2243,7 +2246,7 @@ public abstract class LuceneTestCase ext
             "Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: "
               + base.getAbsolutePath());            
       }
-      f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%3d", attempt));
+      f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%03d", attempt));
     } while (!f.mkdirs());
 
     registerToRemoveAfterSuite(f);
@@ -2263,7 +2266,7 @@ public abstract class LuceneTestCase ext
             "Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: "
               + base.getAbsolutePath());            
       }
-      f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%3d", attempt) + suffix);
+      f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%03d", attempt) + suffix);
     } while (!f.createNewFile());
 
     registerToRemoveAfterSuite(f);
@@ -2300,14 +2303,6 @@ public abstract class LuceneTestCase ext
       return;
     }
 
-    Class<?> suiteClass = RandomizedContext.current().getTargetClass();
-    if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) {
-      System.err.println("WARNING: Will leave temporary files (bugUrl: "
-          + suiteClass.getAnnotation(SuppressTempFileChecks.class).bugUrl() + "): "
-          + f.getAbsolutePath());
-      return;
-    }
-
     synchronized (cleanupQueue) {
       cleanupQueue.addLast(f);
     }
@@ -2315,15 +2310,28 @@ public abstract class LuceneTestCase ext
 
   private static class TemporaryFilesCleanupRule extends TestRuleAdapter {
     @Override
-    protected void afterIfSuccessful() throws Throwable {
-      synchronized (cleanupQueue) {
-        File [] everything = new File [cleanupQueue.size()];
-        for (int i = 0; !cleanupQueue.isEmpty(); i++) {
-          everything[i] = cleanupQueue.removeFirst();
-        }
+    protected void afterAlways(List<Throwable> errors) throws Throwable {
+      if (LuceneTestCase.suiteFailureMarker.wasSuccessful()) {
+        synchronized (cleanupQueue) {
+          File [] everything = new File [cleanupQueue.size()];
+          for (int i = 0; !cleanupQueue.isEmpty(); i++) {
+            everything[i] = cleanupQueue.removeLast();
+          }
 
-        // Will throw an IOException on un-removable files.
-        TestUtil.rm(everything);
+          // Will throw an IOException on un-removable files.
+          try {
+            TestUtil.rm(everything);
+          } catch (IOException e) {
+            Class<?> suiteClass = RandomizedContext.current().getTargetClass();
+            if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) {
+              System.err.println("WARNING: Leftover undeleted temporary files (bugUrl: "
+                  + suiteClass.getAnnotation(SuppressTempFileChecks.class).bugUrl() + "): "
+                  + e.getMessage());
+              return;
+            }
+            throw e;
+          }
+        }
       }
     }
   }

Modified: lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java?rev=1584843&r1=1584842&r2=1584843&view=diff
==============================================================================
--- lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java (original)
+++ lucene/dev/branches/solr5914/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java Fri Apr  4 18:10:55 2014
@@ -32,6 +32,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
@@ -106,7 +107,7 @@ public final class TestUtil {
    * of directories) cannot be removed.
    */
   public static void rm(File... locations) throws IOException {
-    ArrayList<File> unremoved = rm(new ArrayList<File>(), locations);
+    LinkedHashSet<File> unremoved = rm(new LinkedHashSet<File>(), locations);
     if (!unremoved.isEmpty()) {
       StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n");
       for (File f : unremoved) {
@@ -118,7 +119,7 @@ public final class TestUtil {
     }
   }
 
-  private static ArrayList<File> rm(ArrayList<File> unremoved, File... locations) {
+  private static LinkedHashSet<File> rm(LinkedHashSet<File> unremoved, File... locations) {
     for (File location : locations) {
       if (location.exists()) {
         if (location.isDirectory()) {