Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Modified the rule to attempt to remove temporary files, but report only (not throw an exception). Added tests.

Dawid Weiss8cf904fd8ea3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 79911beb6c6..64b6c0dd229 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -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)
diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
index b8157a87fbb..a34efd564f4 100644
--- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
+++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java
@@ -18,13 +18,19 @@ package org.apache.lucene.util.junitcompat;
*/
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 extends WithNestedTests {
static File file;
public void testDummy() {
file = createTempDir("leftover");
- file.mkdirs();
fail();
}
}
@@ -43,7 +48,33 @@ public class TestLeaveFilesIfTestFails extends WithNestedTests {
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);
+ }
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 51e56243a4f..a9d23c724cc 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -2196,8 +2196,11 @@ public abstract class LuceneTestCase extends Assert {
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 extends Assert {
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 extends Assert {
+ 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 extends Assert {
"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 extends Assert {
"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 extends Assert {
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 extends Assert {
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;
+ }
+ }
}
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java
index bba87713ec7..a634f02c73b 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java
@@ -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()) {