LUCENE-5958: add more efficient test

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1625538 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-09-17 12:06:45 +00:00
parent c004edbb7a
commit 1c6749d907
1 changed files with 44 additions and 22 deletions

View File

@ -44,6 +44,7 @@ import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.Rethrow; import org.apache.lucene.util.Rethrow;
import org.junit.Ignore;
/** /**
* Causes a bunch of fake OOM and checks that no other exceptions are delivered instead, * Causes a bunch of fake OOM and checks that no other exceptions are delivered instead,
@ -52,7 +53,7 @@ import org.apache.lucene.util.Rethrow;
public class TestIndexWriterOutOfMemory extends LuceneTestCase { public class TestIndexWriterOutOfMemory extends LuceneTestCase {
// just one thread, serial merge policy, hopefully debuggable // just one thread, serial merge policy, hopefully debuggable
public void testBasics() throws Exception { private void doTest(MockDirectoryWrapper.Failure failOn) throws Exception {
// log all exceptions we hit, in case we fail (for debugging) // log all exceptions we hit, in case we fail (for debugging)
ByteArrayOutputStream exceptionLog = new ByteArrayOutputStream(); ByteArrayOutputStream exceptionLog = new ByteArrayOutputStream();
PrintStream exceptionStream = new PrintStream(exceptionLog, true, "UTF-8"); PrintStream exceptionStream = new PrintStream(exceptionLog, true, "UTF-8");
@ -99,27 +100,7 @@ public class TestIndexWriterOutOfMemory extends LuceneTestCase {
IndexWriter iw = new IndexWriter(dir, conf); IndexWriter iw = new IndexWriter(dir, conf);
iw.commit(); // ensure there is always a commit iw.commit(); // ensure there is always a commit
final Random r = new Random(random().nextLong()); dir.failOn(failOn);
dir.failOn(new Failure() {
@Override
public void eval(MockDirectoryWrapper dir) throws IOException {
Exception e = new Exception();
StackTraceElement stack[] = e.getStackTrace();
boolean ok = false;
for (int i = 0; i < stack.length; i++) {
if (stack[i].getClassName().equals(IndexWriter.class.getName())) {
ok = true;
// don't make life difficult though
if (stack[i].getMethodName().equals("rollback")) {
return;
}
}
}
if (ok && r.nextInt(3000) == 0) {
throw new OutOfMemoryError("Fake OutOfMemoryError");
}
}
});
for (int i = 0; i < numDocs; i++) { for (int i = 0; i < numDocs; i++) {
Document doc = new Document(); Document doc = new Document();
@ -256,4 +237,45 @@ public class TestIndexWriterOutOfMemory extends LuceneTestCase {
System.out.println(exceptionLog.toString("UTF-8")); System.out.println(exceptionLog.toString("UTF-8"));
} }
} }
public void testBasics() throws Exception {
final Random r = new Random(random().nextLong());
doTest(new Failure() {
@Override
public void eval(MockDirectoryWrapper dir) throws IOException {
Exception e = new Exception();
StackTraceElement stack[] = e.getStackTrace();
boolean ok = false;
for (int i = 0; i < stack.length; i++) {
if (stack[i].getClassName().equals(IndexWriter.class.getName())) {
ok = true;
}
}
if (ok && r.nextInt(3000) == 0) {
throw new OutOfMemoryError("Fake OutOfMemoryError");
}
}
});
}
@Ignore("LUCENE-5958: not yet")
public void testCheckpoint() throws Exception {
final Random r = new Random(random().nextLong());
doTest(new Failure() {
@Override
public void eval(MockDirectoryWrapper dir) throws IOException {
Exception e = new Exception();
StackTraceElement stack[] = e.getStackTrace();
boolean ok = false;
for (int i = 0; i < stack.length; i++) {
if (stack[i].getClassName().equals(IndexFileDeleter.class.getName()) && stack[i].getMethodName().equals("checkpoint")) {
ok = true;
}
}
if (ok && r.nextInt(4) == 0) {
throw new OutOfMemoryError("Fake OutOfMemoryError");
}
}
});
}
} }