mirror of
https://github.com/apache/lucene.git
synced 2025-03-04 07:19:18 +00:00
LUCENE-2274: Add support for uncaught exceptions in threads
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@912372 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
dd3960595f
commit
9c8b13a567
@ -54,13 +54,16 @@ public class TestNRTReaderWithThreads extends LuceneTestCase {
|
||||
int addCount = 0;
|
||||
for (int x=0; x < indexThreads.length; x++) {
|
||||
indexThreads[x].run = false;
|
||||
assertTrue(indexThreads[x].ex == null);
|
||||
assertNull("Exception thrown: "+indexThreads[x].ex, indexThreads[x].ex);
|
||||
addCount += indexThreads[x].addCount;
|
||||
delCount += indexThreads[x].delCount;
|
||||
}
|
||||
for (int x=0; x < indexThreads.length; x++) {
|
||||
indexThreads[x].join();
|
||||
}
|
||||
for (int x=0; x < indexThreads.length; x++) {
|
||||
assertNull("Exception thrown: "+indexThreads[x].ex, indexThreads[x].ex);
|
||||
}
|
||||
//System.out.println("addCount:"+addCount);
|
||||
//System.out.println("delCount:"+delCount);
|
||||
writer.close();
|
||||
@ -69,8 +72,8 @@ public class TestNRTReaderWithThreads extends LuceneTestCase {
|
||||
|
||||
public class RunThread extends Thread {
|
||||
IndexWriter writer;
|
||||
boolean run = true;
|
||||
Throwable ex;
|
||||
volatile boolean run = true;
|
||||
volatile Throwable ex;
|
||||
int delCount = 0;
|
||||
int addCount = 0;
|
||||
int type;
|
||||
|
@ -21,6 +21,9 @@ import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@ -57,6 +60,19 @@ public abstract class LuceneTestCase extends TestCase {
|
||||
public static final Version TEST_VERSION_CURRENT = LuceneTestCaseJ4.TEST_VERSION_CURRENT;
|
||||
|
||||
private int savedBoolMaxClauseCount;
|
||||
|
||||
private volatile Thread.UncaughtExceptionHandler savedUncaughtExceptionHandler = null;
|
||||
|
||||
private static class UncaughtExceptionEntry {
|
||||
public final Thread thread;
|
||||
public final Throwable exception;
|
||||
|
||||
public UncaughtExceptionEntry(Thread thread, Throwable exception) {
|
||||
this.thread = thread;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
||||
private List<UncaughtExceptionEntry> uncaughtExceptions = Collections.synchronizedList(new ArrayList<UncaughtExceptionEntry>());
|
||||
|
||||
public LuceneTestCase() {
|
||||
super();
|
||||
@ -69,6 +85,16 @@ public abstract class LuceneTestCase extends TestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
savedUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
uncaughtExceptions.add(new UncaughtExceptionEntry(t, e));
|
||||
if (savedUncaughtExceptionHandler != null)
|
||||
savedUncaughtExceptionHandler.uncaughtException(t, e);
|
||||
}
|
||||
});
|
||||
|
||||
ConcurrentMergeScheduler.setTestMode();
|
||||
savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount();
|
||||
}
|
||||
@ -111,6 +137,16 @@ public abstract class LuceneTestCase extends TestCase {
|
||||
purgeFieldCache(FieldCache.DEFAULT);
|
||||
}
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(savedUncaughtExceptionHandler);
|
||||
if (!uncaughtExceptions.isEmpty()) {
|
||||
System.err.println("The following exceptions were thrown by threads:");
|
||||
for (UncaughtExceptionEntry entry : uncaughtExceptions) {
|
||||
System.err.println("*** Thread: " + entry.thread.getName() + " ***");
|
||||
entry.exception.printStackTrace(System.err);
|
||||
}
|
||||
fail("Some threads throwed uncaught exceptions!");
|
||||
}
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,9 @@ import java.io.PrintStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
@ -79,6 +82,19 @@ public class LuceneTestCaseJ4 extends TestWatchman {
|
||||
|
||||
private int savedBoolMaxClauseCount;
|
||||
|
||||
private volatile Thread.UncaughtExceptionHandler savedUncaughtExceptionHandler = null;
|
||||
|
||||
private static class UncaughtExceptionEntry {
|
||||
public final Thread thread;
|
||||
public final Throwable exception;
|
||||
|
||||
public UncaughtExceptionEntry(Thread thread, Throwable exception) {
|
||||
this.thread = thread;
|
||||
this.exception = exception;
|
||||
}
|
||||
}
|
||||
private List<UncaughtExceptionEntry> uncaughtExceptions = Collections.synchronizedList(new ArrayList<UncaughtExceptionEntry>());
|
||||
|
||||
// This is how we get control when errors occur.
|
||||
// Think of this as start/end/success/failed
|
||||
// events.
|
||||
@ -94,6 +110,15 @@ public class LuceneTestCaseJ4 extends TestWatchman {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
savedUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
uncaughtExceptions.add(new UncaughtExceptionEntry(t, e));
|
||||
if (savedUncaughtExceptionHandler != null)
|
||||
savedUncaughtExceptionHandler.uncaughtException(t, e);
|
||||
}
|
||||
});
|
||||
|
||||
ConcurrentMergeScheduler.setTestMode();
|
||||
savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount();
|
||||
seed = null;
|
||||
@ -138,6 +163,16 @@ public class LuceneTestCaseJ4 extends TestWatchman {
|
||||
} finally {
|
||||
purgeFieldCache(FieldCache.DEFAULT);
|
||||
}
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(savedUncaughtExceptionHandler);
|
||||
if (!uncaughtExceptions.isEmpty()) {
|
||||
System.err.println("The following exceptions were thrown by threads:");
|
||||
for (UncaughtExceptionEntry entry : uncaughtExceptions) {
|
||||
System.err.println("*** Thread: " + entry.thread.getName() + " ***");
|
||||
entry.exception.printStackTrace(System.err);
|
||||
}
|
||||
fail("Some threads throwed uncaught exceptions!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user