Index: lucene/backwards/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java =================================================================== --- lucene/backwards/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (revision 1127062) +++ lucene/backwards/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (working copy) @@ -30,9 +30,13 @@ import org.junit.Test; public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPolicy { + // Keep it a class member so that getDeletionPolicy can use it private Directory snapshotDir; - + + // so we can close it if called by SDP tests + private PersistentSnapshotDeletionPolicy psdp; + @Before @Override public void setUp() throws Exception { @@ -43,12 +47,14 @@ @After @Override public void tearDown() throws Exception { + if (psdp != null) psdp.close(); snapshotDir.close(); super.tearDown(); } @Override protected SnapshotDeletionPolicy getDeletionPolicy() throws IOException { + if (psdp != null) psdp.close(); snapshotDir.close(); snapshotDir = newDirectory(); return new PersistentSnapshotDeletionPolicy( Index: lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java =================================================================== --- lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (revision 1127062) +++ lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (working copy) @@ -45,20 +45,23 @@ @Override public void close() throws IOException { - dir.maybeThrowDeterministicException(); - delegate.close(); - 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; + try { + dir.maybeThrowDeterministicException(); + } finally { + delegate.close(); + 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; + } } + synchronized(dir) { + dir.openFileHandles.remove(this); + dir.openFilesForWrite.remove(name); + } } - synchronized(dir) { - dir.openFileHandles.remove(this); - dir.openFilesForWrite.remove(name); - } } @Override Index: lucene/backwards/src/test-framework/org/apache/lucene/util/LuceneTestCase.java =================================================================== --- lucene/backwards/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (revision 1127062) +++ lucene/backwards/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (working copy) @@ -28,6 +28,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.*; +import java.util.Map.Entry; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -52,6 +53,7 @@ import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockFactory; import org.apache.lucene.store.MockDirectoryWrapper; +import org.apache.lucene.util._TestUtil; import org.apache.lucene.util.FieldCacheSanityChecker.Insanity; import org.junit.After; import org.junit.AfterClass; @@ -131,7 +133,7 @@ } /** set of directories we created, in afterclass we try to clean these up */ - static final Set tempDirs = Collections.synchronizedSet(new HashSet()); + private static final Map tempDirs = Collections.synchronizedMap(new HashMap()); // by default we randomly pick a different codec for // each test case (non-J4 tests) and each test class (J4 @@ -284,11 +286,20 @@ } // clear out any temp directories if we can if (!testsFailed) { - for (String path : tempDirs) { + for (Entry entry : tempDirs.entrySet()) { try { - _TestUtil.rmDir(new File(path)); + _TestUtil.rmDir(entry.getKey()); } catch (IOException e) { e.printStackTrace(); + System.err.println("path " + entry.getKey() + " allocated from"); + // first two STE's are Java's + StackTraceElement[] elements = entry.getValue(); + for (int i = 2; i < elements.length; i++) { + StackTraceElement ste = elements[i]; + // print only our code's stack information + if (ste.getClassName().indexOf("org.apache.lucene") == -1) break; + System.err.println("\t" + ste); + } } } } @@ -914,6 +925,11 @@ } } + /** Registers a temp file that will be deleted when tests are done. */ + public static void registerTempFile(File tmpFile) { + tempDirs.put(tmpFile.getAbsoluteFile(), Thread.currentThread().getStackTrace()); + } + static Directory newDirectoryImpl(Random random, String clazzName) { if (clazzName.equals("random")) clazzName = randomDirectory(random); @@ -926,7 +942,7 @@ final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR); tmpFile.delete(); tmpFile.mkdir(); - tempDirs.add(tmpFile.getAbsolutePath()); + registerTempFile(tmpFile); return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null); } Index: lucene/backwards/src/test-framework/org/apache/lucene/util/_TestUtil.java =================================================================== --- lucene/backwards/src/test-framework/org/apache/lucene/util/_TestUtil.java (revision 1127062) +++ lucene/backwards/src/test-framework/org/apache/lucene/util/_TestUtil.java (working copy) @@ -48,7 +48,10 @@ * does not create the directory. */ public static File getTempDir(String desc) { File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong()); - LuceneTestCase.tempDirs.add(f.getAbsolutePath()); + if (f.exists()) { + System.err.println("======================= " + f + " exist =========================="); + } + LuceneTestCase.registerTempFile(f); return f; } @@ -84,7 +87,7 @@ rmDir(destDir); destDir.mkdir(); - LuceneTestCase.tempDirs.add(destDir.getAbsolutePath()); + LuceneTestCase.registerTempFile(destDir); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); Index: lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (revision 1127062) +++ lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (working copy) @@ -30,9 +30,13 @@ import org.junit.Test; public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPolicy { + // Keep it a class member so that getDeletionPolicy can use it private Directory snapshotDir; + // so we can close it if called by SDP tests + private PersistentSnapshotDeletionPolicy psdp; + @Before @Override public void setUp() throws Exception { @@ -43,15 +47,17 @@ @After @Override public void tearDown() throws Exception { + if (psdp != null) psdp.close(); snapshotDir.close(); super.tearDown(); } @Override protected SnapshotDeletionPolicy getDeletionPolicy() throws IOException { + if (psdp != null) psdp.close(); snapshotDir.close(); snapshotDir = newDirectory(); - return new PersistentSnapshotDeletionPolicy( + return psdp = new PersistentSnapshotDeletionPolicy( new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.CREATE, TEST_VERSION_CURRENT); } @@ -173,6 +179,8 @@ fail("should not have reached here - the snapshots directory should be locked!"); } catch (LockObtainFailedException e) { // expected + } finally { + psdp.close(); } // Reading the snapshots info should succeed though Index: lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java =================================================================== --- lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (revision 1127062) +++ lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (working copy) @@ -59,7 +59,7 @@ /** * Reads the snapshots information from the given {@link Directory}. This - * method does can be used if the snapshots information is needed, however you + * method can be used if the snapshots information is needed, however you * cannot instantiate the deletion policy (because e.g., some other process * keeps a lock on the snapshots directory). */ @@ -122,11 +122,19 @@ writer.commit(); } - // Initializes the snapshots information. This code should basically run - // only if mode != CREATE, but if it is, it's no harm as we only open the - // reader once and immediately close it. - for (Entry e : readSnapshotsInfo(dir).entrySet()) { - registerSnapshotInfo(e.getKey(), e.getValue(), null); + try { + // Initializes the snapshots information. This code should basically run + // only if mode != CREATE, but if it is, it's no harm as we only open the + // reader once and immediately close it. + for (Entry e : readSnapshotsInfo(dir).entrySet()) { + registerSnapshotInfo(e.getKey(), e.getValue(), null); + } + } catch (RuntimeException e) { + writer.close(); // don't leave any open file handles + throw e; + } catch (IOException e) { + writer.close(); // don't leave any open file handles + throw e; } } Index: lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java =================================================================== --- lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (revision 1127062) +++ lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (working copy) @@ -45,20 +45,23 @@ @Override public void close() throws IOException { - dir.maybeThrowDeterministicException(); - delegate.close(); - 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; + try { + dir.maybeThrowDeterministicException(); + } finally { + delegate.close(); + 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; + } } + synchronized(dir) { + dir.openFileHandles.remove(this); + dir.openFilesForWrite.remove(name); + } } - synchronized(dir) { - dir.openFileHandles.remove(this); - dir.openFilesForWrite.remove(name); - } } @Override Index: lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java =================================================================== --- lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (revision 1127062) +++ lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (working copy) @@ -28,6 +28,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.*; +import java.util.Map.Entry; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -135,7 +136,7 @@ } /** set of directories we created, in afterclass we try to clean these up */ - static final Set tempDirs = Collections.synchronizedSet(new HashSet()); + private static final Map tempDirs = Collections.synchronizedMap(new HashMap()); // by default we randomly pick a different codec for // each test case (non-J4 tests) and each test class (J4 @@ -177,7 +178,7 @@ SETUP, // test has called setUp() RANTEST, // test is running TEARDOWN // test has called tearDown() - }; + } private static class UncaughtExceptionEntry { public final Thread thread; @@ -303,11 +304,20 @@ } // clear out any temp directories if we can if (!testsFailed) { - for (String path : tempDirs) { + for (Entry entry : tempDirs.entrySet()) { try { - _TestUtil.rmDir(new File(path)); + _TestUtil.rmDir(entry.getKey()); } catch (IOException e) { e.printStackTrace(); + System.err.println("path " + entry.getKey() + " allocated from"); + // first two STE's are Java's + StackTraceElement[] elements = entry.getValue(); + for (int i = 2; i < elements.length; i++) { + StackTraceElement ste = elements[i]; + // print only our code's stack information + if (ste.getClassName().indexOf("org.apache.lucene") == -1) break; + System.err.println("\t" + ste); + } } } } @@ -986,6 +996,11 @@ return d; } + /** Registers a temp file that will be deleted when tests are done. */ + public static void registerTempFile(File tmpFile) { + tempDirs.put(tmpFile.getAbsoluteFile(), Thread.currentThread().getStackTrace()); + } + static Directory newDirectoryImpl(Random random, String clazzName) { if (clazzName.equals("random")) clazzName = randomDirectory(random); @@ -998,7 +1013,7 @@ final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR); tmpFile.delete(); tmpFile.mkdir(); - tempDirs.add(tmpFile.getAbsolutePath()); + registerTempFile(tmpFile); return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null); } Index: lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java =================================================================== --- lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java (revision 1127062) +++ lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java (working copy) @@ -49,8 +49,8 @@ /** Returns temp dir, containing String arg in its name; * does not create the directory. */ public static File getTempDir(String desc) { - File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong()); - LuceneTestCase.tempDirs.add(f.getAbsolutePath()); + File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + LuceneTestCase.random.nextLong()); + LuceneTestCase.registerTempFile(f); return f; } @@ -86,7 +86,7 @@ rmDir(destDir); destDir.mkdir(); - LuceneTestCase.tempDirs.add(destDir.getAbsolutePath()); + LuceneTestCase.registerTempFile(destDir); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement();