Index: lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java =================================================================== --- lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java (revision 1424417) +++ lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java (working copy) @@ -720,7 +720,9 @@ // DirectoryReader on a non-existent directory, you get a // good exception public void testNoDir() throws Throwable { - Directory dir = newFSDirectory(_TestUtil.getTempDir("doesnotexist")); + File tempDir = _TestUtil.getTempDir("doesnotexist"); + _TestUtil.rmDir(tempDir); + Directory dir = newFSDirectory(tempDir); try { DirectoryReader.open(dir); fail("did not hit expected exception"); Index: lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java =================================================================== --- lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java (revision 1424417) +++ lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java (working copy) @@ -17,73 +17,33 @@ * limitations under the License. */ -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; import org.apache.lucene.store.Directory; +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; /** * This tests the patch for issue #LUCENE-715 (IndexWriter does not * release its write lock when trying to open an index which does not yet * exist). */ - public class TestIndexWriterLockRelease extends LuceneTestCase { - private java.io.File __test_dir; - - @Override - public void setUp() throws Exception { - super.setUp(); - if (this.__test_dir == null) { - 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)"); - } - - if (!this.__test_dir.mkdirs() - && !this.__test_dir.isDirectory()) { - throw new IOException("unable to create test directory \"" + this.__test_dir.getPath() + "\""); - } - } - } - - @Override - public void tearDown() throws Exception { - if (this.__test_dir != null) { - File[] files = this.__test_dir.listFiles(); - - for (int i = 0; - i < files.length; - ++i) { - if (!files[i].delete()) { - throw new IOException("unable to remove file in test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)"); - } - } - - if (!this.__test_dir.delete()) { - throw new IOException("unable to remove test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)"); - } - } - super.tearDown(); - } - - public void testIndexWriterLockRelease() throws IOException { - Directory dir = newFSDirectory(this.__test_dir); + + public void testIndexWriterLockRelease() throws IOException { + Directory dir = newFSDirectory(_TestUtil.getTempDir("testLockRelease")); + try { + new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND)); + } catch (FileNotFoundException e) { try { new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND)); - } catch (FileNotFoundException e) { - try { - new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND)); - } catch (FileNotFoundException e1) { - } - } finally { - dir.close(); + } catch (FileNotFoundException e1) { } + } finally { + dir.close(); } + } } Index: lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java =================================================================== --- lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java (revision 1424417) +++ lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java (working copy) @@ -17,6 +17,7 @@ * limitations under the License. */ +import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -84,15 +85,25 @@ } private Directory newFSSwitchDirectory(Set primaryExtensions) throws IOException { - Directory a = new SimpleFSDirectory(_TestUtil.getTempDir("foo")); - Directory b = new SimpleFSDirectory(_TestUtil.getTempDir("bar")); + File primDir = _TestUtil.getTempDir("foo"); + File secondDir = _TestUtil.getTempDir("bar"); + return newFSSwitchDirectory(primDir, secondDir, primaryExtensions); + } + + private Directory newFSSwitchDirectory(File aDir, File bDir, Set primaryExtensions) throws IOException { + Directory a = new SimpleFSDirectory(aDir); + Directory b = new SimpleFSDirectory(bDir); FileSwitchDirectory switchDir = new FileSwitchDirectory(primaryExtensions, a, b, true); return new MockDirectoryWrapper(random(), switchDir); } // LUCENE-3380 -- make sure we get exception if the directory really does not exist. public void testNoDir() throws Throwable { - Directory dir = newFSSwitchDirectory(Collections.emptySet()); + File primDir = _TestUtil.getTempDir("foo"); + File secondDir = _TestUtil.getTempDir("bar"); + _TestUtil.rmDir(primDir); + _TestUtil.rmDir(secondDir); + Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.emptySet()); try { DirectoryReader.open(dir); fail("did not hit expected exception"); Index: lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java =================================================================== --- lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java (revision 1424417) +++ lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java (working copy) @@ -123,7 +123,9 @@ // LUCENE-3382 -- make sure we get exception if the directory really does not exist. public void testNoDir() throws Throwable { - Directory dir = new NRTCachingDirectory(newFSDirectory(_TestUtil.getTempDir("doesnotexist")), 2.0, 25.0); + File tempDir = _TestUtil.getTempDir("doesnotexist"); + _TestUtil.rmDir(tempDir); + Directory dir = new NRTCachingDirectory(newFSDirectory(tempDir), 2.0, 25.0); try { DirectoryReader.open(dir); fail("did not hit expected exception"); Index: lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java =================================================================== --- lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java (revision 1424417) +++ lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java (working copy) @@ -95,17 +95,35 @@ */ public class _TestUtil { - /** Returns temp dir, based on String arg in its name; - * does not create the directory. */ + // the max number of retries we're going to do in getTempDir + private static final int GET_TEMP_DIR_RETRY_THRESHOLD = 1000; + + /** + * Returns a temp directory, based on the given description. Creates the + * directory. + */ public static File getTempDir(String desc) { - try { - File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR); - f.delete(); - LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker)); - return f; - } catch (IOException e) { - throw new RuntimeException(e); + if (desc.length() < 3) { + throw new IllegalArgumentException("description must be at least 3 characters"); } + // always pull a long from master random. that way, the randomness of the test + // is not affected by whether it initialized the counter (in genTempFile) or not. + // note that the Random used by genTempFile is *not* the master Random, and therefore + // does not affect the randomness of the test. + final Random random = new Random(RandomizedContext.current().getRandom().nextLong()); + int attempt = 0; + File f; + do { + f = genTempFile(random, desc, "tmp", LuceneTestCase.TEMP_DIR); + } while (!f.mkdir() && (attempt++) < GET_TEMP_DIR_RETRY_THRESHOLD); + + if (attempt > GET_TEMP_DIR_RETRY_THRESHOLD) { + throw new RuntimeException( + "failed to get a temporary dir too many times. check your temp directory and consider manually cleaning it."); + } + + LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker)); + return f; } /** @@ -738,43 +756,39 @@ */ public static File createTempFile(String prefix, String suffix, File directory) throws IOException { - // Force a prefix null check first if (prefix.length() < 3) { - throw new IllegalArgumentException("prefix must be 3"); + throw new IllegalArgumentException("prefix must be at least 3 characters"); } String newSuffix = suffix == null ? ".tmp" : suffix; + // always pull a long from master random. that way, the randomness of the test + // is not affected by whether it initialized the counter (in genTempFile) or not. + // note that the Random used by genTempFile is *not* the master Random, and therefore + // does not affect the randomness of the test. + final Random random = new Random(RandomizedContext.current().getRandom().nextLong()); File result; - // just pull one long always: we don't want to rely upon what may or may not - // already exist. otherwise tests might not reproduce, depending on when you last - // ran 'ant clean' - final Random random = new Random(RandomizedContext.current().getRandom().nextLong()); do { result = genTempFile(random, prefix, newSuffix, directory); } while (!result.createNewFile()); return result; } + /* identify for differnt VM processes */ + private static String counterBase; + /* Temp file counter */ - private static int counter = 0; + private static int counter; + private static final Object counterLock = new Object(); - /* identify for differnt VM processes */ - private static int counterBase = 0; - - private static class TempFileLocker {}; - private static TempFileLocker tempFileLocker = new TempFileLocker(); - private static File genTempFile(Random random, String prefix, String suffix, File directory) { - int identify = 0; - - synchronized (tempFileLocker) { - if (counter == 0) { - int newInt = random.nextInt(); - counter = ((newInt / 65535) & 0xFFFF) + 0x2710; - counterBase = counter; + final int identify; + synchronized (counterLock) { + if (counterBase == null) { // init once + // make sure the number is small and positive + counter = ((random.nextInt() / 65535) & 0xFFFF) + 0x2710; + counterBase = Integer.toString(counter); } identify = counter++; } - StringBuilder newName = new StringBuilder(); newName.append(prefix); newName.append(counterBase); Index: solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java =================================================================== --- solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java (revision 1424417) +++ solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java (working copy) @@ -82,7 +82,7 @@ assertResults(((StringWriter) w).getBuffer().toString().getBytes("UTF-8")); // again with default file - File tmpFile = _TestUtil.getTempDir("solr.xml"); + File tmpFile = _TestUtil.createTempFile("solr.xml", null, TEMP_DIR); serializer.persistFile(tmpFile, solrXMLDef); Index: solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java =================================================================== --- solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java (revision 1424417) +++ solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java (working copy) @@ -246,9 +246,8 @@ protected File makeFile(String contents, String charset) { try { - File f = _TestUtil.getTempDir("cachetest_csv"); - Writer out = new OutputStreamWriter(new FileOutputStream(f), - charset); + File f = _TestUtil.createTempFile("cachetest_csv", null, TEMP_DIR); + Writer out = new OutputStreamWriter(new FileOutputStream(f), charset); out.write(contents); out.close(); return f;