changeset 11344:3b999faed2da

8072030: Race condition in ThenComposeExceptionTest.java Reviewed-by: chegar
author psandoz
date Mon, 02 Feb 2015 14:21:32 +0100
parents ed38ff66f7dd
children 330dcd651f3b
files test/java/util/concurrent/CompletableFuture/ThenComposeExceptionTest.java
diffstat 1 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/concurrent/CompletableFuture/ThenComposeExceptionTest.java	Mon Feb 02 14:19:12 2015 +0100
+++ b/test/java/util/concurrent/CompletableFuture/ThenComposeExceptionTest.java	Mon Feb 02 14:21:32 2015 +0100
@@ -36,7 +36,7 @@
 
 /**
  * @test
- * @bug 8068432
+ * @bug 8068432 8072030
  * @run testng ThenComposeExceptionTest
  * @summary Test that CompletableFuture.thenCompose works correctly if the
  * composing future completes exceptionally
@@ -92,7 +92,8 @@
         Assert.assertNotSame(f_thenCompose, fe, "Composed CompletableFuture returned directly");
 
         AtomicReference<Throwable> eOnWhenComplete = new AtomicReference<>();
-        f_thenCompose.whenComplete((r, e) -> eOnWhenComplete.set(e));
+        CompletableFuture<String> f_whenComplete = f_thenCompose.
+                whenComplete((r, e) -> eOnWhenComplete.set(e));
 
         afterAction.accept(fe);
 
@@ -103,10 +104,20 @@
         catch (Throwable t) {
             eOnJoined = t;
         }
+        Assert.assertTrue(eOnJoined instanceof CompletionException,
+                          "Incorrect exception reported when joined on thenCompose: " + eOnJoined);
 
+        // Need to wait for f_whenComplete to complete to avoid
+        // race condition when updating eOnWhenComplete
+        eOnJoined = null;
+        try {
+            f_whenComplete.join();
+        } catch (Throwable t) {
+            eOnJoined = t;
+        }
+        Assert.assertTrue(eOnJoined instanceof CompletionException,
+                          "Incorrect exception reported when joined on whenComplete: " + eOnJoined);
         Assert.assertTrue(eOnWhenComplete.get() instanceof CompletionException,
-                          "Incorrect exception reported on whenComplete");
-        Assert.assertTrue(eOnJoined instanceof CompletionException,
-                          "Incorrect exception reported when joined");
+                          "Incorrect exception passed to whenComplete: " + eOnWhenComplete.get());
     }
-}
+}
\ No newline at end of file