Remove APIs marked as deprecated for removal
Closes gh-33809
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -78,7 +78,7 @@ class FilePatternResourceHintsRegistrarTests {
|
||||
|
||||
@Test
|
||||
void registerWithMultipleClasspathLocations() {
|
||||
FilePatternResourceHintsRegistrar.forClassPathLocations("").withClasspathLocations("META-INF")
|
||||
FilePatternResourceHintsRegistrar.forClassPathLocations("").withClassPathLocations("META-INF")
|
||||
.withFilePrefixes("test").withFileExtensions(".txt")
|
||||
.registerHints(this.hints, null);
|
||||
assertThat(this.hints.resourcePatternHints()).singleElement()
|
||||
@@ -133,7 +133,7 @@ class FilePatternResourceHintsRegistrarTests {
|
||||
@Test
|
||||
void registerWithNonExistingLocationDoesNotRegisterHint() {
|
||||
FilePatternResourceHintsRegistrar.forClassPathLocations("does-not-exist/")
|
||||
.withClasspathLocations("another-does-not-exist/")
|
||||
.withClassPathLocations("another-does-not-exist/")
|
||||
.withFilePrefixes("test").withFileExtensions(".txt")
|
||||
.registerHints(this.hints, null);
|
||||
assertThat(this.hints.resourcePatternHints()).isEmpty();
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util.concurrent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@SuppressWarnings({"deprecation", "removal"})
|
||||
class ListenableFutureTaskTests {
|
||||
|
||||
@Test
|
||||
void success() throws Exception {
|
||||
final String s = "Hello World";
|
||||
Callable<String> callable = () -> s;
|
||||
|
||||
ListenableFutureTask<String> task = new ListenableFutureTask<>(callable);
|
||||
task.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
assertThat(result).isEqualTo(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
throw new AssertionError(ex.getMessage(), ex);
|
||||
}
|
||||
});
|
||||
task.run();
|
||||
|
||||
assertThat(task.get()).isSameAs(s);
|
||||
assertThat(task.completable().get()).isSameAs(s);
|
||||
task.completable().thenAccept(v -> assertThat(v).isSameAs(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failure() {
|
||||
final String s = "Hello World";
|
||||
Callable<String> callable = () -> {
|
||||
throw new IOException(s);
|
||||
};
|
||||
|
||||
ListenableFutureTask<String> task = new ListenableFutureTask<>(callable);
|
||||
task.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
fail("onSuccess not expected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
assertThat(ex.getMessage()).isEqualTo(s);
|
||||
}
|
||||
});
|
||||
task.run();
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class)
|
||||
.isThrownBy(task::get)
|
||||
.havingCause()
|
||||
.withMessage(s);
|
||||
assertThatExceptionOfType(ExecutionException.class)
|
||||
.isThrownBy(task.completable()::get)
|
||||
.havingCause()
|
||||
.withMessage(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
void successWithLambdas() throws Exception {
|
||||
final String s = "Hello World";
|
||||
Callable<String> callable = () -> s;
|
||||
|
||||
SuccessCallback<String> successCallback = mock();
|
||||
FailureCallback failureCallback = mock();
|
||||
ListenableFutureTask<String> task = new ListenableFutureTask<>(callable);
|
||||
task.addCallback(successCallback, failureCallback);
|
||||
task.run();
|
||||
verify(successCallback).onSuccess(s);
|
||||
verifyNoInteractions(failureCallback);
|
||||
|
||||
assertThat(task.get()).isSameAs(s);
|
||||
assertThat(task.completable().get()).isSameAs(s);
|
||||
task.completable().thenAccept(v -> assertThat(v).isSameAs(s));
|
||||
}
|
||||
|
||||
@Test
|
||||
void failureWithLambdas() {
|
||||
final String s = "Hello World";
|
||||
IOException ex = new IOException(s);
|
||||
Callable<String> callable = () -> {
|
||||
throw ex;
|
||||
};
|
||||
|
||||
SuccessCallback<String> successCallback = mock();
|
||||
FailureCallback failureCallback = mock();
|
||||
ListenableFutureTask<String> task = new ListenableFutureTask<>(callable);
|
||||
task.addCallback(successCallback, failureCallback);
|
||||
task.run();
|
||||
verify(failureCallback).onFailure(ex);
|
||||
verifyNoInteractions(successCallback);
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class)
|
||||
.isThrownBy(task::get)
|
||||
.havingCause()
|
||||
.withMessage(s);
|
||||
assertThatExceptionOfType(ExecutionException.class)
|
||||
.isThrownBy(task.completable()::get)
|
||||
.havingCause()
|
||||
.withMessage(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util.concurrent;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MonoToListenableFutureAdapter}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
@SuppressWarnings({"deprecation", "removal"})
|
||||
class MonoToListenableFutureAdapterTests {
|
||||
|
||||
@Test
|
||||
void success() {
|
||||
String expected = "one";
|
||||
AtomicReference<Object> actual = new AtomicReference<>();
|
||||
ListenableFuture<String> future = new MonoToListenableFutureAdapter<>(Mono.just(expected));
|
||||
future.addCallback(actual::set, actual::set);
|
||||
|
||||
assertThat(actual.get()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void failure() {
|
||||
Throwable expected = new IllegalStateException("oops");
|
||||
AtomicReference<Object> actual = new AtomicReference<>();
|
||||
ListenableFuture<String> future = new MonoToListenableFutureAdapter<>(Mono.error(expected));
|
||||
future.addCallback(actual::set, actual::set);
|
||||
|
||||
assertThat(actual.get()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellation() {
|
||||
Mono<Long> mono = Mono.delay(Duration.ofSeconds(60));
|
||||
Future<Long> future = new MonoToListenableFutureAdapter<>(mono);
|
||||
|
||||
assertThat(future.cancel(true)).isTrue();
|
||||
assertThat(future.isCancelled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellationAfterTerminated() {
|
||||
Future<Void> future = new MonoToListenableFutureAdapter<>(Mono.empty());
|
||||
|
||||
assertThat(future.cancel(true)).as("Should return false if task already completed").isFalse();
|
||||
assertThat(future.isCancelled()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,414 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util.concurrent;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* @author Mattias Severson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@SuppressWarnings({"deprecation", "removal"})
|
||||
class SettableListenableFutureTests {
|
||||
|
||||
private final SettableListenableFuture<String> settableListenableFuture = new SettableListenableFuture<>();
|
||||
|
||||
|
||||
@Test
|
||||
void validateInitialValues() {
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsSetValue() throws ExecutionException, InterruptedException {
|
||||
String string = "hello";
|
||||
assertThat(settableListenableFuture.set(string)).isTrue();
|
||||
assertThat(settableListenableFuture.get()).isEqualTo(string);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsSetValueFromCompletable() throws ExecutionException, InterruptedException {
|
||||
String string = "hello";
|
||||
assertThat(settableListenableFuture.set(string)).isTrue();
|
||||
Future<String> completable = settableListenableFuture.completable();
|
||||
assertThat(completable.get()).isEqualTo(string);
|
||||
assertThat(completable.isCancelled()).isFalse();
|
||||
assertThat(completable.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setValueUpdatesDoneStatus() {
|
||||
settableListenableFuture.set("hello");
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsSetExceptionWrappedInExecutionException() {
|
||||
Throwable exception = new RuntimeException();
|
||||
assertThat(settableListenableFuture.setException(exception)).isTrue();
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class).isThrownBy(
|
||||
settableListenableFuture::get)
|
||||
.withCause(exception);
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsSetExceptionWrappedInExecutionExceptionFromCompletable() {
|
||||
Throwable exception = new RuntimeException();
|
||||
assertThat(settableListenableFuture.setException(exception)).isTrue();
|
||||
Future<String> completable = settableListenableFuture.completable();
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class).isThrownBy(
|
||||
completable::get)
|
||||
.withCause(exception);
|
||||
|
||||
assertThat(completable.isCancelled()).isFalse();
|
||||
assertThat(completable.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsSetErrorWrappedInExecutionException() {
|
||||
Throwable exception = new OutOfMemoryError();
|
||||
assertThat(settableListenableFuture.setException(exception)).isTrue();
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class).isThrownBy(
|
||||
settableListenableFuture::get)
|
||||
.withCause(exception);
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsSetErrorWrappedInExecutionExceptionFromCompletable() {
|
||||
Throwable exception = new OutOfMemoryError();
|
||||
assertThat(settableListenableFuture.setException(exception)).isTrue();
|
||||
Future<String> completable = settableListenableFuture.completable();
|
||||
|
||||
assertThatExceptionOfType(ExecutionException.class).isThrownBy(
|
||||
completable::get)
|
||||
.withCause(exception);
|
||||
|
||||
assertThat(completable.isCancelled()).isFalse();
|
||||
assertThat(completable.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setValueTriggersCallback() {
|
||||
String string = "hello";
|
||||
final String[] callbackHolder = new String[1];
|
||||
|
||||
settableListenableFuture.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
callbackHolder[0] = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
throw new AssertionError("Expected onSuccess() to be called", ex);
|
||||
}
|
||||
});
|
||||
|
||||
settableListenableFuture.set(string);
|
||||
assertThat(callbackHolder[0]).isEqualTo(string);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setValueTriggersCallbackOnlyOnce() {
|
||||
String string = "hello";
|
||||
final String[] callbackHolder = new String[1];
|
||||
|
||||
settableListenableFuture.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
callbackHolder[0] = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
throw new AssertionError("Expected onSuccess() to be called", ex);
|
||||
}
|
||||
});
|
||||
|
||||
settableListenableFuture.set(string);
|
||||
assertThat(settableListenableFuture.set("good bye")).isFalse();
|
||||
assertThat(callbackHolder[0]).isEqualTo(string);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setExceptionTriggersCallback() {
|
||||
Throwable exception = new RuntimeException();
|
||||
final Throwable[] callbackHolder = new Throwable[1];
|
||||
|
||||
settableListenableFuture.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
fail("Expected onFailure() to be called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
callbackHolder[0] = ex;
|
||||
}
|
||||
});
|
||||
|
||||
settableListenableFuture.setException(exception);
|
||||
assertThat(callbackHolder[0]).isEqualTo(exception);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setExceptionTriggersCallbackOnlyOnce() {
|
||||
Throwable exception = new RuntimeException();
|
||||
final Throwable[] callbackHolder = new Throwable[1];
|
||||
|
||||
settableListenableFuture.addCallback(new ListenableFutureCallback<>() {
|
||||
@Override
|
||||
public void onSuccess(String result) {
|
||||
fail("Expected onFailure() to be called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
callbackHolder[0] = ex;
|
||||
}
|
||||
});
|
||||
|
||||
settableListenableFuture.setException(exception);
|
||||
assertThat(settableListenableFuture.setException(new IllegalArgumentException())).isFalse();
|
||||
assertThat(callbackHolder[0]).isEqualTo(exception);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullIsAcceptedAsValueToSet() throws ExecutionException, InterruptedException {
|
||||
settableListenableFuture.set(null);
|
||||
assertThat(settableListenableFuture.get()).isNull();
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWaitsForCompletion() throws ExecutionException, InterruptedException {
|
||||
final String string = "hello";
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(20L);
|
||||
settableListenableFuture.set(string);
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}).start();
|
||||
|
||||
String value = settableListenableFuture.get();
|
||||
assertThat(value).isEqualTo(string);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWithTimeoutThrowsTimeoutException() {
|
||||
assertThatExceptionOfType(TimeoutException.class).isThrownBy(() ->
|
||||
settableListenableFuture.get(1L, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWithTimeoutWaitsForCompletion() throws ExecutionException, InterruptedException, TimeoutException {
|
||||
final String string = "hello";
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(20L);
|
||||
settableListenableFuture.set(string);
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}).start();
|
||||
|
||||
String value = settableListenableFuture.get(500L, TimeUnit.MILLISECONDS);
|
||||
assertThat(value).isEqualTo(string);
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelPreventsValueFromBeingSet() {
|
||||
assertThat(settableListenableFuture.cancel(true)).isTrue();
|
||||
assertThat(settableListenableFuture.set("hello")).isFalse();
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelSetsFutureToDone() {
|
||||
settableListenableFuture.cancel(true);
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelWithMayInterruptIfRunningTrueCallsOverriddenMethod() {
|
||||
InterruptibleSettableListenableFuture interruptibleFuture = new InterruptibleSettableListenableFuture();
|
||||
assertThat(interruptibleFuture.cancel(true)).isTrue();
|
||||
assertThat(interruptibleFuture.calledInterruptTask()).isTrue();
|
||||
assertThat(interruptibleFuture.isCancelled()).isTrue();
|
||||
assertThat(interruptibleFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelWithMayInterruptIfRunningFalseDoesNotCallOverriddenMethod() {
|
||||
InterruptibleSettableListenableFuture interruptibleFuture = new InterruptibleSettableListenableFuture();
|
||||
assertThat(interruptibleFuture.cancel(false)).isTrue();
|
||||
assertThat(interruptibleFuture.calledInterruptTask()).isFalse();
|
||||
assertThat(interruptibleFuture.isCancelled()).isTrue();
|
||||
assertThat(interruptibleFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setPreventsCancel() {
|
||||
assertThat(settableListenableFuture.set("hello")).isTrue();
|
||||
assertThat(settableListenableFuture.cancel(true)).isFalse();
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelPreventsExceptionFromBeingSet() {
|
||||
assertThat(settableListenableFuture.cancel(true)).isTrue();
|
||||
assertThat(settableListenableFuture.setException(new RuntimeException())).isFalse();
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setExceptionPreventsCancel() {
|
||||
assertThat(settableListenableFuture.setException(new RuntimeException())).isTrue();
|
||||
assertThat(settableListenableFuture.cancel(true)).isFalse();
|
||||
assertThat(settableListenableFuture.isCancelled()).isFalse();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelStateThrowsExceptionWhenCallingGet() {
|
||||
settableListenableFuture.cancel(true);
|
||||
|
||||
assertThatExceptionOfType(CancellationException.class).isThrownBy(settableListenableFuture::get);
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancelStateThrowsExceptionWhenCallingGetWithTimeout() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(20L);
|
||||
settableListenableFuture.cancel(true);
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}).start();
|
||||
|
||||
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
|
||||
settableListenableFuture.get(500L, TimeUnit.MILLISECONDS));
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void cancelDoesNotNotifyCallbacksOnSet() {
|
||||
ListenableFutureCallback callback = mock();
|
||||
settableListenableFuture.addCallback(callback);
|
||||
settableListenableFuture.cancel(true);
|
||||
|
||||
verify(callback).onFailure(any(CancellationException.class));
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
settableListenableFuture.set("hello");
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void cancelDoesNotNotifyCallbacksOnSetException() {
|
||||
ListenableFutureCallback callback = mock();
|
||||
settableListenableFuture.addCallback(callback);
|
||||
settableListenableFuture.cancel(true);
|
||||
|
||||
verify(callback).onFailure(any(CancellationException.class));
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
settableListenableFuture.setException(new RuntimeException());
|
||||
verifyNoMoreInteractions(callback);
|
||||
|
||||
assertThat(settableListenableFuture.isCancelled()).isTrue();
|
||||
assertThat(settableListenableFuture.isDone()).isTrue();
|
||||
}
|
||||
|
||||
|
||||
private static class InterruptibleSettableListenableFuture extends SettableListenableFuture<String> {
|
||||
|
||||
private boolean interrupted = false;
|
||||
|
||||
@Override
|
||||
protected void interruptTask() {
|
||||
interrupted = true;
|
||||
}
|
||||
|
||||
boolean calledInterruptTask() {
|
||||
return interrupted;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user