LUCENE-3374: sometimes turn on NRTCachingDirectory in tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1215018 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2011-12-16 01:52:41 +00:00
parent b3d2c99128
commit 5421461aba
3 changed files with 37 additions and 8 deletions

View File

@ -58,6 +58,7 @@ import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.MergeInfo;
import org.apache.lucene.store.MockDirectoryWrapper;
import org.apache.lucene.store.MockDirectoryWrapper.Throttling;
import org.apache.lucene.store.NRTCachingDirectory;
import org.apache.lucene.util.FieldCacheSanityChecker.Insanity;
import org.junit.*;
import org.junit.rules.MethodRule;
@ -1006,8 +1007,18 @@ public abstract class LuceneTestCase extends Assert {
* See {@link #newDirectory()} for more information.
*/
public static MockDirectoryWrapper newDirectory(Random r) throws IOException {
return newDirectory(r, true);
}
/**
* Returns a new Directory instance, using the specified random. You
* can specify maybeWrap as to whether the directory might be also
* wrapped by NRTCachingDirectory or FileSwitchDirectory
* See {@link #newDirectory()} for more information.
*/
public static MockDirectoryWrapper newDirectory(Random r, boolean maybeWrap) throws IOException {
Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);
MockDirectoryWrapper dir = new MockDirectoryWrapper(r, impl);
MockDirectoryWrapper dir = new MockDirectoryWrapper(r, maybeWrap ? maybeNRTWrap(r, impl) : impl);
stores.put(dir, Thread.currentThread().getStackTrace());
dir.setThrottling(TEST_THROTTLING);
return dir;
@ -1019,7 +1030,7 @@ public abstract class LuceneTestCase extends Assert {
* information.
*/
public static MockDirectoryWrapper newDirectory(Directory d) throws IOException {
return newDirectory(random, d);
return newDirectory(random, d, true);
}
/** Returns a new FSDirectory instance over the given file, which must be a folder. */
@ -1029,6 +1040,11 @@ public abstract class LuceneTestCase extends Assert {
/** Returns a new FSDirectory instance over the given file, which must be a folder. */
public static MockDirectoryWrapper newFSDirectory(File f, LockFactory lf) throws IOException {
return newFSDirectory(f, lf, true);
}
/** Returns a new FSDirectory instance over the given file, which must be a folder. */
public static MockDirectoryWrapper newFSDirectory(File f, LockFactory lf, boolean maybeWrap) throws IOException {
String fsdirClass = TEST_DIRECTORY;
if (fsdirClass.equals("random")) {
fsdirClass = FS_DIRECTORIES[random.nextInt(FS_DIRECTORIES.length)];
@ -1044,7 +1060,8 @@ public abstract class LuceneTestCase extends Assert {
clazz = CommandLineUtil.loadFSDirectoryClass(fsdirClass);
}
MockDirectoryWrapper dir = new MockDirectoryWrapper(random, newFSDirectoryImpl(clazz, f));
Directory fsdir = newFSDirectoryImpl(clazz, f);
MockDirectoryWrapper dir = new MockDirectoryWrapper(random, maybeWrap ? maybeNRTWrap(random, fsdir) : fsdir);
if (lf != null) {
dir.setLockFactory(lf);
}
@ -1061,17 +1078,25 @@ public abstract class LuceneTestCase extends Assert {
* with contents copied from the provided directory. See
* {@link #newDirectory()} for more information.
*/
public static MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {
public static MockDirectoryWrapper newDirectory(Random r, Directory d, boolean maybeWrap) throws IOException {
Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);
for (String file : d.listAll()) {
d.copy(impl, file, file, newIOContext(r));
}
MockDirectoryWrapper dir = new MockDirectoryWrapper(r, impl);
MockDirectoryWrapper dir = new MockDirectoryWrapper(r, maybeWrap ? maybeNRTWrap(r, impl) : impl);
stores.put(dir, Thread.currentThread().getStackTrace());
dir.setThrottling(TEST_THROTTLING);
return dir;
}
private static Directory maybeNRTWrap(Random random, Directory directory) {
if (rarely(random)) {
return new NRTCachingDirectory(directory, random.nextDouble(), random.nextDouble());
} else {
return directory;
}
}
public static Field newField(String name, String value, FieldType type) {
return newField(random, name, value, type);
}

View File

@ -30,7 +30,9 @@ import org.apache.lucene.document.TextField;
public class TestCrash extends LuceneTestCase {
private IndexWriter initIndex(Random random, boolean initialCommit) throws IOException {
return initIndex(random, newDirectory(), initialCommit);
// note: we pass 'false' here so our crashing/deleting won't trigger assertions in NRTCachingDir
// TODO: don't remember why this is ok... maybe we should check again that it really actually is.
return initIndex(random, newDirectory(random, false), initialCommit);
}
private IndexWriter initIndex(Random random, MockDirectoryWrapper dir, boolean initialCommit) throws IOException {

View File

@ -114,7 +114,8 @@ public class TestDoc extends LuceneTestCase {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw, true);
Directory directory = newFSDirectory(indexDir);
// TODO: why does this test trigger NRTCachingDirectory's assert?
Directory directory = newFSDirectory(indexDir, null, false);
IndexWriter writer = new IndexWriter(
directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
@ -148,7 +149,8 @@ public class TestDoc extends LuceneTestCase {
sw = new StringWriter();
out = new PrintWriter(sw, true);
directory = newFSDirectory(indexDir);
// TODO: why does this test trigger NRTCachingDirectory's assert?
directory = newFSDirectory(indexDir, null, false);
writer = new IndexWriter(
directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).