AsyncExecutionInterceptor supports Java 8's CompletableFuture as a return type

Issue: SPR-13128
This commit is contained in:
Juergen Hoeller
2015-06-15 20:35:48 +02:00
parent 06a5ed9cae
commit c41779f895
2 changed files with 125 additions and 4 deletions

View File

@@ -21,6 +21,7 @@ import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@@ -71,24 +72,48 @@ public class AsyncExecutionTests {
assertEquals("20", future.get());
ListenableFuture<String> listenableFuture = asyncTest.returnSomethingListenable(20);
assertEquals("20", listenableFuture.get());
CompletableFuture<String> completableFuture = asyncTest.returnSomethingCompletable(20);
assertEquals("20", completableFuture.get());
future = asyncTest.returnSomething(0);
try {
future.get();
asyncTest.returnSomething(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
future = asyncTest.returnSomething(-1);
try {
future.get();
asyncTest.returnSomething(-1).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IOException);
}
try {
asyncTest.returnSomethingListenable(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
try {
asyncTest.returnSomethingListenable(-1).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IOException);
}
try {
asyncTest.returnSomethingCompletable(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
}
@Test
@@ -163,6 +188,32 @@ public class AsyncExecutionTests {
assertEquals("20", future.get());
ListenableFuture<String> listenableFuture = asyncTest.returnSomethingListenable(20);
assertEquals("20", listenableFuture.get());
CompletableFuture<String> completableFuture = asyncTest.returnSomethingCompletable(20);
assertEquals("20", completableFuture.get());
try {
asyncTest.returnSomething(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
try {
asyncTest.returnSomethingListenable(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
try {
asyncTest.returnSomethingCompletable(0).get();
fail("Should have thrown ExecutionException");
}
catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
}
@Test
@@ -397,8 +448,23 @@ public class AsyncExecutionTests {
@Async
public ListenableFuture<String> returnSomethingListenable(int i) {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (i == 0) {
throw new IllegalArgumentException();
}
else if (i < 0) {
return AsyncResult.forExecutionException(new IOException());
}
return new AsyncResult<String>(Integer.toString(i));
}
@Async
public CompletableFuture<String> returnSomethingCompletable(int i) {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (i == 0) {
throw new IllegalArgumentException();
}
return CompletableFuture.completedFuture(Integer.toString(i));
}
}
@@ -459,14 +525,29 @@ public class AsyncExecutionTests {
public Future<String> returnSomething(int i) {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (i == 0) {
throw new IllegalArgumentException();
}
return new AsyncResult<String>(Integer.toString(i));
}
public ListenableFuture<String> returnSomethingListenable(int i) {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (i == 0) {
throw new IllegalArgumentException();
}
return new AsyncResult<String>(Integer.toString(i));
}
@Async
public CompletableFuture<String> returnSomethingCompletable(int i) {
assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
if (i == 0) {
throw new IllegalArgumentException();
}
return CompletableFuture.completedFuture(Integer.toString(i));
}
@Override
public void destroy() {
}