Skip to content

Apache Lucene open-source search software

LUCENE-3374: sometimes turn on NRTCachingDirectory in tests

Robert Muir5421461abae5
diff --git a/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
index a5d780cf83b..6cf75ddda4f 100644
--- a/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
@@ -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);
}
diff --git a/lucene/src/test/org/apache/lucene/index/TestCrash.java b/lucene/src/test/org/apache/lucene/index/TestCrash.java
index b2fa7eb54f7..91340f85672 100644
--- a/lucene/src/test/org/apache/lucene/index/TestCrash.java
+++ b/lucene/src/test/org/apache/lucene/index/TestCrash.java
@@ -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 {
diff --git a/lucene/src/test/org/apache/lucene/index/TestDoc.java b/lucene/src/test/org/apache/lucene/index/TestDoc.java
index 5d14917bd77..1c9af886b52 100644
--- a/lucene/src/test/org/apache/lucene/index/TestDoc.java
+++ b/lucene/src/test/org/apache/lucene/index/TestDoc.java
@@ -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)).