author | martin |
Wed, 02 Sep 2015 14:11:50 -0700 | |
changeset 32427 | c22b7e41adf3 |
parent 28755 | 204aa673becb |
child 32987 | e5e5ab01398e |
permissions | -rw-r--r-- |
28540
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
1 |
/* |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
4 |
* |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
8 |
* |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
13 |
* accompanied this code). |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
14 |
* |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
18 |
* |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
21 |
* questions. |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
22 |
*/ |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
23 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
24 |
import org.testng.Assert; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
25 |
import org.testng.annotations.DataProvider; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
26 |
import org.testng.annotations.Test; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
27 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
28 |
import java.util.ArrayList; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
29 |
import java.util.List; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
30 |
import java.util.concurrent.CompletableFuture; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
31 |
import java.util.concurrent.CompletionException; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
32 |
import java.util.concurrent.atomic.AtomicReference; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
33 |
import java.util.function.BiFunction; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
34 |
import java.util.function.Consumer; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
35 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
36 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
37 |
/** |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
38 |
* @test |
28755
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
39 |
* @bug 8068432 8072030 |
28540
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
40 |
* @run testng ThenComposeExceptionTest |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
41 |
* @summary Test that CompletableFuture.thenCompose works correctly if the |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
42 |
* composing future completes exceptionally |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
43 |
*/ |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
44 |
@Test |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
45 |
public class ThenComposeExceptionTest { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
46 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
47 |
static final BiFunction<CompletableFuture<String>, CompletableFuture<String>, CompletableFuture<String>> |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
48 |
THEN_COMPOSE = (f, fe) -> f.thenCompose(s -> fe); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
49 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
50 |
static final BiFunction<CompletableFuture<String>, CompletableFuture<String>, CompletableFuture<String>> |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
51 |
THEN_COMPOSE_ASYNC = (f, fe) -> f.thenComposeAsync(s -> fe); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
52 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
53 |
static final Consumer<CompletableFuture<String>> |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
54 |
COMPLETE_EXCEPTIONALLY = f -> f.completeExceptionally(new RuntimeException()); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
55 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
56 |
static final Consumer<CompletableFuture<String>> |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
57 |
NOP = f -> { }; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
58 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
59 |
static Object[][] actionsDataProvider; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
60 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
61 |
@DataProvider(name = "actions") |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
62 |
static Object[][] actionsDataProvider() { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
63 |
if (actionsDataProvider != null) { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
64 |
return actionsDataProvider; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
65 |
} |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
66 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
67 |
List<Object[]> data = new ArrayList<>(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
68 |
data.add(new Object[]{"thenCompose and completeExceptionally", NOP, THEN_COMPOSE, COMPLETE_EXCEPTIONALLY}); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
69 |
data.add(new Object[]{"thenComposeAsync and completeExceptionally", NOP, THEN_COMPOSE_ASYNC, COMPLETE_EXCEPTIONALLY}); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
70 |
data.add(new Object[]{"completeExceptionally and thenCompose", COMPLETE_EXCEPTIONALLY, THEN_COMPOSE, NOP}); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
71 |
data.add(new Object[]{"completeExceptionally and thenComposeAsync", COMPLETE_EXCEPTIONALLY, THEN_COMPOSE_ASYNC, NOP}); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
72 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
73 |
return actionsDataProvider = data.toArray(new Object[0][]); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
74 |
} |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
75 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
76 |
@Test(dataProvider = "actions") |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
77 |
public void testThenCompose( |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
78 |
String description, |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
79 |
Consumer<CompletableFuture<String>> beforeAction, |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
80 |
BiFunction<CompletableFuture<String>, CompletableFuture<String>, CompletableFuture<String>> composeFunction, |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
81 |
Consumer<CompletableFuture<String>> afterAction) throws Exception { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
82 |
CompletableFuture<String> f = new CompletableFuture<>(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
83 |
CompletableFuture<String> fe = new CompletableFuture<>(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
84 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
85 |
// Ensure pre-composed stage is completed to trigger |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
86 |
// processing the composing future |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
87 |
f.complete(""); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
88 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
89 |
beforeAction.accept(fe); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
90 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
91 |
CompletableFuture<String> f_thenCompose = composeFunction.apply(f, fe); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
92 |
Assert.assertNotSame(f_thenCompose, fe, "Composed CompletableFuture returned directly"); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
93 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
94 |
AtomicReference<Throwable> eOnWhenComplete = new AtomicReference<>(); |
28755
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
95 |
CompletableFuture<String> f_whenComplete = f_thenCompose. |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
96 |
whenComplete((r, e) -> eOnWhenComplete.set(e)); |
28540
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
97 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
98 |
afterAction.accept(fe); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
99 |
|
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
100 |
Throwable eOnJoined = null; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
101 |
try { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
102 |
f_thenCompose.join(); |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
103 |
} |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
104 |
catch (Throwable t) { |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
105 |
eOnJoined = t; |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
106 |
} |
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
107 |
Assert.assertTrue(eOnJoined instanceof CompletionException, |
28755
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
108 |
"Incorrect exception reported when joined on thenCompose: " + eOnJoined); |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
109 |
|
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
110 |
// Need to wait for f_whenComplete to complete to avoid |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
111 |
// race condition when updating eOnWhenComplete |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
112 |
eOnJoined = null; |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
113 |
try { |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
114 |
f_whenComplete.join(); |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
115 |
} catch (Throwable t) { |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
116 |
eOnJoined = t; |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
117 |
} |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
118 |
Assert.assertTrue(eOnJoined instanceof CompletionException, |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
119 |
"Incorrect exception reported when joined on whenComplete: " + eOnJoined); |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
120 |
Assert.assertTrue(eOnWhenComplete.get() instanceof CompletionException, |
204aa673becb
8072030: Race condition in ThenComposeExceptionTest.java
psandoz
parents:
28540
diff
changeset
|
121 |
"Incorrect exception passed to whenComplete: " + eOnWhenComplete.get()); |
28540
04b399ec3dfc
8068432: Inconsistent exception handling in CompletableFuture.thenCompose
dl
parents:
diff
changeset
|
122 |
} |
32427
c22b7e41adf3
8134984: Text files should end in exactly one newline
martin
parents:
28755
diff
changeset
|
123 |
} |