mirror of https://github.com/apache/lucene.git
LUCENE-9110: Backport refactored stack analysis in tests to use generalized LuceneTestCase methods
This commit is contained in:
parent
8f5f18041a
commit
a6a220c89a
|
@ -23,6 +23,9 @@ Improvements
|
|||
* LUCENE-9109: Backport some changes from master (except StackWalker) to improve
|
||||
TestSecurityManager (Uwe Schindler)
|
||||
|
||||
* LUCENE-9110: Backport refactored stack analysis in tests to use generalized
|
||||
LuceneTestCase methods (Uwe Schindler)
|
||||
|
||||
Optimizations
|
||||
---------------------
|
||||
(No changes)
|
||||
|
|
|
@ -89,18 +89,15 @@ public class TestMergeSchedulerExternal extends LuceneTestCase {
|
|||
private static class FailOnlyOnMerge extends MockDirectoryWrapper.Failure {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("doMerge".equals(trace[i].getMethodName())) {
|
||||
IOException ioe = new IOException("now failing during merge");
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
ioe.printStackTrace(pw);
|
||||
if (infoStream.isEnabled("IW")) {
|
||||
infoStream.message("IW", "TEST: now throw exc:\n" + sw.toString());
|
||||
}
|
||||
throw ioe;
|
||||
if (callStackContainsAnyOf("doMerge")) {
|
||||
IOException ioe = new IOException("now failing during merge");
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
ioe.printStackTrace(pw);
|
||||
if (infoStream.isEnabled("IW")) {
|
||||
infoStream.message("IW", "TEST: now throw exc:\n" + sw.toString());
|
||||
}
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,21 +54,7 @@ public class TestConcurrentMergeScheduler extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFail && isTestThread()) {
|
||||
boolean isDoFlush = false;
|
||||
boolean isClose = false;
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (isDoFlush && isClose) {
|
||||
break;
|
||||
}
|
||||
if ("flush".equals(trace[i].getMethodName())) {
|
||||
isDoFlush = true;
|
||||
}
|
||||
if ("close".equals(trace[i].getMethodName())) {
|
||||
isClose = true;
|
||||
}
|
||||
}
|
||||
if (isDoFlush && !isClose && random().nextBoolean()) {
|
||||
if (callStackContainsAnyOf("flush") && false == callStackContainsAnyOf("close") && random().nextBoolean()) {
|
||||
hitExc = true;
|
||||
throw new IOException(Thread.currentThread().getName() + ": now failing during flush");
|
||||
}
|
||||
|
|
|
@ -659,16 +659,13 @@ public class TestDirectoryReaderReopen extends LuceneTestCase {
|
|||
}
|
||||
//System.out.println("failOn: ");
|
||||
//new Throwable().printStackTrace(System.out);
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("readLiveDocs".equals(trace[i].getMethodName())) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
failed = true;
|
||||
throw new FakeIOException();
|
||||
if (callStackContainsAnyOf("readLiveDocs")) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
failed = true;
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -414,12 +414,8 @@ public class TestIndexFileDeleter extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFailExc.get() && random().nextInt(4) == 1) {
|
||||
Exception e = new Exception();
|
||||
StackTraceElement stack[] = e.getStackTrace();
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
if (stack[i].getClassName().equals(IndexFileDeleter.class.getName()) && stack[i].getMethodName().equals("decRef")) {
|
||||
throw new RuntimeException("fake fail");
|
||||
}
|
||||
if (callStackContains(IndexFileDeleter.class, "decRef")) {
|
||||
throw new RuntimeException("fake fail");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -497,12 +493,8 @@ public class TestIndexFileDeleter extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFailExc.get() && random().nextInt(4) == 1) {
|
||||
Exception e = new Exception();
|
||||
StackTraceElement stack[] = e.getStackTrace();
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
if (stack[i].getClassName().equals(MockDirectoryWrapper.class.getName()) && stack[i].getMethodName().equals("deleteFile")) {
|
||||
throw new MockDirectoryWrapper.FakeIOException();
|
||||
}
|
||||
if (callStackContains(MockDirectoryWrapper.class, "deleteFile")) {
|
||||
throw new MockDirectoryWrapper.FakeIOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3048,13 +3048,8 @@ public class TestIndexWriter extends LuceneTestCase {
|
|||
dir.failOn(new MockDirectoryWrapper.Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("flush".equals(trace[i].getMethodName())
|
||||
&& "org.apache.lucene.index.DocumentsWriterPerThread".equals(trace[i].getClassName())) {
|
||||
flushingThreads.add(Thread.currentThread().getName());
|
||||
break;
|
||||
}
|
||||
if (callStackContains(DocumentsWriterPerThread.class, "flush")) {
|
||||
flushingThreads.add(Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -3384,15 +3379,12 @@ public class TestIndexWriter extends LuceneTestCase {
|
|||
try (Directory dir = new FilterDirectory(newDirectory()) {
|
||||
@Override
|
||||
public IndexOutput createOutput(String name, IOContext context) throws IOException {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("flush".equals(trace[i].getMethodName()) && DefaultIndexingChain.class.getName().equals(trace[i].getClassName())) {
|
||||
try {
|
||||
inFlush.countDown();
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
if (callStackContains(DefaultIndexingChain.class, "flush")) {
|
||||
try {
|
||||
inFlush.countDown();
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
return super.createOutput(name, context);
|
||||
|
|
|
@ -731,15 +731,7 @@ public class TestIndexWriterDelete extends LuceneTestCase {
|
|||
}
|
||||
new Throwable().printStackTrace(System.out);
|
||||
if (sawMaybe && !failed) {
|
||||
boolean seen = false;
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("applyDeletesAndUpdates".equals(trace[i].getMethodName()) ||
|
||||
"slowFileExists".equals(trace[i].getMethodName())) {
|
||||
seen = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean seen = callStackContainsAnyOf("applyDeletesAndUpdates", "slowFileExists");
|
||||
if (!seen) {
|
||||
// Only fail once we are no longer in applyDeletes
|
||||
failed = true;
|
||||
|
@ -751,16 +743,12 @@ public class TestIndexWriterDelete extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
if (!failed) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("applyDeletesAndUpdates".equals(trace[i].getMethodName())) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: mock failure: saw applyDeletes");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
sawMaybe = true;
|
||||
break;
|
||||
if (callStackContainsAnyOf("applyDeletesAndUpdates")) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: mock failure: saw applyDeletes");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
sawMaybe = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -566,19 +566,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFail) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
boolean sawFlush = false;
|
||||
boolean sawFinishDocument = false;
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("flush".equals(trace[i].getMethodName())) {
|
||||
sawFlush = true;
|
||||
}
|
||||
if ("finishDocument".equals(trace[i].getMethodName())) {
|
||||
sawFinishDocument = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (sawFlush && sawFinishDocument == false && count++ >= 30) {
|
||||
if (callStackContainsAnyOf("flush") && false == callStackContainsAnyOf("finishDocument") && count++ >= 30) {
|
||||
doFail = false;
|
||||
throw new IOException("now failing during flush");
|
||||
}
|
||||
|
@ -871,16 +859,14 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFail) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (doFail && MockDirectoryWrapper.class.getName().equals(trace[i].getClassName()) && "sync".equals(trace[i].getMethodName())) {
|
||||
didFail = true;
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now throw exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
throw new IOException("now failing on purpose during sync");
|
||||
if (callStackContains(MockDirectoryWrapper.class, "sync")) {
|
||||
didFail = true;
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now throw exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
throw new IOException("now failing on purpose during sync");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -948,28 +934,10 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
boolean isCommit = false;
|
||||
boolean isDelete = false;
|
||||
boolean isSyncMetadata = false;
|
||||
boolean isInGlobalFieldMap = false;
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (isCommit && isDelete && isInGlobalFieldMap && isSyncMetadata) {
|
||||
break;
|
||||
}
|
||||
if (SegmentInfos.class.getName().equals(trace[i].getClassName()) && stage.equals(trace[i].getMethodName())) {
|
||||
isCommit = true;
|
||||
}
|
||||
if (MockDirectoryWrapper.class.getName().equals(trace[i].getClassName()) && "deleteFile".equals(trace[i].getMethodName())) {
|
||||
isDelete = true;
|
||||
}
|
||||
if (SegmentInfos.class.getName().equals(trace[i].getClassName()) && "writeGlobalFieldMap".equals(trace[i].getMethodName())) {
|
||||
isInGlobalFieldMap = true;
|
||||
}
|
||||
if (MockDirectoryWrapper.class.getName().equals(trace[i].getClassName()) && "syncMetaData".equals(trace[i].getMethodName())) {
|
||||
isSyncMetadata = true;
|
||||
}
|
||||
}
|
||||
boolean isCommit = callStackContains(SegmentInfos.class, stage);
|
||||
boolean isDelete = callStackContains(MockDirectoryWrapper.class, "deleteFile");
|
||||
boolean isSyncMetadata = callStackContains(MockDirectoryWrapper.class, "syncMetaData");
|
||||
boolean isInGlobalFieldMap = callStackContains(SegmentInfos.class, "writeGlobalFieldMap");
|
||||
if (isInGlobalFieldMap && dontFailDuringGlobalFieldMap) {
|
||||
isCommit = false;
|
||||
}
|
||||
|
@ -1383,17 +1351,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
boolean fail = false;
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (TermVectorsConsumer.class.getName().equals(trace[i].getClassName()) && stage.equals(trace[i].getMethodName())) {
|
||||
fail = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
if (callStackContains(TermVectorsConsumer.class, stage)) {
|
||||
throw new RuntimeException(EXC_MSG);
|
||||
}
|
||||
}
|
||||
|
@ -1719,11 +1677,8 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
@Override
|
||||
public IndexInput openInput(String name, IOContext context) throws IOException {
|
||||
if (doFail && name.startsWith("segments_")) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("readCommit".equals(trace[i].getMethodName()) || "readLatestCommit".equals(trace[i].getMethodName())) {
|
||||
throw new UnsupportedOperationException("expected UOE");
|
||||
}
|
||||
if (callStackContainsAnyOf("readCommit", "readLatestCommit")) {
|
||||
throw new UnsupportedOperationException("expected UOE");
|
||||
}
|
||||
}
|
||||
return super.openInput(name, context);
|
||||
|
@ -1939,17 +1894,7 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
if (random().nextInt(10) != 0) {
|
||||
return;
|
||||
}
|
||||
boolean maybeFail = false;
|
||||
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
||||
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("rollbackInternal".equals(trace[i].getMethodName())) {
|
||||
maybeFail = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (maybeFail) {
|
||||
if (callStackContainsAnyOf("rollbackInternal")) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; thread=" + Thread.currentThread().getName() + " exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
|
@ -2012,17 +1957,14 @@ public class TestIndexWriterExceptions extends LuceneTestCase {
|
|||
// Already failed
|
||||
return;
|
||||
}
|
||||
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
||||
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("merge".equals(trace[i].getMethodName())) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; thread=" + Thread.currentThread().getName() + " exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
didFail.set(true);
|
||||
throw new FakeIOException();
|
||||
|
||||
if (callStackContainsAnyOf("merge")) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; thread=" + Thread.currentThread().getName() + " exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
didFail.set(true);
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.apache.lucene.index;
|
|||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
import org.apache.lucene.codecs.LiveDocsFormat;
|
||||
import org.apache.lucene.document.Document;
|
||||
|
@ -478,16 +477,13 @@ public class TestIndexWriterOnDiskFull extends LuceneTestCase {
|
|||
if (!doFail) {
|
||||
return;
|
||||
}
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (SegmentMerger.class.getName().equals(trace[i].getClassName()) && "mergeTerms".equals(trace[i].getMethodName()) && !didFail1) {
|
||||
didFail1 = true;
|
||||
throw new IOException("fake disk full during mergeTerms");
|
||||
}
|
||||
if (LiveDocsFormat.class.getName().equals(trace[i].getClassName()) && "writeLiveDocs".equals(trace[i].getMethodName()) && !didFail2) {
|
||||
didFail2 = true;
|
||||
throw new IOException("fake disk full while writing LiveDocs");
|
||||
}
|
||||
if (callStackContains(SegmentMerger.class, "mergeTerms") && !didFail1) {
|
||||
didFail1 = true;
|
||||
throw new IOException("fake disk full during mergeTerms");
|
||||
}
|
||||
if (callStackContains(LiveDocsFormat.class, "writeLiveDocs") && !didFail2) {
|
||||
didFail2 = true;
|
||||
throw new IOException("fake disk full while writing LiveDocs");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -238,14 +238,7 @@ public class TestIndexWriterOnVMError extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (r.nextInt(3000) == 0) {
|
||||
StackTraceElement stack[] = Thread.currentThread().getStackTrace();
|
||||
boolean ok = false;
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
if (stack[i].getClassName().equals(IndexWriter.class.getName())) {
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
if (callStackContains(IndexWriter.class)) {
|
||||
throw new OutOfMemoryError("Fake OutOfMemoryError");
|
||||
}
|
||||
}
|
||||
|
@ -259,14 +252,7 @@ public class TestIndexWriterOnVMError extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (r.nextInt(3000) == 0) {
|
||||
StackTraceElement stack[] = Thread.currentThread().getStackTrace();
|
||||
boolean ok = false;
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
if (stack[i].getClassName().equals(IndexWriter.class.getName())) {
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
if (callStackContains(IndexWriter.class)) {
|
||||
throw new UnknownError("Fake UnknownError");
|
||||
}
|
||||
}
|
||||
|
@ -281,16 +267,11 @@ public class TestIndexWriterOnVMError extends LuceneTestCase {
|
|||
doTest(new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement stack[] = Thread.currentThread().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 (r.nextInt(4) == 0) {
|
||||
if (callStackContains(IndexFileDeleter.class, "checkpoint")) {
|
||||
throw new OutOfMemoryError("Fake OutOfMemoryError");
|
||||
}
|
||||
}
|
||||
if (ok && r.nextInt(4) == 0) {
|
||||
throw new OutOfMemoryError("Fake OutOfMemoryError");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1067,17 +1067,14 @@ public class TestIndexWriterReader extends LuceneTestCase {
|
|||
dir.failOn(new MockDirectoryWrapper.Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
if (shouldFail.get()) {
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("getReadOnlyClone".equals(trace[i].getMethodName())) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
shouldFail.set(false);
|
||||
throw new FakeIOException();
|
||||
if (callStackContainsAnyOf("getReadOnlyClone")) {
|
||||
if (VERBOSE) {
|
||||
System.out.println("TEST: now fail; exc:");
|
||||
new Throwable().printStackTrace(System.out);
|
||||
}
|
||||
shouldFail.set(false);
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -407,26 +407,7 @@ public class TestIndexWriterWithThreads extends LuceneTestCase {
|
|||
dir.setAssertNoUnrefencedFilesOnClose(false);
|
||||
|
||||
if (doFail) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
boolean sawAbortOrFlushDoc = false;
|
||||
boolean sawClose = false;
|
||||
boolean sawMerge = false;
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (sawAbortOrFlushDoc && sawMerge && sawClose) {
|
||||
break;
|
||||
}
|
||||
if ("abort".equals(trace[i].getMethodName()) ||
|
||||
"finishDocument".equals(trace[i].getMethodName())) {
|
||||
sawAbortOrFlushDoc = true;
|
||||
}
|
||||
if ("merge".equals(trace[i].getMethodName())) {
|
||||
sawMerge = true;
|
||||
}
|
||||
if ("close".equals(trace[i].getMethodName())) {
|
||||
sawClose = true;
|
||||
}
|
||||
}
|
||||
if (sawAbortOrFlushDoc && !sawClose && !sawMerge) {
|
||||
if (callStackContainsAnyOf("abort", "finishDocument") && false == callStackContainsAnyOf("merge", "close")) {
|
||||
if (onlyOnce) {
|
||||
doFail = false;
|
||||
}
|
||||
|
@ -473,15 +454,12 @@ public class TestIndexWriterWithThreads extends LuceneTestCase {
|
|||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
if (doFail) {
|
||||
StackTraceElement[] trace = new Exception().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if ("flush".equals(trace[i].getMethodName()) && DefaultIndexingChain.class.getName().equals(trace[i].getClassName())) {
|
||||
if (onlyOnce)
|
||||
doFail = false;
|
||||
//System.out.println(Thread.currentThread().getName() + ": NOW FAIL: onlyOnce=" + onlyOnce);
|
||||
//new Throwable().printStackTrace(System.out);
|
||||
throw new IOException("now failing on purpose");
|
||||
}
|
||||
if (callStackContains(DefaultIndexingChain.class, "flush")) {
|
||||
if (onlyOnce)
|
||||
doFail = false;
|
||||
//System.out.println(Thread.currentThread().getName() + ": NOW FAIL: onlyOnce=" + onlyOnce);
|
||||
//new Throwable().printStackTrace(System.out);
|
||||
throw new IOException("now failing on purpose");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,11 +116,8 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPo
|
|||
dir.failOn(new MockDirectoryWrapper.Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
|
||||
for (int i = 0; i < trace.length; i++) {
|
||||
if (PersistentSnapshotDeletionPolicy.class.getName().equals(trace[i].getClassName()) && "persist".equals(trace[i].getMethodName())) {
|
||||
throw new IOException("now fail on purpose");
|
||||
}
|
||||
if (callStackContains(PersistentSnapshotDeletionPolicy.class, "persist")) {
|
||||
throw new IOException("now fail on purpose");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -133,8 +133,8 @@ public class TestUnifiedHighlighterTermVec extends LuceneTestCase {
|
|||
@Override
|
||||
public Fields getTermVectors(int docID) throws IOException {
|
||||
// if we're invoked by ParallelLeafReader then we can't do our assertion. TODO see LUCENE-6868
|
||||
if (calledBy(ParallelLeafReader.class) == false
|
||||
&& calledBy(CheckIndex.class) == false) {
|
||||
if (callStackContains(ParallelLeafReader.class) == false
|
||||
&& callStackContains(CheckIndex.class) == false) {
|
||||
assertFalse("Should not request TVs for doc more than once.", seenDocIDs.get(docID));
|
||||
seenDocIDs.set(docID);
|
||||
}
|
||||
|
@ -170,14 +170,6 @@ public class TestUnifiedHighlighterTermVec extends LuceneTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private static boolean calledBy(Class<?> clazz) {
|
||||
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
|
||||
if (stackTraceElement.getClassName().equals(clazz.getName()))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testUserFailedToIndexOffsets() throws IOException {
|
||||
FieldType fieldType = new FieldType(UHTestHelper.tvType); // note: it's indexed too
|
||||
|
|
|
@ -102,10 +102,8 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTes
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "createOutput".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("createOutput")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -137,10 +135,8 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTes
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "close".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("close")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -172,10 +168,8 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTes
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "openInput".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("openInput")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -208,10 +202,8 @@ public abstract class BaseFieldInfoFormatTestCase extends BaseIndexFileFormatTes
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "close".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("close")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -292,10 +292,8 @@ public abstract class BaseSegmentInfoFormatTestCase extends BaseIndexFileFormatT
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "createOutput".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("createOutput")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -325,10 +323,8 @@ public abstract class BaseSegmentInfoFormatTestCase extends BaseIndexFileFormatT
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "close".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("close")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -358,10 +354,8 @@ public abstract class BaseSegmentInfoFormatTestCase extends BaseIndexFileFormatT
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "openInput".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("openInput")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -392,10 +386,8 @@ public abstract class BaseSegmentInfoFormatTestCase extends BaseIndexFileFormatT
|
|||
Failure fail = new Failure() {
|
||||
@Override
|
||||
public void eval(MockDirectoryWrapper dir) throws IOException {
|
||||
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
|
||||
if (doFail && "close".equals(e.getMethodName())) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
if (doFail && callStackContainsAnyOf("close")) {
|
||||
throw new FakeIOException();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -66,6 +66,7 @@ import java.util.concurrent.TimeUnit;
|
|||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.MockAnalyzer;
|
||||
|
@ -2700,6 +2701,27 @@ public abstract class LuceneTestCase extends Assert {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Inspects stack trace to figure out if a method of a specific class called us. */
|
||||
public static boolean callStackContains(Class<?> clazz, String methodName) {
|
||||
final String className = clazz.getName();
|
||||
return Stream.of(new Exception().getStackTrace()).skip(1) // exclude this utility method
|
||||
.anyMatch(f -> className.equals(f.getClassName()) && methodName.equals(f.getMethodName()));
|
||||
}
|
||||
|
||||
/** Inspects stack trace to figure out if one of the given method names (no class restriction) called us. */
|
||||
public static boolean callStackContainsAnyOf(String... methodNames) {
|
||||
return Stream.of(new Exception().getStackTrace()).skip(1) // exclude this utility method
|
||||
.map(StackTraceElement::getMethodName)
|
||||
.anyMatch(Arrays.asList(methodNames)::contains);
|
||||
}
|
||||
|
||||
/** Inspects stack trace if the given class called us. */
|
||||
public static boolean callStackContains(Class<?> clazz) {
|
||||
return Stream.of(new Exception().getStackTrace()).skip(1) // exclude this utility method
|
||||
.map(StackTraceElement::getClassName)
|
||||
.anyMatch(clazz.getName()::equals);
|
||||
}
|
||||
|
||||
/** A runnable that can throw any checked exception. */
|
||||
@FunctionalInterface
|
||||
|
|
Loading…
Reference in New Issue