From b48ad6841f0e57810c3767e2a4f64f6fb2eb1fff Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 22 Apr 2011 14:46:14 +0000 Subject: [PATCH] LUCENE-2796: Lucene tests need to clean up after themselves git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1095937 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/util/LuceneTestCase.java | 15 +++++++++++++++ .../org/apache/lucene/util/_TestUtil.java | 5 ++++- .../org/apache/lucene/index/TestCompoundFile.java | 4 +--- .../src/test/org/apache/lucene/index/TestDoc.java | 5 +++-- .../org/apache/lucene/index/TestFieldsReader.java | 5 ++--- .../org/apache/lucene/index/TestIndexReader.java | 2 +- .../lucene/index/TestIndexReaderReopen.java | 2 +- .../org/apache/lucene/index/TestIndexWriter.java | 2 +- .../lucene/index/TestIndexWriterLockRelease.java | 3 ++- .../lucene/index/TestIndexWriterOnJRECrash.java | 2 +- .../org/apache/lucene/index/TestLongPostings.java | 2 +- .../lucene/store/TestBufferedIndexInput.java | 2 +- .../org/apache/lucene/store/TestDirectory.java | 8 ++++---- .../org/apache/lucene/store/TestMultiMMap.java | 2 +- .../org/apache/lucene/store/TestRAMDirectory.java | 3 ++- .../org/apache/lucene/store/TestWindowsMMap.java | 3 ++- 16 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java index 07b5c6261ac..ba9064acc65 100644 --- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java @@ -128,6 +128,9 @@ public abstract class LuceneTestCase extends Assert { TEMP_DIR = new File(s); TEMP_DIR.mkdirs(); } + + /** set of directories we created, in afterclass we try to clean these up */ + static final Set tempDirs = Collections.synchronizedSet(new HashSet()); // by default we randomly pick a different codec for // each test case (non-J4 tests) and each test class (J4 @@ -323,6 +326,7 @@ public abstract class LuceneTestCase extends Assert { public static void beforeClassLuceneTestCaseJ4() { staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1; random.setSeed(staticSeed); + tempDirs.clear(); stores = Collections.synchronizedMap(new IdentityHashMap()); savedCodecProvider = CodecProvider.getDefault(); if ("randomPerField".equals(TEST_CODEC)) { @@ -411,6 +415,16 @@ public abstract class LuceneTestCase extends Assert { + "free=" + Runtime.getRuntime().freeMemory() + "," + "total=" + Runtime.getRuntime().totalMemory()); } + // clear out any temp directories if we can + if (!testsFailed) { + for (String path : tempDirs) { + try { + _TestUtil.rmDir(new File(path)); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } private static boolean testsFailed; /* true if any tests failed */ @@ -1063,6 +1077,7 @@ public abstract class LuceneTestCase extends Assert { final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR); tmpFile.delete(); tmpFile.mkdir(); + tempDirs.add(tmpFile.getAbsolutePath()); return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null); } diff --git a/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java b/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java index 19609d03ef7..be1a4500f1e 100644 --- a/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java +++ b/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java @@ -54,7 +54,9 @@ public class _TestUtil { /** Returns temp dir, containing String arg in its name; * does not create the directory. */ public static File getTempDir(String desc) { - return new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong()); + File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong()); + LuceneTestCase.tempDirs.add(f.getAbsolutePath()); + return f; } /** @@ -89,6 +91,7 @@ public class _TestUtil { rmDir(destDir); destDir.mkdir(); + LuceneTestCase.tempDirs.add(destDir.getAbsolutePath()); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); diff --git a/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java b/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java index 9e4eba610e2..88d499e1359 100644 --- a/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java +++ b/lucene/src/test/org/apache/lucene/index/TestCompoundFile.java @@ -57,8 +57,7 @@ public class TestCompoundFile extends LuceneTestCase @Override public void setUp() throws Exception { super.setUp(); - File file = new File(TEMP_DIR, "testIndex"); - _TestUtil.rmDir(file); + File file = _TestUtil.getTempDir("testIndex"); // use a simple FSDir here, to be sure to have SimpleFSInputs dir = new SimpleFSDirectory(file,null); } @@ -66,7 +65,6 @@ public class TestCompoundFile extends LuceneTestCase @Override public void tearDown() throws Exception { dir.close(); - _TestUtil.rmDir(new File(TEMP_DIR, "testIndex")); super.tearDown(); } diff --git a/lucene/src/test/org/apache/lucene/index/TestDoc.java b/lucene/src/test/org/apache/lucene/index/TestDoc.java index 049e9072f67..874df62c91a 100644 --- a/lucene/src/test/org/apache/lucene/index/TestDoc.java +++ b/lucene/src/test/org/apache/lucene/index/TestDoc.java @@ -36,6 +36,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; import org.apache.lucene.index.codecs.CodecProvider; @@ -60,10 +61,10 @@ public class TestDoc extends LuceneTestCase { if (VERBOSE) { System.out.println("TEST: setUp"); } - workDir = new File(TEMP_DIR,"TestDoc"); + workDir = _TestUtil.getTempDir("TestDoc"); workDir.mkdirs(); - indexDir = new File(workDir, "testIndex"); + indexDir = _TestUtil.getTempDir("testIndex"); indexDir.mkdirs(); Directory directory = newFSDirectory(indexDir); diff --git a/lucene/src/test/org/apache/lucene/index/TestFieldsReader.java b/lucene/src/test/org/apache/lucene/index/TestFieldsReader.java index 57e00290ef6..26b1717072f 100644 --- a/lucene/src/test/org/apache/lucene/index/TestFieldsReader.java +++ b/lucene/src/test/org/apache/lucene/index/TestFieldsReader.java @@ -286,8 +286,7 @@ public class TestFieldsReader extends LuceneTestCase { */ public void testLazyPerformance() throws Exception { String userName = System.getProperty("user.name"); - File file = new File(TEMP_DIR, "lazyDir" + userName); - _TestUtil.rmDir(file); + File file = _TestUtil.getTempDir("lazyDir" + userName); Directory tmpDir = newFSDirectory(file); assertTrue(tmpDir != null); @@ -473,7 +472,7 @@ public class TestFieldsReader extends LuceneTestCase { // LUCENE-1262 public void testExceptions() throws Throwable { - File indexDir = new File(TEMP_DIR, "testfieldswriterexceptions"); + File indexDir = _TestUtil.getTempDir("testfieldswriterexceptions"); try { Directory dir = new FaultyFSDirectory(indexDir); diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java index e0c700e6001..7f7fa8bf34f 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java @@ -1139,7 +1139,7 @@ public class TestIndexReader extends LuceneTestCase } public void testOpenReaderAfterDelete() throws IOException { - File dirFile = new File(TEMP_DIR, "deletetest"); + File dirFile = _TestUtil.getTempDir("deletetest"); Directory dir = newFSDirectory(dirFile); try { IndexReader.open(dir, false); diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java b/lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java index b7ff7cc7fd2..f55a0c7e2c1 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java @@ -1090,7 +1090,7 @@ public class TestIndexReaderReopen extends LuceneTestCase { @Override public void setUp() throws Exception { super.setUp(); - indexDir = new File(TEMP_DIR, "IndexReaderReopen"); + indexDir = _TestUtil.getTempDir("IndexReaderReopen"); } public void testCloseOrig() throws Throwable { diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java index ed6f862830d..261a0bebd41 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2755,7 +2755,7 @@ public class TestIndexWriter extends LuceneTestCase { // Tests that if FSDir is opened w/ a NoLockFactory (or SingleInstanceLF), // then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed // when listAll() was called in IndexFileDeleter. - Directory dir = newFSDirectory(new File(TEMP_DIR, "emptyFSDirNoLock"), NoLockFactory.getNoLockFactory()); + Directory dir = newFSDirectory(_TestUtil.getTempDir("emptyFSDirNoLock"), NoLockFactory.getNoLockFactory()); new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))).close(); dir.close(); } diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java index 3a4a38163af..58eea6b9609 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig.OpenMode; @@ -39,7 +40,7 @@ public class TestIndexWriterLockRelease extends LuceneTestCase { public void setUp() throws Exception { super.setUp(); if (this.__test_dir == null) { - this.__test_dir = new File(TEMP_DIR, "testIndexWriter"); + this.__test_dir = _TestUtil.getTempDir("testIndexWriter"); if (this.__test_dir.exists()) { throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)"); diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java index cc5638fc56d..e3d68a77bec 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java @@ -41,7 +41,7 @@ public class TestIndexWriterOnJRECrash extends TestNRTThreads { @Override public void setUp() throws Exception { super.setUp(); - tempDir = File.createTempFile("jrecrash", "tmp", TEMP_DIR); + tempDir = _TestUtil.getTempDir("jrecrash"); tempDir.delete(); tempDir.mkdir(); } diff --git a/lucene/src/test/org/apache/lucene/index/TestLongPostings.java b/lucene/src/test/org/apache/lucene/index/TestLongPostings.java index 086b3a98e00..fbe6fa1e0f4 100644 --- a/lucene/src/test/org/apache/lucene/index/TestLongPostings.java +++ b/lucene/src/test/org/apache/lucene/index/TestLongPostings.java @@ -67,7 +67,7 @@ public class TestLongPostings extends LuceneTestCase { // Don't use _TestUtil.getTempDir so that we own the // randomness (ie same seed will point to same dir): - Directory dir = newFSDirectory(new File(LuceneTestCase.TEMP_DIR, "longpostings" + "." + random.nextLong())); + Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong())); final int NUM_DOCS = (int) ((TEST_NIGHTLY ? 4e6 : (RANDOM_MULTIPLIER*2e4)) * (1+random.nextDouble())); diff --git a/lucene/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/lucene/src/test/org/apache/lucene/store/TestBufferedIndexInput.java index 2bb66ba83bc..cab6a66b266 100755 --- a/lucene/src/test/org/apache/lucene/store/TestBufferedIndexInput.java +++ b/lucene/src/test/org/apache/lucene/store/TestBufferedIndexInput.java @@ -240,7 +240,7 @@ public class TestBufferedIndexInput extends LuceneTestCase { } public void testSetBufferSize() throws IOException { - File indexDir = new File(TEMP_DIR, "testSetBufferSize"); + File indexDir = _TestUtil.getTempDir("testSetBufferSize"); MockFSDirectory dir = new MockFSDirectory(indexDir, random); try { IndexWriter writer = new IndexWriter( diff --git a/lucene/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/src/test/org/apache/lucene/store/TestDirectory.java index a2aab82a689..03e6f150574 100644 --- a/lucene/src/test/org/apache/lucene/store/TestDirectory.java +++ b/lucene/src/test/org/apache/lucene/store/TestDirectory.java @@ -42,7 +42,7 @@ public class TestDirectory extends LuceneTestCase { // Test that different instances of FSDirectory can coexist on the same // path, can read, write, and lock files. public void testDirectInstantiation() throws Exception { - File path = new File(TEMP_DIR, "testDirectInstantiation"); + File path = _TestUtil.getTempDir("testDirectInstantiation"); int sz = 3; Directory[] dirs = new Directory[sz]; @@ -134,7 +134,7 @@ public class TestDirectory extends LuceneTestCase { // LUCENE-1468 public void testFSDirectoryFilter() throws IOException { - checkDirectoryFilter(newFSDirectory(new File(TEMP_DIR,"test"))); + checkDirectoryFilter(newFSDirectory(_TestUtil.getTempDir("test"))); } // LUCENE-1468 @@ -151,7 +151,7 @@ public class TestDirectory extends LuceneTestCase { // LUCENE-1468 public void testCopySubdir() throws Throwable { - File path = new File(TEMP_DIR, "testsubdir"); + File path = _TestUtil.getTempDir("testsubdir"); try { path.mkdirs(); new File(path, "subdir").mkdirs(); @@ -164,7 +164,7 @@ public class TestDirectory extends LuceneTestCase { // LUCENE-1468 public void testNotDirectory() throws Throwable { - File path = new File(TEMP_DIR, "testnotdir"); + File path = _TestUtil.getTempDir("testnotdir"); Directory fsDir = new SimpleFSDirectory(path, null); try { IndexOutput out = fsDir.createOutput("afile"); diff --git a/lucene/src/test/org/apache/lucene/store/TestMultiMMap.java b/lucene/src/test/org/apache/lucene/store/TestMultiMMap.java index 2105b5e5c08..16d0d83623f 100644 --- a/lucene/src/test/org/apache/lucene/store/TestMultiMMap.java +++ b/lucene/src/test/org/apache/lucene/store/TestMultiMMap.java @@ -41,7 +41,7 @@ public class TestMultiMMap extends LuceneTestCase { @Override public void setUp() throws Exception { super.setUp(); - workDir = new File(TEMP_DIR, "TestMultiMMap"); + workDir = _TestUtil.getTempDir("TestMultiMMap"); workDir.mkdirs(); } diff --git a/lucene/src/test/org/apache/lucene/store/TestRAMDirectory.java b/lucene/src/test/org/apache/lucene/store/TestRAMDirectory.java index df249e6902a..592481d736b 100644 --- a/lucene/src/test/org/apache/lucene/store/TestRAMDirectory.java +++ b/lucene/src/test/org/apache/lucene/store/TestRAMDirectory.java @@ -24,6 +24,7 @@ import java.io.ObjectOutputStream; import java.io.ByteArrayOutputStream; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; @@ -49,7 +50,7 @@ public class TestRAMDirectory extends LuceneTestCase { @Override public void setUp() throws Exception { super.setUp(); - indexDir = new File(TEMP_DIR, "RAMDirIndex"); + indexDir = _TestUtil.getTempDir("RAMDirIndex"); Directory dir = newFSDirectory(indexDir); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( diff --git a/lucene/src/test/org/apache/lucene/store/TestWindowsMMap.java b/lucene/src/test/org/apache/lucene/store/TestWindowsMMap.java index 58443180d6f..88f333f9651 100644 --- a/lucene/src/test/org/apache/lucene/store/TestWindowsMMap.java +++ b/lucene/src/test/org/apache/lucene/store/TestWindowsMMap.java @@ -20,6 +20,7 @@ package org.apache.lucene.store; import java.io.File; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; @@ -59,7 +60,7 @@ public class TestWindowsMMap extends LuceneTestCase { } private final static String storePathname = - new File(TEMP_DIR,"testLuceneMmap").getAbsolutePath(); + _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath(); public void testMmapIndex() throws Exception { // sometimes the directory is not cleaned by rmDir, because on Windows it