ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.181 by jsr166, Sat Nov 26 20:32:24 2016 UTC vs.
Revision 1.194 by jsr166, Tue May 29 03:42:37 2018 UTC

# Line 41 | Line 41 | import java.util.function.Function;
41   import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
44 import junit.framework.AssertionFailedError;
44   import junit.framework.Test;
45   import junit.framework.TestSuite;
46  
# Line 59 | Line 58 | public class CompletableFutureTest exten
58      void checkIncomplete(CompletableFuture<?> f) {
59          assertFalse(f.isDone());
60          assertFalse(f.isCancelled());
61 <        assertTrue(f.toString().contains("Not completed"));
61 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
62          try {
63              assertNull(f.getNow(null));
64          } catch (Throwable fail) { threadUnexpectedException(fail); }
65          try {
66 <            f.get(0L, SECONDS);
66 >            f.get(randomExpiredTimeout(), randomTimeUnit());
67              shouldThrow();
68          }
69          catch (TimeoutException success) {}
# Line 76 | Line 75 | public class CompletableFutureTest exten
75  
76          try {
77              assertEquals(value, f.join());
79        } catch (Throwable fail) { threadUnexpectedException(fail); }
80        try {
78              assertEquals(value, f.getNow(null));
82        } catch (Throwable fail) { threadUnexpectedException(fail); }
83        try {
79              assertEquals(value, f.get());
80          } catch (Throwable fail) { threadUnexpectedException(fail); }
81          assertTrue(f.isDone());
82          assertFalse(f.isCancelled());
83          assertFalse(f.isCompletedExceptionally());
84 <        assertTrue(f.toString().contains("[Completed normally]"));
84 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
85      }
86  
87      /**
88       * Returns the "raw" internal exceptional completion of f,
89       * without any additional wrapping with CompletionException.
90       */
91 <    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
92 <        // handle (and whenComplete) can distinguish between "direct"
93 <        // and "wrapped" exceptional completion
94 <        return f.handle((U u, Throwable t) -> t).join();
91 >    Throwable exceptionalCompletion(CompletableFuture<?> f) {
92 >        // handle (and whenComplete and exceptionally) can distinguish
93 >        // between "direct" and "wrapped" exceptional completion
94 >        return f.handle((u, t) -> t).join();
95      }
96  
97      void checkCompletedExceptionally(CompletableFuture<?> f,
# Line 142 | Line 137 | public class CompletableFutureTest exten
137          assertFalse(f.isCancelled());
138          assertTrue(f.isDone());
139          assertTrue(f.isCompletedExceptionally());
140 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
140 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
141      }
142  
143      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
# Line 197 | Line 192 | public class CompletableFutureTest exten
192          assertTrue(f.isDone());
193          assertTrue(f.isCompletedExceptionally());
194          assertTrue(f.isCancelled());
195 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
195 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
196      }
197  
198      /**
# Line 296 | Line 291 | public class CompletableFutureTest exten
291          }
292  
293          f = new CompletableFuture<>();
294 <        f.completeExceptionally(ex = new CFException());
294 >        f.completeExceptionally(new CFException());
295          f.obtrudeValue(v1);
296          checkCompletedNormally(f, v1);
297          f.obtrudeException(ex = new CFException());
# Line 333 | Line 328 | public class CompletableFutureTest exten
328      /**
329       * toString indicates current completion state
330       */
331 <    public void testToString() {
332 <        CompletableFuture<String> f;
333 <
334 <        f = new CompletableFuture<String>();
335 <        assertTrue(f.toString().contains("[Not completed]"));
331 >    public void testToString_incomplete() {
332 >        CompletableFuture<String> f = new CompletableFuture<>();
333 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
334 >        if (testImplementationDetails)
335 >            assertEquals(identityString(f) + "[Not completed]",
336 >                         f.toString());
337 >    }
338  
339 +    public void testToString_normal() {
340 +        CompletableFuture<String> f = new CompletableFuture<>();
341          assertTrue(f.complete("foo"));
342 <        assertTrue(f.toString().contains("[Completed normally]"));
342 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
343 >        if (testImplementationDetails)
344 >            assertEquals(identityString(f) + "[Completed normally]",
345 >                         f.toString());
346 >    }
347  
348 <        f = new CompletableFuture<String>();
348 >    public void testToString_exception() {
349 >        CompletableFuture<String> f = new CompletableFuture<>();
350          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
351 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
351 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
352 >        if (testImplementationDetails)
353 >            assertTrue(f.toString().startsWith(
354 >                               identityString(f) + "[Completed exceptionally: "));
355 >    }
356  
357 +    public void testToString_cancelled() {
358          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
359 <            f = new CompletableFuture<String>();
359 >            CompletableFuture<String> f = new CompletableFuture<>();
360              assertTrue(f.cancel(mayInterruptIfRunning));
361 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
361 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
362 >            if (testImplementationDetails)
363 >                assertTrue(f.toString().startsWith(
364 >                                   identityString(f) + "[Completed exceptionally: "));
365          }
366      }
367  
# Line 1242 | Line 1254 | public class CompletableFutureTest exten
1254          r.assertInvoked();
1255      }}
1256  
1257 +    @SuppressWarnings("FutureReturnValueIgnored")
1258      public void testRunAsync_rejectingExecutor() {
1259          CountingRejectingExecutor e = new CountingRejectingExecutor();
1260          try {
# Line 1288 | Line 1301 | public class CompletableFutureTest exten
1301          r.assertInvoked();
1302      }}
1303  
1304 +    @SuppressWarnings("FutureReturnValueIgnored")
1305      public void testSupplyAsync_rejectingExecutor() {
1306          CountingRejectingExecutor e = new CountingRejectingExecutor();
1307          try {
# Line 2562 | Line 2576 | public class CompletableFutureTest exten
2576  
2577          // unspecified behavior - both source completions available
2578          try {
2579 <            assertEquals(null, h0.join());
2579 >            assertNull(h0.join());
2580              rs[0].assertValue(v1);
2581          } catch (CompletionException ok) {
2582              checkCompletedWithWrappedException(h0, ex);
2583              rs[0].assertNotInvoked();
2584          }
2585          try {
2586 <            assertEquals(null, h1.join());
2586 >            assertNull(h1.join());
2587              rs[1].assertValue(v1);
2588          } catch (CompletionException ok) {
2589              checkCompletedWithWrappedException(h1, ex);
2590              rs[1].assertNotInvoked();
2591          }
2592          try {
2593 <            assertEquals(null, h2.join());
2593 >            assertNull(h2.join());
2594              rs[2].assertValue(v1);
2595          } catch (CompletionException ok) {
2596              checkCompletedWithWrappedException(h2, ex);
2597              rs[2].assertNotInvoked();
2598          }
2599          try {
2600 <            assertEquals(null, h3.join());
2600 >            assertNull(h3.join());
2601              rs[3].assertValue(v1);
2602          } catch (CompletionException ok) {
2603              checkCompletedWithWrappedException(h3, ex);
# Line 2822 | Line 2836 | public class CompletableFutureTest exten
2836  
2837          // unspecified behavior - both source completions available
2838          try {
2839 <            assertEquals(null, h0.join());
2839 >            assertNull(h0.join());
2840              rs[0].assertInvoked();
2841          } catch (CompletionException ok) {
2842              checkCompletedWithWrappedException(h0, ex);
2843              rs[0].assertNotInvoked();
2844          }
2845          try {
2846 <            assertEquals(null, h1.join());
2846 >            assertNull(h1.join());
2847              rs[1].assertInvoked();
2848          } catch (CompletionException ok) {
2849              checkCompletedWithWrappedException(h1, ex);
2850              rs[1].assertNotInvoked();
2851          }
2852          try {
2853 <            assertEquals(null, h2.join());
2853 >            assertNull(h2.join());
2854              rs[2].assertInvoked();
2855          } catch (CompletionException ok) {
2856              checkCompletedWithWrappedException(h2, ex);
2857              rs[2].assertNotInvoked();
2858          }
2859          try {
2860 <            assertEquals(null, h3.join());
2860 >            assertNull(h3.join());
2861              rs[3].assertInvoked();
2862          } catch (CompletionException ok) {
2863              checkCompletedWithWrappedException(h3, ex);
# Line 3238 | Line 3252 | public class CompletableFutureTest exten
3252      /**
3253       * Completion methods throw NullPointerException with null arguments
3254       */
3255 +    @SuppressWarnings("FutureReturnValueIgnored")
3256      public void testNPE() {
3257          CompletableFuture<Integer> f = new CompletableFuture<>();
3258          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3532 | Line 3547 | public class CompletableFutureTest exten
3547       */
3548      public void testCompletedStage() {
3549          AtomicInteger x = new AtomicInteger(0);
3550 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3550 >        AtomicReference<Throwable> r = new AtomicReference<>();
3551          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3552          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3553          assertEquals(x.get(), 1);
# Line 3634 | Line 3649 | public class CompletableFutureTest exten
3649          CompletableFuture<Integer> f = new CompletableFuture<>();
3650          CompletionStage<Integer> g = f.minimalCompletionStage();
3651          AtomicInteger x = new AtomicInteger(0);
3652 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3652 >        AtomicReference<Throwable> r = new AtomicReference<>();
3653          checkIncomplete(f);
3654          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3655          f.complete(1);
# Line 3651 | Line 3666 | public class CompletableFutureTest exten
3666          CompletableFuture<Integer> f = new CompletableFuture<>();
3667          CompletionStage<Integer> g = f.minimalCompletionStage();
3668          AtomicInteger x = new AtomicInteger(0);
3669 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3669 >        AtomicReference<Throwable> r = new AtomicReference<>();
3670          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3671          checkIncomplete(f);
3672          CFException ex = new CFException();
# Line 3669 | Line 3684 | public class CompletableFutureTest exten
3684          CFException ex = new CFException();
3685          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3686          AtomicInteger x = new AtomicInteger(0);
3687 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3687 >        AtomicReference<Throwable> r = new AtomicReference<>();
3688          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3689          assertEquals(x.get(), 0);
3690          assertEquals(r.get(), ex);
# Line 4174 | Line 4189 | public class CompletableFutureTest exten
4189          static void assertZero(CompletableFuture<?> f) {
4190              try {
4191                  f.getNow(null);
4192 <                throw new AssertionFailedError("should throw");
4192 >                throw new AssertionError("should throw");
4193              } catch (CompletionException success) {
4194                  assertTrue(success.getCause() instanceof ZeroException);
4195              }
# Line 4299 | Line 4314 | public class CompletableFutureTest exten
4314      }
4315  
4316      /** Test long recursive chains of CompletableFutures with cascading completions */
4317 +    @SuppressWarnings("FutureReturnValueIgnored")
4318      public void testRecursiveChains() throws Throwable {
4319          for (ExecutionMode m : ExecutionMode.values())
4320          for (boolean addDeadEnds : new boolean[] { true, false })
# Line 4323 | Line 4339 | public class CompletableFutureTest exten
4339       * A single CompletableFuture with many dependents.
4340       * A demo of scalability - runtime is O(n).
4341       */
4342 +    @SuppressWarnings("FutureReturnValueIgnored")
4343      public void testManyDependents() throws Throwable {
4344          final int n = expensiveTests ? 1_000_000 : 10;
4345          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4352 | Line 4369 | public class CompletableFutureTest exten
4369      }
4370  
4371      /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4372 +    @SuppressWarnings("FutureReturnValueIgnored")
4373      public void testCoCompletionGarbageRetention() throws Throwable {
4374          final int n = expensiveTests ? 1_000_000 : 10;
4375          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 4370 | Line 4388 | public class CompletableFutureTest exten
4388              f.complete(null);
4389  
4390              f = new CompletableFuture<>();
4391 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4391 >            CompletableFuture.anyOf(f, incomplete);
4392              f.complete(null);
4393          }
4394  
# Line 4388 | Line 4406 | public class CompletableFutureTest exten
4406              f.complete(null);
4407  
4408              f = new CompletableFuture<>();
4409 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4409 >            CompletableFuture.anyOf(incomplete, f);
4410              f.complete(null);
4411          }
4412      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines