下面列出了java.util.concurrent.CompletableFuture#obtrudeValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* obtrudeValue forces completion with given value
*/
public void testObtrudeValue() {
CompletableFuture<Integer> f = new CompletableFuture<>();
checkIncomplete(f);
assertTrue(f.complete(one));
checkCompletedNormally(f, one);
f.obtrudeValue(three);
checkCompletedNormally(f, three);
f.obtrudeValue(two);
checkCompletedNormally(f, two);
f = new CompletableFuture<>();
f.obtrudeValue(three);
checkCompletedNormally(f, three);
f.obtrudeValue(null);
checkCompletedNormally(f, null);
f = new CompletableFuture<>();
f.completeExceptionally(new CFException());
f.obtrudeValue(four);
checkCompletedNormally(f, four);
}
/**
* obtrudeValue forces completion with given value
*/
public void testObtrudeValue() {
CompletableFuture<Integer> f = new CompletableFuture<>();
checkIncomplete(f);
assertTrue(f.complete(one));
checkCompletedNormally(f, one);
f.obtrudeValue(three);
checkCompletedNormally(f, three);
f.obtrudeValue(two);
checkCompletedNormally(f, two);
f = new CompletableFuture<>();
f.obtrudeValue(three);
checkCompletedNormally(f, three);
f.obtrudeValue(null);
checkCompletedNormally(f, null);
f = new CompletableFuture<>();
f.completeExceptionally(new CFException());
f.obtrudeValue(four);
checkCompletedNormally(f, four);
}
/**
* obtrudeException forces completion with given exception
*/
public void testObtrudeException() {
for (Integer v1 : new Integer[] { 1, null })
{
CFException ex;
CompletableFuture<Integer> f;
f = new CompletableFuture<>();
assertTrue(f.complete(v1));
for (int i = 0; i < 2; i++) {
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
}
f = new CompletableFuture<>();
for (int i = 0; i < 2; i++) {
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
}
f = new CompletableFuture<>();
f.completeExceptionally(ex = new CFException());
f.obtrudeValue(v1);
checkCompletedNormally(f, v1);
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
f.completeExceptionally(new CFException());
checkCompletedExceptionally(f, ex);
assertFalse(f.complete(v1));
checkCompletedExceptionally(f, ex);
}}
@Test
public void getWhenSuccessful_success_async() {
CompletableFuture<Integer> future = new CompletableFuture<Integer>();
AtomicInteger result = new AtomicInteger();
ConcurrentTestHarness.execute(() -> {
result.set(1);
result.set(Async.getWhenSuccessful(future));
});
Awaits.await().untilAtomic(result, is(1));
future.obtrudeValue(2);
Awaits.await().untilAtomic(result, is(2));
}
/**
* obtrudeException forces completion with given exception
*/
public void testObtrudeException() {
for (Integer v1 : new Integer[] { 1, null })
{
CFException ex;
CompletableFuture<Integer> f;
f = new CompletableFuture<>();
assertTrue(f.complete(v1));
for (int i = 0; i < 2; i++) {
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
}
f = new CompletableFuture<>();
for (int i = 0; i < 2; i++) {
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
}
f = new CompletableFuture<>();
f.completeExceptionally(ex = new CFException());
f.obtrudeValue(v1);
checkCompletedNormally(f, v1);
f.obtrudeException(ex = new CFException());
checkCompletedExceptionally(f, ex);
f.completeExceptionally(new CFException());
checkCompletedExceptionally(f, ex);
assertFalse(f.complete(v1));
checkCompletedExceptionally(f, ex);
}}