Apache Solr open-source search software
LUCENE-4639: improve _TestUtil.getTempDir
Shai Ererad64cfed29181
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.javaindex 9d27f09ea26..d4c6379f1a5 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java@@ -720,7 +720,9 @@ public void testFilesOpenClose() throws IOException {// DirectoryReader on a non-existent directory, you get a// good exceptionpublic 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");diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.javaindex cb2a8e8cb66..aaa0fb1338e 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java@@ -17,73 +17,33 @@ package org.apache.lucene.index;* 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();}+ }}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.javaindex 662fd7d41c2..c18d31e7bcd 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java@@ -17,6 +17,7 @@ package org.apache.lucene.store;* limitations under the License.*/+import java.io.File;import java.io.IOException;import java.util.Arrays;import java.util.Collections;@@ -84,15 +85,25 @@ public class TestFileSwitchDirectory extends LuceneTestCase {}private Directory newFSSwitchDirectory(Set<String> 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<String> 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.<String>emptySet());+ File primDir = _TestUtil.getTempDir("foo");+ File secondDir = _TestUtil.getTempDir("bar");+ _TestUtil.rmDir(primDir);+ _TestUtil.rmDir(secondDir);+ Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet());try {DirectoryReader.open(dir);fail("did not hit expected exception");diff --git a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.javaindex 6f6aa237e48..e9d0aa05d36 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java@@ -123,7 +123,9 @@ public class TestNRTCachingDirectory extends LuceneTestCase {// 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");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.javaindex b0860709e9e..4a3b0eaff7c 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@@ -95,17 +95,35 @@ import com.carrotsearch.randomizedtesting.generators.RandomPicks;*/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,38 @@ public class _TestUtil {*/public static File createTempFile(String prefix, String suffix, File directory)throws IOException {- // Force a prefix null check firstif (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;- 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'+ // 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;do {result = genTempFile(random, prefix, newSuffix, directory);} while (!result.createNewFile());return result;}- /* Temp file counter */- private static int counter = 0;-/* identify for differnt VM processes */- private static int counterBase = 0;-- private static class TempFileLocker {};- private static TempFileLocker tempFileLocker = new TempFileLocker();+ private static String counterBase;++ /* Temp file counter */+ private static int counter;+ private static final Object counterLock = new Object();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+ counter = random.nextInt() & 0xFFFF; // up to five digits number+ counterBase = Integer.toString(counter);}identify = counter++;}-StringBuilder newName = new StringBuilder();newName.append(prefix);newName.append(counterBase);diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java b/solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.javaindex cfd457775d9..2aa88f03047 100644--- a/solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java+++ b/solr/core/src/test/org/apache/solr/core/TestSolrXMLSerializer.java@@ -82,7 +82,7 @@ public class TestSolrXMLSerializer extends LuceneTestCase {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);diff --git a/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java b/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.javaindex 20b58166772..d3a651fc25d 100644--- a/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java+++ b/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java@@ -246,9 +246,8 @@ public class CacheHeaderTest extends CacheHeaderTestBase {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;