Cleanups and refactorings after Mark's suggestions.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/solr5914@1584633 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2014-04-04 12:04:49 +00:00
parent bc43bebedf
commit ee084bbe60
25 changed files with 205 additions and 192 deletions

View File

@ -990,6 +990,9 @@
<propertyref prefix="tests.jettyConnector" />
<propertyref prefix="tests.disableHdfs" />
<propertyref prefix="tests.filter" />
<propertyref prefix="tests.leavetmpdir" />
<propertyref prefix="tests.leaveTemporary" />
<propertyref prefix="solr.test.leavetmpdir" />
</syspropertyset>
<!-- Pass randomized settings to the forked JVM. -->

View File

@ -179,6 +179,6 @@ public class TestAtomicUpdate extends LuceneTestCase {
directory = newFSDirectory(dirPath);
runTest(directory);
directory.close();
TestUtil.rmDir(dirPath);
TestUtil.rm(dirPath);
}
}

View File

@ -446,7 +446,7 @@ public void testFilesOpenClose() throws IOException {
dir.close();
// Try to erase the data - this ensures that the writer closed all files
TestUtil.rmDir(dirFile);
TestUtil.rm(dirFile);
dir = newFSDirectory(dirFile);
// Now create the data set again, just as before
@ -463,7 +463,7 @@ public void testFilesOpenClose() throws IOException {
// The following will fail if reader did not close
// all files
TestUtil.rmDir(dirFile);
TestUtil.rm(dirFile);
}
public void testOpenReaderAfterDelete() throws IOException {
@ -716,7 +716,7 @@ public void testFilesOpenClose() throws IOException {
// good exception
public void testNoDir() throws Throwable {
File tempDir = TestUtil.createTempDir("doesnotexist");
TestUtil.rmDir(tempDir);
TestUtil.rm(tempDir);
Directory dir = newFSDirectory(tempDir);
try {
DirectoryReader.open(dir);

View File

@ -223,7 +223,7 @@ public class TestFieldsReader extends LuceneTestCase {
reader.close();
dir.close();
} finally {
TestUtil.rmDir(indexDir);
TestUtil.rm(indexDir);
}
}

View File

@ -108,6 +108,6 @@ public class TestNeverDelete extends LuceneTestCase {
w.close();
d.close();
TestUtil.rmDir(tmpDir);
TestUtil.rm(tmpDir);
}
}

View File

@ -274,7 +274,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {
writer.close();
reader.close();
} finally {
TestUtil.rmDir(indexDir);
TestUtil.rm(indexDir);
}
}

View File

@ -218,7 +218,7 @@ public class TestDirectory extends LuceneTestCase {
assertFalse(dir.isOpen);
}
TestUtil.rmDir(path);
TestUtil.rm(path);
}
// LUCENE-1464
@ -230,7 +230,7 @@ public class TestDirectory extends LuceneTestCase {
assertTrue(!path.exists());
dir.close();
} finally {
TestUtil.rmDir(path);
TestUtil.rm(path);
}
}
@ -265,7 +265,7 @@ public class TestDirectory extends LuceneTestCase {
Directory fsDir = new SimpleFSDirectory(path, null);
assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length);
} finally {
TestUtil.rmDir(path);
TestUtil.rm(path);
}
}
@ -285,7 +285,7 @@ public class TestDirectory extends LuceneTestCase {
}
} finally {
fsDir.close();
TestUtil.rmDir(path);
TestUtil.rm(path);
}
}
}

View File

@ -104,8 +104,8 @@ public class TestFileSwitchDirectory extends LuceneTestCase {
public void testNoDir() throws Throwable {
File primDir = TestUtil.createTempDir("foo");
File secondDir = TestUtil.createTempDir("bar");
TestUtil.rmDir(primDir);
TestUtil.rmDir(secondDir);
TestUtil.rm(primDir);
TestUtil.rm(secondDir);
Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet());
try {
DirectoryReader.open(dir);

View File

@ -171,7 +171,7 @@ public class TestLockFactory extends LuceneTestCase {
dir.close();
// Cleanup
TestUtil.rmDir(indexDir);
TestUtil.rm(indexDir);
}
// Verify: NativeFSLockFactory works correctly
@ -250,8 +250,8 @@ public class TestLockFactory extends LuceneTestCase {
dir1.close();
dir2.close();
TestUtil.rmDir(fdir1);
TestUtil.rmDir(fdir2);
TestUtil.rm(fdir1);
TestUtil.rm(fdir2);
}
// Verify: default LockFactory has no prefix (ie
@ -273,7 +273,7 @@ public class TestLockFactory extends LuceneTestCase {
assertNull("Default lock prefix should be null", dir.getLockFactory().getLockPrefix());
dir.close();
TestUtil.rmDir(dirName);
TestUtil.rm(dirName);
}
private class WriterThread extends Thread {

View File

@ -124,7 +124,7 @@ public class TestNRTCachingDirectory extends LuceneTestCase {
// LUCENE-3382 -- make sure we get exception if the directory really does not exist.
public void testNoDir() throws Throwable {
File tempDir = TestUtil.createTempDir("doesnotexist");
TestUtil.rmDir(tempDir);
TestUtil.rm(tempDir);
Directory dir = new NRTCachingDirectory(newFSDirectory(tempDir), 2.0, 25.0);
try {
DirectoryReader.open(dir);

View File

@ -645,7 +645,7 @@ public abstract class ThreadedIndexingAndSearchingTestCase extends LuceneTestCas
TestUtil.checkIndex(dir);
dir.close();
TestUtil.rmDir(tempDir);
TestUtil.rm(tempDir);
if (VERBOSE) {
System.out.println("TEST: done [" + (System.currentTimeMillis()-t0) + " ms]");

View File

@ -228,6 +228,23 @@ public abstract class LuceneTestCase extends Assert {
String[] value();
}
/**
* Marks any suites which are known not to close all the temporary
* files. This may prevent temp. files and folders from being cleaned
* up after the suite is completed.
*
* @see TestUtil#createTempDir()
* @see TestUtil#createTempFile(String, String)
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SuppressTempFileChecks {
/** Point to JIRA entry. */
public String bugUrl() default "None";
}
// -----------------------------------------------------------------
// Truly immutable fields and constants, initialized once and valid
// for all suites ever since.
@ -291,6 +308,20 @@ public abstract class LuceneTestCase extends Assert {
/** Throttling, see {@link MockDirectoryWrapper#setThrottling(Throttling)}. */
public static final Throttling TEST_THROTTLING = TEST_NIGHTLY ? Throttling.SOMETIMES : Throttling.NEVER;
/** Leave temporary files on disk, even on successful runs. */
public static final boolean LEAVE_TEMPORARY;
static {
boolean defaultValue = false;
for (String property : Arrays.asList(
"tests.leaveTemporary" /* ANT tasks's (junit4) flag. */,
"tests.leavetemporary" /* lowercase */,
"tests.leavetmpdir" /* default */,
"solr.test.leavetmpdir" /* Solr's legacy */)) {
defaultValue |= systemPropertyAsBoolean(property, false);
}
LEAVE_TEMPORARY = defaultValue;
}
/**
* These property keys will be ignored in verification of altered properties.
* @see SystemPropertiesInvariantRule
@ -1147,7 +1178,7 @@ public abstract class LuceneTestCase extends Assert {
final Class<? extends Directory> clazz = CommandLineUtil.loadDirectoryClass(clazzName);
// If it is a FSDirectory type, try its ctor(File)
if (FSDirectory.class.isAssignableFrom(clazz)) {
final File dir = TestUtil.createTempDir("index");
final File dir = new File(TestUtil.createTempDir(), "index");
dir.mkdirs(); // ensure it's created so we 'have' it.
return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), dir);
}

View File

@ -24,12 +24,12 @@ import java.io.IOException;
/**
* A {@link Closeable} that attempts to remove a given file/folder.
*/
final class CloseableFile implements Closeable {
final class RemoveUponClose implements Closeable {
private final File file;
private final TestRuleMarkFailure failureMarker;
private final String creationStack;
public CloseableFile(File file, TestRuleMarkFailure failureMarker) {
public RemoveUponClose(File file, TestRuleMarkFailure failureMarker) {
this.file = file;
this.failureMarker = failureMarker;
@ -45,20 +45,12 @@ final class CloseableFile implements Closeable {
// only if there were no other test failures.
if (failureMarker.wasSuccessful()) {
if (file.exists()) {
if (file.listFiles().length > 0) {
// throw new IOException("Temporary folder not clean: " + file.getAbsolutePath() + ". Created at stack trace:\n" + creationStack);
}
try {
TestUtil.rmDir(file);
TestUtil.rm(file);
} catch (IOException e) {
// Ignore the exception from rmDir.
}
// Re-check.
if (file.exists()) {
throw new IOException(
"Could not remove temporary folder: " + file.getAbsolutePath() + ". Created at stack trace:\n" + creationStack);
"Could not remove temporary location '"
+ file.getAbsolutePath() + "', created at stack trace:\n" + creationStack, e);
}
}
}

View File

@ -28,6 +28,7 @@ import java.io.PrintStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
@ -36,6 +37,7 @@ import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.zip.ZipEntry;
@ -85,6 +87,7 @@ import org.apache.lucene.search.FilteredQuery.FilterStrategy;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.junit.Assert;
import com.carrotsearch.randomizedtesting.RandomizedContext;
@ -101,26 +104,36 @@ public final class TestUtil {
// the max number of retries we're going to do in getTempDir
private static final int GET_TEMP_DIR_RETRY_THRESHOLD = 1000;
/**
* Deletes a file or a directory (and everything underneath it).
*/
public static void rm(File location) throws IOException {
if (!location.exists()) {
return;
}
if (location.isDirectory()) {
for (File f : location.listFiles()) {
rm(f);
ArrayList<File> unremoved = rm(location, new ArrayList<File>());
if (!unremoved.isEmpty()) {
StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n");
for (File f : unremoved) {
b.append(" ")
.append(f.getAbsolutePath())
.append("\n");
}
} else {
throw new IOException(b.toString());
}
}
private static ArrayList<File> rm(File location, ArrayList<File> unremoved) {
if (location.exists()) {
if (location.isDirectory()) {
for (File f : location.listFiles()) {
rm(f, unremoved);
}
}
if (!location.delete()) {
throw new IOException("Could not delete: " + location.getAbsolutePath());
unremoved.add(location);
}
}
assert !location.exists();
return unremoved;
}
/**
@ -130,7 +143,7 @@ public final class TestUtil {
public static void unzip(File zipName, File destDir) throws IOException {
rm(destDir);
destDir.mkdir();
LuceneTestCase.closeAfterSuite(new CloseableFile(destDir, LuceneTestCase.suiteFailureMarker));
maybeRemoveAfterSuite(destDir);
ZipFile zipFile = new ZipFile(zipName);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
@ -779,104 +792,128 @@ public final class TestUtil {
}
/**
* Returns a new, empty temporary folder, based on the given name. The folder will be
* deleted at the end of the suite. Failure to delete the temporary folder will cause
* an exception (typically on Windows).
* Returns a new, empty temporary folder, based on the current test class's name.
*
* @see #createTempDir(String, File)
*/
public static File createTempDir(String name) {
return createTempDir(name, new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir"))));
public static File createTempDir() {
Class<?> clazz = RandomizedContext.current().getTargetClass();
String prefix = clazz.getName();
prefix = prefix.replaceFirst("^org.apache.lucene.", "lucene-");
prefix = prefix.replaceFirst("^org.apache.solr.", "solr-");
return createTempDir(prefix);
}
/**
* Returns a new, empty temporary folder, based on the given name. The folder will be
* deleted at the end of the suite. Failure to delete the temporary folder will cause
* an exception (typically on Windows).
* @see #createTempDir(String, File)
*/
public static File createTempDir(String name, File tmpDir) {
return createTempDir(name, tmpDir, true);
public static File createTempDir(String prefix) {
return createTempDir(prefix, null);
}
/**
* Returns a new, empty temporary folder, based on the given name. The folder will be
* deleted at the end of the suite. Failure to delete the temporary folder will cause
* an exception (typically on Windows).
* Returns a new and empty temporary folder, based on the given name. The folder will be
* deleted at the end of the test suite. Failure to delete the temporary folder will cause
* an exception (typically happens on Windows on unclosed file handles).
*/
public static File createTempDir(String name, File tmpDir, boolean ensureCleanedUp) {
if (name.length() < 3) {
throw new IllegalArgumentException("description must be at least 3 characters");
public static File createTempDir(String prefix, File parent) {
if (prefix.length() < 3) {
throw new IllegalArgumentException("The prefix must be at least 3 characters: " + prefix);
}
// always pull a long from master random. that way, the randomness of the test
if (parent == null) {
parent = tempDirectory();
}
if (!parent.exists()) {
throw new RuntimeException("Parent location does not exist: " + parent.getAbsolutePath());
}
if (!parent.isDirectory()) {
throw new RuntimeException("Parent location is not a folder: " + parent.getAbsolutePath());
}
if (!parent.canWrite()) {
throw new RuntimeException("Parent folder not writeable: " + parent.getAbsolutePath());
}
// 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, name + "_", "", tmpDir);
f = genTempFile(prefix + "_", "", parent);
} 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.");
"Failed to get a temporary dir too many times, check your temp directory and consider manually cleaning it: "
+ parent.getAbsolutePath());
}
if (ensureCleanedUp) {
LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker));
return maybeRemoveAfterSuite(f);
}
private static File maybeRemoveAfterSuite(File f) {
if (LuceneTestCase.LEAVE_TEMPORARY) {
System.err.println("INFO: Will leave temporary file: " + f.getAbsolutePath());
return f;
}
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 f;
}
LuceneTestCase.closeAfterSuite(new RemoveUponClose(f, LuceneTestCase.suiteFailureMarker));
return f;
}
public static File createTempFile(String prefix, String suffix) throws IOException {
String tmpDir = System.getProperty("tempDir", System.getProperty("java.io.tmpdir"));
return createTempFile(prefix, suffix, new File(tmpDir));
return createTempFile(prefix, suffix, tempDirectory());
}
/**
* Do NOT expose this method public. Use {@link #createTempDir()} instead.
*/
private static File tempDirectory() {
File directory = new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir")));
assert directory.exists() &&
directory.isDirectory() &&
directory.canWrite();
return directory;
}
/**
* Insecure, fast version of {@link File#createTempFile(String, String)}, uses
* Random instead of SecureRandom.
*/
public static File createTempFile(String prefix, String suffix, File directory)
throws IOException {
public static File createTempFile(String prefix, String suffix, File directory) throws IOException {
if (prefix.length() < 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;
do {
result = genTempFile(random, prefix, newSuffix, directory);
result = genTempFile(prefix, newSuffix, directory);
} while (!result.createNewFile());
LuceneTestCase.closeAfterSuite(new CloseableFile(result, LuceneTestCase.suiteFailureMarker));
return result;
return maybeRemoveAfterSuite(result);
}
/* identify for differnt VM processes */
private static String counterBase;
/* Temp file counter */
private static int counter;
private static final Object counterLock = new Object();
private static final AtomicInteger counter = new AtomicInteger();
private static File genTempFile(Random random, String prefix, String suffix, File directory) {
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);
newName.append(identify);
newName.append(suffix);
return new File(directory, newName.toString());
private static File genTempFile(String prefix, String suffix, File directory) {
return new File(directory,
prefix
+ RandomizedContext.current().getRunnerSeedAsString()
+ "-" + counter.incrementAndGet()
+ suffix);
}
public static void assertEquals(TopDocs expected, TopDocs actual) {

View File

@ -20,8 +20,6 @@ import java.io.File;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.request.DirectXmlRequest;
@ -151,10 +149,7 @@ public class TestContentStreamDataSource extends AbstractDataImportHandlerTestCa
public void setUp() throws Exception {
File home = new File(dataDir,
getClass().getName() + "-" + System.currentTimeMillis());
homeDir = new File(home, "inst");
homeDir = createTempDir("inst");
dataDir = new File(homeDir + "/collection1", "data");
confDir = new File(homeDir + "/collection1", "conf");

View File

@ -76,7 +76,7 @@ public class ResourceLoaderTest extends SolrTestCaseJ4
}
loader.close();
} finally {
TestUtil.rmDir(temp);
TestUtil.rm(temp);
}
}

View File

@ -23,8 +23,8 @@ import java.util.List;
import junit.framework.Assert;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.BaseDistributedSearchTestCase;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
@ -41,7 +41,7 @@ import org.junit.BeforeClass;
* @see org.apache.solr.handler.component.SpellCheckComponent
*/
@Slow
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class DistributedSpellCheckComponentTest extends BaseDistributedSearchTestCase {
public DistributedSpellCheckComponentTest()

View File

@ -21,8 +21,8 @@ import java.io.File;
import java.util.*;
import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SpellingParams;
@ -42,7 +42,7 @@ import org.junit.Test;
* @since solr 1.3
*/
@Slow
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class SpellCheckComponentTest extends SolrTestCaseJ4 {
static String rh = "spellCheckCompRH";

View File

@ -21,8 +21,8 @@ import java.util.Collection;
import java.util.Map;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.params.SpellingParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
@ -35,7 +35,7 @@ import org.junit.Test;
/**
* Simple tests for {@link DirectSolrSpellChecker}
*/
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class DirectSolrSpellCheckerTest extends SolrTestCaseJ4 {
private static SpellingQueryConverter queryConverter;

View File

@ -24,8 +24,8 @@ import java.util.Map;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.search.SolrIndexSearcher;
@ -38,7 +38,7 @@ import org.junit.Test;
*
* @since solr 1.3
**/
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class FileBasedSpellCheckerTest extends SolrTestCaseJ4 {
private static SpellingQueryConverter queryConverter;

View File

@ -37,8 +37,8 @@ import org.apache.lucene.search.spell.SuggestMode;
import org.apache.lucene.search.spell.SuggestWord;
import org.apache.lucene.search.spell.SuggestWordFrequencyComparator;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.SpellCheckComponent;
@ -51,7 +51,7 @@ import org.junit.Test;
/**
* @since solr 1.3
*/
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class IndexBasedSpellCheckerTest extends SolrTestCaseJ4 {
protected static SpellingQueryConverter queryConverter;

View File

@ -21,9 +21,9 @@ import java.util.List;
import java.util.Set;
import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.lucene.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.GroupParams;
@ -42,7 +42,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
@Slow
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class SpellCheckCollatorTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {

View File

@ -23,8 +23,8 @@ import java.util.Map;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.SpellCheckComponent;
@ -33,7 +33,7 @@ import org.apache.solr.util.RefCounted;
import org.junit.BeforeClass;
import org.junit.Test;
@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")
public class WordBreakSolrSpellCheckerTest extends SolrTestCaseJ4 {
@BeforeClass

View File

@ -134,25 +134,9 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
public String bugUrl() default "None";
}
/**
* Annotation for test classes to prevent TEMP_DIR cleanup.
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SuppressTempDirCleanUp {
/** Point to JIRA entry. */
public String bugUrl() default "None";
}
// these are meant to be accessed sequentially, but are volatile just to ensure any test
// thread will read the latest value
protected static volatile SSLTestConfig sslConfig;
private static boolean LEAVE_TEST_TMP_DIR = Boolean.getBoolean("solr.test.leavetmpdir");
private static boolean LEAVE_TEST_TMP_DIR_ANNOTATION;
@ClassRule
public static TestRule solrClassRules =
@ -166,14 +150,10 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
@BeforeClass
@SuppressWarnings("unused")
private static void beforeClass() {
String cname = getSimpleClassName();
LEAVE_TEST_TMP_DIR_ANNOTATION = RandomizedContext.current().getTargetClass()
.isAnnotationPresent(SuppressTempDirCleanUp.class);
boolean ensureClosed = !(LEAVE_TEST_TMP_DIR || LEAVE_TEST_TMP_DIR_ANNOTATION);
rootTmpDir = TestUtil.createTempDir("solrtest-" + cname, null, ensureClosed);
initCoreDataDir = TestUtil.createTempDir("solrtest-" + cname, rootTmpDir, ensureClosed);
// Create the root parent folder for all other temporary solr files.
rootTmpDir = TestUtil.createTempDir();
initCoreDataDir = TestUtil.createTempDir("init-core-data", rootTmpDir);
System.err.println("Creating dataDir: " + initCoreDataDir.getAbsolutePath());
@ -207,42 +187,19 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
resetFactory();
coreName = ConfigSolrXmlOld.DEFAULT_DEFAULT_CORE_NAME;
} finally {
try {
if (LEAVE_TEST_TMP_DIR) {
System.err
.println("NOTE: per solr.test.leavetmpdir, the test tmp directory will not be removed: "
+ rootTmpDir.getAbsolutePath());
} else {
// TODO: tmp files should already get cleaned up by the test framework, but
// we still do it here as well, so that we clean up as much as we can, even
// when a test is the SuppressTempDirCleanUp annotation
if (rootTmpDir != null && rootTmpDir.exists() && !recurseDelete(rootTmpDir)) {
String msg = "!!!! WARNING: best effort to remove "
+ rootTmpDir.getAbsolutePath() + " FAILED !!!!!";
if (LEAVE_TEST_TMP_DIR_ANNOTATION) {
System.err.println(msg);
} else {
// TODO: do we want to fail here with this message? Is the test framework
// fail that should happen better?
fail(msg);
}
}
}
} finally {
initCoreDataDir = null;
System.clearProperty("jetty.testMode");
System.clearProperty("tests.shardhandler.randomSeed");
System.clearProperty("enable.update.log");
System.clearProperty("useCompoundFile");
System.clearProperty("urlScheme");
if (isSSLMode()) {
HttpClientUtil.setConfigurer(new HttpClientConfigurer());
}
// clean up static
sslConfig = null;
initCoreDataDir = null;
System.clearProperty("jetty.testMode");
System.clearProperty("tests.shardhandler.randomSeed");
System.clearProperty("enable.update.log");
System.clearProperty("useCompoundFile");
System.clearProperty("urlScheme");
if (isSSLMode()) {
HttpClientUtil.setConfigurer(new HttpClientConfigurer());
}
// clean up static
sslConfig = null;
}
IpTables.unblockAllPorts();
@ -511,9 +468,9 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
public static File createTempDir() {
return createTempDir(null);
}
public static File createTempDir(String tag) {
return TestUtil.createTempDir(getClassName() + (tag == null ? "" : "-" + tag), rootTmpDir, !(LEAVE_TEST_TMP_DIR || LEAVE_TEST_TMP_DIR_ANNOTATION));
public static File createTempDir(String prefix) {
return TestUtil.createTempDir(prefix == null ? "temp" : prefix, rootTmpDir);
}
public static void resetExceptionIgnores() {

View File

@ -154,13 +154,11 @@ public abstract class AbstractZkTestCase extends SolrTestCaseJ4 {
System.clearProperty("jetty.port");
System.clearProperty(ZOOKEEPER_FORCE_SYNC);
zkServer.shutdown();
zkServer = null;
if (zkServer != null) {
zkServer.shutdown();
zkServer = null;
}
zkDir = null;
// wait just a bit for any zk client threads to outlast timeout
Thread.sleep(2000);
}
protected void printLayout(String zkHost) throws Exception {