SettableListenableFuture properly rethrows Error
Issue: SPR-14298
(cherry picked from commit a979885
)
This commit is contained in:
parent
f64ed69280
commit
cc7758869c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -23,6 +23,7 @@ import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link org.springframework.util.concurrent.ListenableFuture ListenableFuture}
|
* A {@link org.springframework.util.concurrent.ListenableFuture ListenableFuture}
|
||||||
|
@ -49,11 +50,11 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the value of this future. This method will return {@code true} if
|
* Set the value of this future. This method will return {@code true} if the
|
||||||
* the value was set successfully, or {@code false} if the future has already
|
* value was set successfully, or {@code false} if the future has already been
|
||||||
* been set or cancelled.
|
* set or cancelled.
|
||||||
* @param value the value that will be set.
|
* @param value the value that will be set
|
||||||
* @return {@code true} if the value was successfully set, else {@code false}.
|
* @return {@code true} if the value was successfully set, else {@code false}
|
||||||
*/
|
*/
|
||||||
public boolean set(T value) {
|
public boolean set(T value) {
|
||||||
boolean success = this.settableTask.setValue(value);
|
boolean success = this.settableTask.setValue(value);
|
||||||
|
@ -64,14 +65,14 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the exception of this future. This method will return {@code true} if
|
* Set the exception of this future. This method will return {@code true} if the
|
||||||
* the exception was set successfully, or {@code false} if the future has already
|
* exception was set successfully, or {@code false} if the future has already been
|
||||||
* been set or cancelled.
|
* set or cancelled.
|
||||||
* @param exception the value that will be set.
|
* @param exception the value that will be set
|
||||||
* @return {@code true} if the exception was successfully set, else {@code false}.
|
* @return {@code true} if the exception was successfully set, else {@code false}
|
||||||
*/
|
*/
|
||||||
public boolean setException(Throwable exception) {
|
public boolean setException(Throwable exception) {
|
||||||
Assert.notNull(exception, "'exception' must not be null");
|
Assert.notNull(exception, "Exception must not be null");
|
||||||
boolean success = this.settableTask.setException(exception);
|
boolean success = this.settableTask.setException(exception);
|
||||||
if (success) {
|
if (success) {
|
||||||
this.listenableFuture.run();
|
this.listenableFuture.run();
|
||||||
|
@ -149,7 +150,7 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
|
||||||
|
|
||||||
private static class SettableTask<T> implements Callable<T> {
|
private static class SettableTask<T> implements Callable<T> {
|
||||||
|
|
||||||
private static final String NO_VALUE = SettableListenableFuture.class.getName() + ".NO_VALUE";
|
private static final Object NO_VALUE = new Object();
|
||||||
|
|
||||||
private final AtomicReference<Object> value = new AtomicReference<Object>(NO_VALUE);
|
private final AtomicReference<Object> value = new AtomicReference<Object>(NO_VALUE);
|
||||||
|
|
||||||
|
@ -176,10 +177,11 @@ public class SettableListenableFuture<T> implements ListenableFuture<T> {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public T call() throws Exception {
|
public T call() throws Exception {
|
||||||
if (value.get() instanceof Exception) {
|
Object val = this.value.get();
|
||||||
throw (Exception) value.get();
|
if (val instanceof Throwable) {
|
||||||
|
ReflectionUtils.rethrowException((Throwable) val);
|
||||||
}
|
}
|
||||||
return (T) value.get();
|
return (T) val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,7 +21,6 @@ import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
|
@ -35,12 +34,8 @@ import static org.mockito.Mockito.*;
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
public class SettableListenableFutureTests {
|
public class SettableListenableFutureTests {
|
||||||
|
|
||||||
private SettableListenableFuture<String> settableListenableFuture;
|
private final SettableListenableFuture<String> settableListenableFuture = new SettableListenableFuture<String>();
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
settableListenableFuture = new SettableListenableFuture<String>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void validateInitialValues() {
|
public void validateInitialValues() {
|
||||||
|
@ -76,6 +71,20 @@ public class SettableListenableFutureTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void throwsSetErrorWrappedInExecutionException() throws ExecutionException, InterruptedException {
|
||||||
|
Throwable exception = new OutOfMemoryError();
|
||||||
|
boolean wasSet = settableListenableFuture.setException(exception);
|
||||||
|
assertTrue(wasSet);
|
||||||
|
try {
|
||||||
|
settableListenableFuture.get();
|
||||||
|
fail("Expected ExecutionException");
|
||||||
|
}
|
||||||
|
catch (ExecutionException ex) {
|
||||||
|
assertThat(ex.getCause(), equalTo(exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setValueTriggersCallback() {
|
public void setValueTriggersCallback() {
|
||||||
String string = "hello";
|
String string = "hello";
|
||||||
|
@ -85,7 +94,6 @@ public class SettableListenableFutureTests {
|
||||||
public void onSuccess(String result) {
|
public void onSuccess(String result) {
|
||||||
callbackHolder[0] = result;
|
callbackHolder[0] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable ex) {
|
public void onFailure(Throwable ex) {
|
||||||
fail("Expected onSuccess() to be called");
|
fail("Expected onSuccess() to be called");
|
||||||
|
@ -104,7 +112,6 @@ public class SettableListenableFutureTests {
|
||||||
public void onSuccess(String result) {
|
public void onSuccess(String result) {
|
||||||
callbackHolder[0] = result;
|
callbackHolder[0] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable ex) {
|
public void onFailure(Throwable ex) {
|
||||||
fail("Expected onSuccess() to be called");
|
fail("Expected onSuccess() to be called");
|
||||||
|
@ -124,7 +131,6 @@ public class SettableListenableFutureTests {
|
||||||
public void onSuccess(String result) {
|
public void onSuccess(String result) {
|
||||||
fail("Expected onFailure() to be called");
|
fail("Expected onFailure() to be called");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable ex) {
|
public void onFailure(Throwable ex) {
|
||||||
callbackHolder[0] = ex;
|
callbackHolder[0] = ex;
|
||||||
|
@ -143,7 +149,6 @@ public class SettableListenableFutureTests {
|
||||||
public void onSuccess(String result) {
|
public void onSuccess(String result) {
|
||||||
fail("Expected onFailure() to be called");
|
fail("Expected onFailure() to be called");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Throwable ex) {
|
public void onFailure(Throwable ex) {
|
||||||
callbackHolder[0] = ex;
|
callbackHolder[0] = ex;
|
||||||
|
@ -169,7 +174,8 @@ public class SettableListenableFutureTests {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(20L);
|
Thread.sleep(20L);
|
||||||
settableListenableFuture.set(string);
|
settableListenableFuture.set(string);
|
||||||
} catch (InterruptedException ex) {
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,7 +189,8 @@ public class SettableListenableFutureTests {
|
||||||
try {
|
try {
|
||||||
settableListenableFuture.get(1L, TimeUnit.MILLISECONDS);
|
settableListenableFuture.get(1L, TimeUnit.MILLISECONDS);
|
||||||
fail("Expected TimeoutException");
|
fail("Expected TimeoutException");
|
||||||
} catch (TimeoutException ex) {
|
}
|
||||||
|
catch (TimeoutException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +204,8 @@ public class SettableListenableFutureTests {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(20L);
|
Thread.sleep(20L);
|
||||||
settableListenableFuture.set(string);
|
settableListenableFuture.set(string);
|
||||||
} catch (InterruptedException ex) {
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -278,7 +286,8 @@ public class SettableListenableFutureTests {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(20L);
|
Thread.sleep(20L);
|
||||||
settableListenableFuture.cancel(true);
|
settableListenableFuture.cancel(true);
|
||||||
} catch (InterruptedException ex) {
|
}
|
||||||
|
catch (InterruptedException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,7 +295,8 @@ public class SettableListenableFutureTests {
|
||||||
try {
|
try {
|
||||||
settableListenableFuture.get(100L, TimeUnit.MILLISECONDS);
|
settableListenableFuture.get(100L, TimeUnit.MILLISECONDS);
|
||||||
fail("Expected CancellationException");
|
fail("Expected CancellationException");
|
||||||
} catch (CancellationException ex) {
|
}
|
||||||
|
catch (CancellationException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -317,6 +327,7 @@ public class SettableListenableFutureTests {
|
||||||
verifyNoMoreInteractions(callback);
|
verifyNoMoreInteractions(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class InterruptableSettableListenableFuture extends SettableListenableFuture<String> {
|
private static class InterruptableSettableListenableFuture extends SettableListenableFuture<String> {
|
||||||
|
|
||||||
private boolean interrupted = false;
|
private boolean interrupted = false;
|
||||||
|
@ -330,4 +341,5 @@ public class SettableListenableFutureTests {
|
||||||
return interrupted;
|
return interrupted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue