Skip to content

Commit

Permalink
SettableListenableFuture properly rethrows Error
Browse files Browse the repository at this point in the history
Issue: SPR-14298
(cherry picked from commit a979885)
  • Loading branch information
jhoeller committed May 31, 2016
1 parent f64ed69 commit cc77588
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
* A {@link org.springframework.util.concurrent.ListenableFuture ListenableFuture}
Expand All @@ -49,11 +50,11 @@ public SettableListenableFuture() {


/**
* Set the value of this future. This method will return {@code true} if
* the value was set successfully, or {@code false} if the future has already
* been set or cancelled.
* @param value the value that will be set.
* @return {@code true} if the value was successfully set, else {@code false}.
* Set the value of this future. This method will return {@code true} if the
* value was set successfully, or {@code false} if the future has already been
* set or cancelled.
* @param value the value that will be set
* @return {@code true} if the value was successfully set, else {@code false}
*/
public boolean set(T value) {
boolean success = this.settableTask.setValue(value);
Expand All @@ -64,14 +65,14 @@ public boolean set(T value) {
}

/**
* Set the exception of this future. This method will return {@code true} if
* the exception was set successfully, or {@code false} if the future has already
* been set or cancelled.
* @param exception the value that will be set.
* @return {@code true} if the exception was successfully set, else {@code false}.
* Set the exception of this future. This method will return {@code true} if the
* exception was set successfully, or {@code false} if the future has already been
* set or cancelled.
* @param exception the value that will be set
* @return {@code true} if the exception was successfully set, else {@code false}
*/
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);
if (success) {
this.listenableFuture.run();
Expand Down Expand Up @@ -149,7 +150,7 @@ protected void interruptTask() {

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);

Expand All @@ -176,10 +177,11 @@ public void setCancelled() {
@SuppressWarnings("unchecked")
@Override
public T call() throws Exception {
if (value.get() instanceof Exception) {
throw (Exception) value.get();
Object val = this.value.get();
if (val instanceof Throwable) {
ReflectionUtils.rethrowException((Throwable) val);
}
return (T) value.get();
return (T) val;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.*;
Expand All @@ -35,12 +34,8 @@
@SuppressWarnings({ "rawtypes", "unchecked" })
public class SettableListenableFutureTests {

private SettableListenableFuture<String> settableListenableFuture;
private final SettableListenableFuture<String> settableListenableFuture = new SettableListenableFuture<String>();

@Before
public void setUp() {
settableListenableFuture = new SettableListenableFuture<String>();
}

@Test
public void validateInitialValues() {
Expand Down Expand Up @@ -76,6 +71,20 @@ public void throwsSetExceptionWrappedInExecutionException() throws ExecutionExce
}
}

@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
public void setValueTriggersCallback() {
String string = "hello";
Expand All @@ -85,7 +94,6 @@ public void setValueTriggersCallback() {
public void onSuccess(String result) {
callbackHolder[0] = result;
}

@Override
public void onFailure(Throwable ex) {
fail("Expected onSuccess() to be called");
Expand All @@ -104,7 +112,6 @@ public void setValueTriggersCallbackOnlyOnce() {
public void onSuccess(String result) {
callbackHolder[0] = result;
}

@Override
public void onFailure(Throwable ex) {
fail("Expected onSuccess() to be called");
Expand All @@ -124,7 +131,6 @@ public void setExceptionTriggersCallback() {
public void onSuccess(String result) {
fail("Expected onFailure() to be called");
}

@Override
public void onFailure(Throwable ex) {
callbackHolder[0] = ex;
Expand All @@ -143,7 +149,6 @@ public void setExceptionTriggersCallbackOnlyOnce() {
public void onSuccess(String result) {
fail("Expected onFailure() to be called");
}

@Override
public void onFailure(Throwable ex) {
callbackHolder[0] = ex;
Expand All @@ -169,7 +174,8 @@ public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
} catch (InterruptedException ex) {
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
Expand All @@ -183,7 +189,8 @@ public void getWithTimeoutThrowsTimeoutException() throws ExecutionException, In
try {
settableListenableFuture.get(1L, TimeUnit.MILLISECONDS);
fail("Expected TimeoutException");
} catch (TimeoutException ex) {
}
catch (TimeoutException ex) {
// expected
}
}
Expand All @@ -197,7 +204,8 @@ public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
} catch (InterruptedException ex) {
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
Expand Down Expand Up @@ -278,15 +286,17 @@ public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.cancel(true);
} catch (InterruptedException ex) {
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}).start();
try {
settableListenableFuture.get(100L, TimeUnit.MILLISECONDS);
fail("Expected CancellationException");
} catch (CancellationException ex) {
}
catch (CancellationException ex) {
// expected
}
}
Expand Down Expand Up @@ -317,6 +327,7 @@ public void cancelDoesNotNotifyCallbacksOnSetException() {
verifyNoMoreInteractions(callback);
}


private static class InterruptableSettableListenableFuture extends SettableListenableFuture<String> {

private boolean interrupted = false;
Expand All @@ -330,4 +341,5 @@ boolean calledInterruptTask() {
return interrupted;
}
}

}

0 comments on commit cc77588

Please sign in to comment.