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.182 by jsr166, Tue Jan 3 03:18:02 2017 UTC vs.
Revision 1.197 by jsr166, Sun Jul 22 22:08:49 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 >
63 >        Object result = null;
64          try {
65 <            assertNull(f.getNow(null));
65 >            result = f.getNow(null);
66          } catch (Throwable fail) { threadUnexpectedException(fail); }
67 +        assertNull(result);
68 +
69          try {
70 <            f.get(0L, SECONDS);
70 >            f.get(randomExpiredTimeout(), randomTimeUnit());
71              shouldThrow();
72          }
73          catch (TimeoutException success) {}
74          catch (Throwable fail) { threadUnexpectedException(fail); }
75      }
76  
77 <    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
78 <        checkTimedGet(f, value);
77 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
78 >        checkTimedGet(f, expectedValue);
79  
80 +        assertEquals(expectedValue, f.join());
81 +        assertEquals(expectedValue, f.getNow(null));
82 +
83 +        T result = null;
84          try {
85 <            assertEquals(value, f.join());
79 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
80 <        try {
81 <            assertEquals(value, f.getNow(null));
82 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 <        try {
84 <            assertEquals(value, f.get());
85 >            result = f.get();
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertEquals(expectedValue, result);
88 +
89          assertTrue(f.isDone());
90          assertFalse(f.isCancelled());
91          assertFalse(f.isCompletedExceptionally());
92 <        assertTrue(f.toString().contains("[Completed normally]"));
92 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
93      }
94  
95      /**
# Line 142 | Line 145 | public class CompletableFutureTest exten
145          assertFalse(f.isCancelled());
146          assertTrue(f.isDone());
147          assertTrue(f.isCompletedExceptionally());
148 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
148 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
149      }
150  
151      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
# Line 197 | Line 200 | public class CompletableFutureTest exten
200          assertTrue(f.isDone());
201          assertTrue(f.isCompletedExceptionally());
202          assertTrue(f.isCancelled());
203 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
203 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
204      }
205  
206      /**
# Line 296 | Line 299 | public class CompletableFutureTest exten
299          }
300  
301          f = new CompletableFuture<>();
302 <        f.completeExceptionally(ex = new CFException());
302 >        f.completeExceptionally(new CFException());
303          f.obtrudeValue(v1);
304          checkCompletedNormally(f, v1);
305          f.obtrudeException(ex = new CFException());
# Line 333 | Line 336 | public class CompletableFutureTest exten
336      /**
337       * toString indicates current completion state
338       */
339 <    public void testToString() {
340 <        CompletableFuture<String> f;
341 <
342 <        f = new CompletableFuture<String>();
343 <        assertTrue(f.toString().contains("[Not completed]"));
339 >    public void testToString_incomplete() {
340 >        CompletableFuture<String> f = new CompletableFuture<>();
341 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
342 >        if (testImplementationDetails)
343 >            assertEquals(identityString(f) + "[Not completed]",
344 >                         f.toString());
345 >    }
346  
347 +    public void testToString_normal() {
348 +        CompletableFuture<String> f = new CompletableFuture<>();
349          assertTrue(f.complete("foo"));
350 <        assertTrue(f.toString().contains("[Completed normally]"));
350 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
351 >        if (testImplementationDetails)
352 >            assertEquals(identityString(f) + "[Completed normally]",
353 >                         f.toString());
354 >    }
355  
356 <        f = new CompletableFuture<String>();
356 >    public void testToString_exception() {
357 >        CompletableFuture<String> f = new CompletableFuture<>();
358          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
359 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
360 >        if (testImplementationDetails)
361 >            assertTrue(f.toString().startsWith(
362 >                               identityString(f) + "[Completed exceptionally: "));
363 >    }
364  
365 +    public void testToString_cancelled() {
366          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
367 <            f = new CompletableFuture<String>();
367 >            CompletableFuture<String> f = new CompletableFuture<>();
368              assertTrue(f.cancel(mayInterruptIfRunning));
369 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
369 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
370 >            if (testImplementationDetails)
371 >                assertTrue(f.toString().startsWith(
372 >                                   identityString(f) + "[Completed exceptionally: "));
373          }
374      }
375  
# Line 1242 | Line 1262 | public class CompletableFutureTest exten
1262          r.assertInvoked();
1263      }}
1264  
1265 +    @SuppressWarnings("FutureReturnValueIgnored")
1266      public void testRunAsync_rejectingExecutor() {
1267          CountingRejectingExecutor e = new CountingRejectingExecutor();
1268          try {
# Line 1288 | Line 1309 | public class CompletableFutureTest exten
1309          r.assertInvoked();
1310      }}
1311  
1312 +    @SuppressWarnings("FutureReturnValueIgnored")
1313      public void testSupplyAsync_rejectingExecutor() {
1314          CountingRejectingExecutor e = new CountingRejectingExecutor();
1315          try {
# Line 2562 | Line 2584 | public class CompletableFutureTest exten
2584  
2585          // unspecified behavior - both source completions available
2586          try {
2587 <            assertEquals(null, h0.join());
2587 >            assertNull(h0.join());
2588              rs[0].assertValue(v1);
2589          } catch (CompletionException ok) {
2590              checkCompletedWithWrappedException(h0, ex);
2591              rs[0].assertNotInvoked();
2592          }
2593          try {
2594 <            assertEquals(null, h1.join());
2594 >            assertNull(h1.join());
2595              rs[1].assertValue(v1);
2596          } catch (CompletionException ok) {
2597              checkCompletedWithWrappedException(h1, ex);
2598              rs[1].assertNotInvoked();
2599          }
2600          try {
2601 <            assertEquals(null, h2.join());
2601 >            assertNull(h2.join());
2602              rs[2].assertValue(v1);
2603          } catch (CompletionException ok) {
2604              checkCompletedWithWrappedException(h2, ex);
2605              rs[2].assertNotInvoked();
2606          }
2607          try {
2608 <            assertEquals(null, h3.join());
2608 >            assertNull(h3.join());
2609              rs[3].assertValue(v1);
2610          } catch (CompletionException ok) {
2611              checkCompletedWithWrappedException(h3, ex);
# Line 2822 | Line 2844 | public class CompletableFutureTest exten
2844  
2845          // unspecified behavior - both source completions available
2846          try {
2847 <            assertEquals(null, h0.join());
2847 >            assertNull(h0.join());
2848              rs[0].assertInvoked();
2849          } catch (CompletionException ok) {
2850              checkCompletedWithWrappedException(h0, ex);
2851              rs[0].assertNotInvoked();
2852          }
2853          try {
2854 <            assertEquals(null, h1.join());
2854 >            assertNull(h1.join());
2855              rs[1].assertInvoked();
2856          } catch (CompletionException ok) {
2857              checkCompletedWithWrappedException(h1, ex);
2858              rs[1].assertNotInvoked();
2859          }
2860          try {
2861 <            assertEquals(null, h2.join());
2861 >            assertNull(h2.join());
2862              rs[2].assertInvoked();
2863          } catch (CompletionException ok) {
2864              checkCompletedWithWrappedException(h2, ex);
2865              rs[2].assertNotInvoked();
2866          }
2867          try {
2868 <            assertEquals(null, h3.join());
2868 >            assertNull(h3.join());
2869              rs[3].assertInvoked();
2870          } catch (CompletionException ok) {
2871              checkCompletedWithWrappedException(h3, ex);
# Line 3238 | Line 3260 | public class CompletableFutureTest exten
3260      /**
3261       * Completion methods throw NullPointerException with null arguments
3262       */
3263 +    @SuppressWarnings("FutureReturnValueIgnored")
3264      public void testNPE() {
3265          CompletableFuture<Integer> f = new CompletableFuture<>();
3266          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3532 | Line 3555 | public class CompletableFutureTest exten
3555       */
3556      public void testCompletedStage() {
3557          AtomicInteger x = new AtomicInteger(0);
3558 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3558 >        AtomicReference<Throwable> r = new AtomicReference<>();
3559          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3560          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3561          assertEquals(x.get(), 1);
# Line 3634 | Line 3657 | public class CompletableFutureTest exten
3657          CompletableFuture<Integer> f = new CompletableFuture<>();
3658          CompletionStage<Integer> g = f.minimalCompletionStage();
3659          AtomicInteger x = new AtomicInteger(0);
3660 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3660 >        AtomicReference<Throwable> r = new AtomicReference<>();
3661          checkIncomplete(f);
3662          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3663          f.complete(1);
# Line 3651 | Line 3674 | public class CompletableFutureTest exten
3674          CompletableFuture<Integer> f = new CompletableFuture<>();
3675          CompletionStage<Integer> g = f.minimalCompletionStage();
3676          AtomicInteger x = new AtomicInteger(0);
3677 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3677 >        AtomicReference<Throwable> r = new AtomicReference<>();
3678          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3679          checkIncomplete(f);
3680          CFException ex = new CFException();
# Line 3669 | Line 3692 | public class CompletableFutureTest exten
3692          CFException ex = new CFException();
3693          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3694          AtomicInteger x = new AtomicInteger(0);
3695 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3695 >        AtomicReference<Throwable> r = new AtomicReference<>();
3696          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3697          assertEquals(x.get(), 0);
3698          assertEquals(r.get(), ex);
# Line 4174 | Line 4197 | public class CompletableFutureTest exten
4197          static void assertZero(CompletableFuture<?> f) {
4198              try {
4199                  f.getNow(null);
4200 <                throw new AssertionFailedError("should throw");
4200 >                throw new AssertionError("should throw");
4201              } catch (CompletionException success) {
4202                  assertTrue(success.getCause() instanceof ZeroException);
4203              }
# Line 4299 | Line 4322 | public class CompletableFutureTest exten
4322      }
4323  
4324      /** Test long recursive chains of CompletableFutures with cascading completions */
4325 +    @SuppressWarnings("FutureReturnValueIgnored")
4326      public void testRecursiveChains() throws Throwable {
4327          for (ExecutionMode m : ExecutionMode.values())
4328          for (boolean addDeadEnds : new boolean[] { true, false })
# Line 4323 | Line 4347 | public class CompletableFutureTest exten
4347       * A single CompletableFuture with many dependents.
4348       * A demo of scalability - runtime is O(n).
4349       */
4350 +    @SuppressWarnings("FutureReturnValueIgnored")
4351      public void testManyDependents() throws Throwable {
4352          final int n = expensiveTests ? 1_000_000 : 10;
4353          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4352 | Line 4377 | public class CompletableFutureTest exten
4377      }
4378  
4379      /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4380 +    @SuppressWarnings("FutureReturnValueIgnored")
4381      public void testCoCompletionGarbageRetention() throws Throwable {
4382          final int n = expensiveTests ? 1_000_000 : 10;
4383          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 4370 | Line 4396 | public class CompletableFutureTest exten
4396              f.complete(null);
4397  
4398              f = new CompletableFuture<>();
4399 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4399 >            CompletableFuture.anyOf(f, incomplete);
4400              f.complete(null);
4401          }
4402  
# Line 4388 | Line 4414 | public class CompletableFutureTest exten
4414              f.complete(null);
4415  
4416              f = new CompletableFuture<>();
4417 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4417 >            CompletableFuture.anyOf(incomplete, f);
4418              f.complete(null);
4419          }
4420      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines