ListenableFuture provides CompletableFuture adaptation via completable()

Issue: SPR-15696
This commit is contained in:
Juergen Hoeller
2017-06-27 00:43:37 +02:00
parent 98642c7e29
commit 87430f3cd3
12 changed files with 284 additions and 51 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.scheduling.annotation;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
@@ -33,7 +34,7 @@ import static org.junit.Assert.*;
public class AsyncResultTests {
@Test
public void asyncResultWithCallbackAndValue() {
public void asyncResultWithCallbackAndValue() throws Exception {
String value = "val";
final Set<String> values = new HashSet<>(1);
ListenableFuture<String> future = AsyncResult.forValue(value);
@@ -48,10 +49,13 @@ public class AsyncResultTests {
}
});
assertSame(value, values.iterator().next());
assertSame(value, future.get());
assertSame(value, future.completable().get());
future.completable().thenAccept(v -> assertSame(value, v));
}
@Test
public void asyncResultWithCallbackAndException() {
public void asyncResultWithCallbackAndException() throws Exception {
IOException ex = new IOException();
final Set<Throwable> values = new HashSet<>(1);
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
@@ -66,24 +70,55 @@ public class AsyncResultTests {
}
});
assertSame(ex, values.iterator().next());
try {
future.get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex2) {
assertSame(ex, ex2.getCause());
}
try {
future.completable().get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex2) {
assertSame(ex, ex2.getCause());
}
}
@Test
public void asyncResultWithSeparateCallbacksAndValue() {
public void asyncResultWithSeparateCallbacksAndValue() throws Exception {
String value = "val";
final Set<String> values = new HashSet<>(1);
ListenableFuture<String> future = AsyncResult.forValue(value);
future.addCallback(values::add, (ex) -> fail("Failure callback not expected: " + ex));
assertSame(value, values.iterator().next());
assertSame(value, future.get());
assertSame(value, future.completable().get());
future.completable().thenAccept(v -> assertSame(value, v));
}
@Test
public void asyncResultWithSeparateCallbacksAndException() {
public void asyncResultWithSeparateCallbacksAndException() throws Exception {
IOException ex = new IOException();
final Set<Throwable> values = new HashSet<>(1);
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
future.addCallback((result) -> fail("Success callback not expected: " + result), values::add);
assertSame(ex, values.iterator().next());
try {
future.get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex2) {
assertSame(ex, ex2.getCause());
}
try {
future.completable().get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex2) {
assertSame(ex, ex2.getCause());
}
}
}