@SuppressWarnings("unchecked") static <T> void checkCompletedExceptionally(CompletableFuture<T> cf, boolean cancelled) throws Exception { try { cf.join(); fail("Excepted exception to be thrown"); } catch (CompletionException x) { if (cancelled) fail(); else pass(); } catch (CancellationException x) { if (cancelled) pass(); else fail(); } try { cf.getNow(null); fail("Excepted exception to be thrown"); } catch (CompletionException x) { if (cancelled) fail(); else pass(); } catch (CancellationException x) { if (cancelled) pass(); else fail(); } try { cf.get(); fail("Excepted exception to be thrown"); } catch (CancellationException x) { if (cancelled) pass(); else fail(); } catch (ExecutionException x) { if (cancelled) check(x.getCause() instanceof CancellationException); else pass(); } try { cf.get(0L, SECONDS); fail("Excepted exception to be thrown"); } catch (CancellationException x) { if (cancelled) pass(); else fail(); } catch (ExecutionException x) { if (cancelled) check(x.getCause() instanceof CancellationException); else pass(); } check(cf.isDone(), "Expected isDone to be true, got:" + cf); check( cf.isCancelled() == cancelled, "Expected isCancelled: " + cancelled + ", got:" + cf.isCancelled()); check( cf.cancel(true) == cancelled, "Expected cancel: " + cancelled + ", got:" + cf.cancel(true)); check(cf.toString().contains("[Completed exceptionally]")); // ## TODO: 'E'xceptionally check(cf.complete((T) new Object()) == false, "Expected complete() to fail"); check( cf.completeExceptionally(new Throwable()) == false, "Expected completeExceptionally() to fail, already completed"); }
static void checkCompletedNormally(CompletableFuture<?> cf, Object[] values) { try { equalAnyOf(cf.join(), values); } catch (Throwable x) { unexpected(x); } try { equalAnyOf(cf.getNow(null), values); } catch (Throwable x) { unexpected(x); } try { equalAnyOf(cf.get(), values); } catch (Throwable x) { unexpected(x); } try { equalAnyOf(cf.get(0L, SECONDS), values); } catch (Throwable x) { unexpected(x); } check(cf.isDone(), "Expected isDone to be true, got:" + cf); check(!cf.isCancelled(), "Expected isCancelled to be false"); check(!cf.cancel(true), "Expected cancel to return false"); check(cf.toString().contains("[Completed normally]")); check(cf.complete(null) == false, "Expected complete() to fail"); check( cf.completeExceptionally(new Throwable()) == false, "Expected completeExceptionally() to fail"); }