changeset 11738:aa10794f1574

better reporting of failures during multi-threaded tests
author Doug Simon <doug.simon@oracle.com>
date Fri, 20 Sep 2013 21:14:24 +0200
parents 2c590fb9d695
children b8695542d41b
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java
diffstat 1 files changed, 42 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java	Fri Sep 20 13:20:49 2013 -0400
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/GraalCompilerTest.java	Fri Sep 20 21:14:24 2013 +0200
@@ -25,6 +25,7 @@
 import static com.oracle.graal.api.code.CodeUtil.*;
 import static com.oracle.graal.phases.GraalOptions.*;
 
+import java.io.*;
 import java.lang.reflect.*;
 import java.util.*;
 import java.util.concurrent.*;
@@ -206,11 +207,45 @@
         }
     }
 
+    @SuppressWarnings("serial")
+    public static class MultiCauseAssertionError extends AssertionError {
+
+        private Throwable[] causes;
+
+        public MultiCauseAssertionError(String message, Throwable... causes) {
+            super(message);
+            this.causes = causes;
+        }
+
+        @Override
+        public void printStackTrace(PrintStream out) {
+            super.printStackTrace(out);
+            int num = 0;
+            for (Throwable cause : causes) {
+                if (cause != null) {
+                    out.print("cause " + (num++));
+                    cause.printStackTrace(out);
+                }
+            }
+        }
+
+        @Override
+        public void printStackTrace(PrintWriter out) {
+            super.printStackTrace(out);
+            int num = 0;
+            for (Throwable cause : causes) {
+                if (cause != null) {
+                    out.print("cause " + (num++) + ": ");
+                    cause.printStackTrace(out);
+                }
+            }
+        }
+    }
+
     protected void testN(int n, final String name, final Object... args) {
-        final Throwable[] errors = new Throwable[n];
+        final List<Throwable> errors = new ArrayList<>(n);
         Thread[] threads = new Thread[n];
         for (int i = 0; i < n; i++) {
-            final int idx = i;
             Thread t = new Thread(i + ":" + name) {
 
                 @Override
@@ -218,26 +253,23 @@
                     try {
                         test(name, args);
                     } catch (Throwable e) {
-                        errors[idx] = e;
+                        errors.add(e);
                     }
                 }
             };
             threads[i] = t;
             t.start();
         }
-        int failed = 0;
         for (int i = 0; i < n; i++) {
             try {
                 threads[i].join();
             } catch (InterruptedException e) {
-                errors[i] = e;
-            }
-            if (errors[i] != null) {
-                errors[i].printStackTrace();
-                failed++;
+                errors.add(e);
             }
         }
-        Assert.assertTrue(failed + " of " + n + " failed", failed == 0);
+        if (!errors.isEmpty()) {
+            throw new MultiCauseAssertionError(errors.size() + " failures", errors.toArray(new Throwable[errors.size()]));
+        }
     }
 
     protected Object referenceInvoke(Method method, Object receiver, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {