Apache Solr open-source search software
LUCENE-2274: Add support for uncaught exceptions in threads
Uwe Schindler9c8b13a567da
diff --git a/src/test/org/apache/lucene/index/TestNRTReaderWithThreads.java b/src/test/org/apache/lucene/index/TestNRTReaderWithThreads.javaindex 834037211ad..46b634cb912 100644--- a/src/test/org/apache/lucene/index/TestNRTReaderWithThreads.java+++ b/src/test/org/apache/lucene/index/TestNRTReaderWithThreads.java@@ -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;diff --git a/src/test/org/apache/lucene/util/LuceneTestCase.java b/src/test/org/apache/lucene/util/LuceneTestCase.javaindex 391f5e665e6..8dd0dc4b445 100644--- a/src/test/org/apache/lucene/util/LuceneTestCase.java+++ b/src/test/org/apache/lucene/util/LuceneTestCase.java@@ -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 {@Overrideprotected 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();}diff --git a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.javaindex bea26632ee3..4ef59b37e88 100644--- a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java+++ b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java@@ -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 {@Beforepublic 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!");+ }}/**