use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testThenCompose_normalCompletion.
/**
* thenCompose result completes normally after normal completion of source
*/
public void testThenCompose_normalCompletion() {
for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFutureInc r = new CompletableFutureInc(m);
if (!createIncomplete)
assertTrue(f.complete(v1));
final CompletableFuture<Integer> g = m.thenCompose(f, r);
if (createIncomplete)
assertTrue(f.complete(v1));
checkCompletedNormally(g, inc(v1));
checkCompletedNormally(f, v1);
r.assertValue(v1);
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testComplete.
/**
* complete completes normally, as indicated by methods isDone,
* isCancelled, join, get, and getNow
*/
public void testComplete() {
for (Integer v1 : new Integer[] { 1, null }) {
CompletableFuture<Integer> f = new CompletableFuture<>();
checkIncomplete(f);
assertTrue(f.complete(v1));
assertFalse(f.complete(v1));
checkCompletedNormally(f, v1);
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testApplyToEither_actionFailed.
/**
* applyToEither result completes exceptionally if action does
*/
public void testApplyToEither_actionFailed() {
for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) {
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final FailingFunction[] rs = new FailingFunction[6];
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
f.complete(v1);
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
checkCompletedWithWrappedException(h0, rs[0].ex);
checkCompletedWithWrappedException(h1, rs[1].ex);
checkCompletedWithWrappedException(h2, rs[2].ex);
checkCompletedWithWrappedException(h3, rs[3].ex);
for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
g.complete(v2);
// 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]);
checkCompletedWithWrappedException(h4, rs[4].ex);
assertTrue(Objects.equals(v1, rs[4].value) || Objects.equals(v2, rs[4].value));
checkCompletedWithWrappedException(h5, rs[5].ex);
assertTrue(Objects.equals(v1, rs[5].value) || Objects.equals(v2, rs[5].value));
checkCompletedNormally(f, v1);
checkCompletedNormally(g, v2);
}
}
use of java8.util.concurrent.CompletableFuture in project streamsupport by stefan-zobel.
the class CompletableFutureTest method testCompleteOnTimeout_completed.
/**
* completeOnTimeout has no effect if completed within timeout
*/
public void testCompleteOnTimeout_completed() {
for (Integer v1 : new Integer[] { 1, null }) {
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletableFuture<Integer> g = new CompletableFuture<>();
long startTime = System.nanoTime();
f.complete(v1);
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
g.complete(v1);
checkCompletedNormally(f, v1);
checkCompletedNormally(g, v1);
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
}
}
Aggregations