use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testRejectingExecutorNeverInvoked.
/**
* Test submissions to an executor that rejects all tasks, but
* should never be invoked because the dependent future is
* explicitly completed.
*/
public void testRejectingExecutorNeverInvoked() {
final CountingRejectingExecutor e = new CountingRejectingExecutor();
for (Integer v : new Integer[] { 1, null }) {
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
// List<CompletableFuture<?>> futures = new ArrayList<>();
List<CompletableFuture<Integer>> srcs = new ArrayList<>();
srcs.add(complete);
srcs.add(incomplete);
List<CompletableFuture<?>> fs = new ArrayList<>();
fs.add(incomplete.thenRunAsync(() -> {
}, e));
fs.add(incomplete.thenAcceptAsync(z -> {
}, e));
fs.add(incomplete.thenApplyAsync(z -> z, e));
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {
}, e));
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {
}, e));
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {
}, e));
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {
}, e));
fs.add(incomplete.thenComposeAsync(z -> null, e));
fs.add(incomplete.whenCompleteAsync((z, t) -> {
}, e));
fs.add(incomplete.handleAsync((z, t) -> null, e));
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {
}, e));
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {
}, e));
fs.add(complete.runAfterBothAsync(incomplete, () -> {
}, e));
fs.add(incomplete.runAfterBothAsync(complete, () -> {
}, e));
for (CompletableFuture<?> future : fs) checkIncomplete(future);
for (CompletableFuture<?> future : fs) future.complete(null);
incomplete.complete(v);
for (CompletableFuture<?> future : fs) checkCompletedNormally(future, null);
assertEquals(0, e.count.get());
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testApplyToEither_sourceCancelled.
/**
* applyToEither result completes exceptionally if either source cancelled
*/
public void testApplyToEither_sourceCancelled() {
for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final IncFunction[] rs = new IncFunction[6];
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
checkIncomplete(h0);
checkIncomplete(h1);
rs[0].assertNotInvoked();
rs[1].assertNotInvoked();
f.cancel(mayInterruptIfRunning);
checkCompletedWithWrappedCancellationException(h0);
checkCompletedWithWrappedCancellationException(h1);
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
checkCompletedWithWrappedCancellationException(h2);
checkCompletedWithWrappedCancellationException(h3);
g.complete(v1);
// unspecified behavior - both source completions available
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
try {
assertEquals(inc(v1), h4.join());
rs[4].assertValue(inc(v1));
} catch (CompletionException ok) {
checkCompletedWithWrappedCancellationException(h4);
rs[4].assertNotInvoked();
}
try {
assertEquals(inc(v1), h5.join());
rs[5].assertValue(inc(v1));
} catch (CompletionException ok) {
checkCompletedWithWrappedCancellationException(h5);
rs[5].assertNotInvoked();
}
checkCancelled(f);
checkCompletedNormally(g, v1);
checkCompletedWithWrappedCancellationException(h0);
checkCompletedWithWrappedCancellationException(h1);
checkCompletedWithWrappedCancellationException(h2);
checkCompletedWithWrappedCancellationException(h3);
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testAcceptEither_sourceCancelled.
/**
* acceptEither result completes exceptionally if either source cancelled
*/
public void testAcceptEither_sourceCancelled() {
for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final NoopConsumer[] rs = new NoopConsumer[6];
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
checkIncomplete(h0);
checkIncomplete(h1);
rs[0].assertNotInvoked();
rs[1].assertNotInvoked();
f.cancel(mayInterruptIfRunning);
checkCompletedWithWrappedCancellationException(h0);
checkCompletedWithWrappedCancellationException(h1);
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
checkCompletedWithWrappedCancellationException(h2);
checkCompletedWithWrappedCancellationException(h3);
g.complete(v1);
// unspecified behavior - both source completions available
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
try {
assertNull(h4.join());
rs[4].assertValue(v1);
} catch (CompletionException ok) {
checkCompletedWithWrappedCancellationException(h4);
rs[4].assertNotInvoked();
}
try {
assertNull(h5.join());
rs[5].assertValue(v1);
} catch (CompletionException ok) {
checkCompletedWithWrappedCancellationException(h5);
rs[5].assertNotInvoked();
}
checkCancelled(f);
checkCompletedNormally(g, v1);
checkCompletedWithWrappedCancellationException(h0);
checkCompletedWithWrappedCancellationException(h1);
checkCompletedWithWrappedCancellationException(h2);
checkCompletedWithWrappedCancellationException(h3);
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testMinimalCompletionStage2.
/**
* minimalCompletionStage returns a CompletableFuture that is
* completed exceptionally when source is.
*/
public void testMinimalCompletionStage2() {
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletionStage<Integer> g = f.minimalCompletionStage();
AtomicInteger x = new AtomicInteger(0);
AtomicReference<Throwable> r = new AtomicReference<>();
g.whenComplete((v, e) -> {
if (e != null)
r.set(e);
else
x.set(v);
});
checkIncomplete(f);
CFException ex = new CFException();
f.completeExceptionally(ex);
checkCompletedExceptionally(f, ex);
assertEquals(x.get(), 0);
assertEquals(r.get().getCause(), ex);
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testRunAfterBoth_sourceCancelled.
/**
* runAfterBoth result completes exceptionally if either source cancelled
*/
public void testRunAfterBoth_sourceCancelled() throws Throwable {
for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (boolean fFirst : new boolean[] { true, false }) for (boolean failFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final Noop r1 = new Noop(m);
final Noop r2 = new Noop(m);
final Noop r3 = new Noop(m);
final CompletableFuture<Integer> fst = fFirst ? f : g;
final CompletableFuture<Integer> snd = !fFirst ? f : g;
final Callable<Boolean> complete1 = failFirst ? () -> fst.cancel(mayInterruptIfRunning) : () -> fst.complete(v1);
final Callable<Boolean> complete2 = failFirst ? () -> snd.complete(v1) : () -> snd.cancel(mayInterruptIfRunning);
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
assertTrue(complete1.call());
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
checkIncomplete(h1);
checkIncomplete(h2);
assertTrue(complete2.call());
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
checkCompletedWithWrappedCancellationException(h1);
checkCompletedWithWrappedCancellationException(h2);
checkCompletedWithWrappedCancellationException(h3);
r1.assertNotInvoked();
r2.assertNotInvoked();
r3.assertNotInvoked();
checkCompletedNormally(failFirst ? snd : fst, v1);
checkCancelled(failFirst ? fst : snd);
}
}
Aggregations