Refactor Vavr Future (#3349)
This commit is contained in:
parent
b54440ddb3
commit
2afc7dc623
|
@ -1,20 +0,0 @@
|
|||
package com.baeldung.vavr.future;
|
||||
|
||||
public class Util {
|
||||
|
||||
public static String appendData(String initial) {
|
||||
return initial + "Baeldung!";
|
||||
}
|
||||
|
||||
public static int divideByZero(int num) {
|
||||
return num / 0;
|
||||
}
|
||||
|
||||
public static String getSubstringMinusOne(String s) {
|
||||
return s.substring(-1);
|
||||
}
|
||||
|
||||
public static String getSubstringMinusTwo(String s) {
|
||||
return s.substring(-2);
|
||||
}
|
||||
}
|
|
@ -1,119 +1,102 @@
|
|||
package com.baeldung.vavr.future;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.vavr.Tuple;
|
||||
import io.vavr.Tuple2;
|
||||
import io.vavr.concurrent.Future;
|
||||
import io.vavr.control.Option;
|
||||
import io.vavr.control.Try;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static java.util.concurrent.Executors.newSingleThreadExecutor;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class FutureTest {
|
||||
|
||||
|
||||
private static final String error = "Failed to get underlying value.";
|
||||
private static final String HELLO = "Welcome to Baeldung!";
|
||||
|
||||
@Test
|
||||
public void whenChangeExecutorService_thenCorrect() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(
|
||||
Executors.newSingleThreadExecutor(),
|
||||
() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
String result = resultFuture.getOrElse(error);
|
||||
public void whenChangeExecutorService_thenCorrect() {
|
||||
String result = Future.of(newSingleThreadExecutor(), () -> HELLO)
|
||||
.getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(result)
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenCorrect1() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
String result = resultFuture.getOrElse(new String(error));
|
||||
public void whenAppendData_thenCorrect1() {
|
||||
String result = Future.of(() -> HELLO)
|
||||
.getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(result)
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenCorrect2() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
resultFuture.await();
|
||||
public void whenAppendData_thenCorrect2() {
|
||||
Future<String> resultFuture = Future.of(() -> HELLO)
|
||||
.await();
|
||||
|
||||
Option<Try<String>> futureOption = resultFuture.getValue();
|
||||
Try<String> futureTry = futureOption.get();
|
||||
String result = futureTry.getOrElse(error);
|
||||
String result = futureOption.get().getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(result)
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenSuccess() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
|
||||
public void whenAppendData_thenSuccess() {
|
||||
String result = Future.of(() -> HELLO)
|
||||
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
|
||||
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
|
||||
Thread.sleep(20);
|
||||
String result = resultFuture.getOrElse(error);
|
||||
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult))
|
||||
.getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(result)
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChainingCallbacks_thenCorrect() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
|
||||
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
|
||||
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
|
||||
Thread.sleep(20);
|
||||
String result = resultFuture.getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
public void whenChainingCallbacks_thenCorrect() {
|
||||
Future.of(() -> HELLO)
|
||||
.andThen(r -> System.out.println("Completed - 1: " + r))
|
||||
.andThen(r -> System.out.println("Completed - 2: " + r));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallAwait_thenCorrect() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
resultFuture = resultFuture.await();
|
||||
String result = resultFuture.getOrElse(error);
|
||||
public void whenCallAwait_thenCorrect() {
|
||||
Future<String> resultFuture = Future.of(() -> HELLO)
|
||||
.await();
|
||||
String result = resultFuture.getValue().get().getOrElse(error);
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(result)
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenGetThrowable1() throws InterruptedException {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
Thread.sleep(20);
|
||||
Future<Throwable> throwableFuture = resultFuture.failed();
|
||||
Throwable throwable = throwableFuture.getOrElse(new Throwable());
|
||||
public void whenDivideByZero_thenGetThrowable1() {
|
||||
Future<Integer> resultFuture = Future.of(() -> 10 / 0);
|
||||
|
||||
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
|
||||
assertThatThrownBy(resultFuture::get)
|
||||
.isInstanceOf(ArithmeticException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenGetThrowable2() throws InterruptedException {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
Thread.sleep(20);
|
||||
resultFuture.await();
|
||||
Option<Throwable> throwableOption = resultFuture.getCause();
|
||||
Throwable throwable = throwableOption.getOrElse(new Throwable());
|
||||
public void whenDivideByZero_thenGetThrowable2() {
|
||||
Future<Integer> resultFuture = Future.of(() -> 10 / 0)
|
||||
.await();
|
||||
|
||||
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
|
||||
assertThat(resultFuture.getCause().get().getMessage())
|
||||
.isEqualTo("/ by zero");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenCorrect() throws InterruptedException {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
Thread.sleep(20);
|
||||
resultFuture.await();
|
||||
public void whenDivideByZero_thenCorrect() {
|
||||
Future<Integer> resultFuture = Future.of(() -> 10 / 0)
|
||||
.await();
|
||||
|
||||
assertThat(resultFuture.isCompleted()).isTrue();
|
||||
assertThat(resultFuture.isSuccess()).isFalse();
|
||||
|
@ -121,76 +104,70 @@ public class FutureTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenFutureNotEmpty() throws InterruptedException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
resultFuture.await();
|
||||
public void whenAppendData_thenFutureNotEmpty() {
|
||||
Future<String> resultFuture = Future.of(() -> HELLO)
|
||||
.await();
|
||||
|
||||
assertThat(resultFuture.isEmpty()).isFalse();
|
||||
assertThat(resultFuture.isEmpty())
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallZip_thenCorrect() throws InterruptedException {
|
||||
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
|
||||
.zip(Future.of(() -> new Integer(5)));
|
||||
Thread.sleep(20);
|
||||
future.await();
|
||||
public void whenCallZip_thenCorrect() {
|
||||
Future<String> f1 = Future.of(() -> "hello1");
|
||||
Future<String> f2 = Future.of(() -> "hello2");
|
||||
|
||||
assertThat(future.getOrElse(new Tuple2<String, Integer>(error, 0)))
|
||||
.isEqualTo(Tuple.of("John", new Integer(5)));
|
||||
assertThat(f1.zip(f2).get())
|
||||
.isEqualTo(Tuple.of("hello1", "hello2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
Thread.sleep(20);
|
||||
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
|
||||
CompletableFuture<String> convertedFuture = Future.of(() -> HELLO)
|
||||
.toCompletableFuture();
|
||||
|
||||
assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!");
|
||||
assertThat(convertedFuture.get())
|
||||
.isEqualTo(HELLO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallMap_thenCorrect() throws InterruptedException {
|
||||
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
|
||||
.map(a -> "Hello " + a);
|
||||
Thread.sleep(20);
|
||||
futureResult.await();
|
||||
public void whenCallMap_thenCorrect() {
|
||||
Future<String> futureResult = Future.of(() -> "from Baeldung")
|
||||
.map(a -> "Hello " + a)
|
||||
.await();
|
||||
|
||||
assertThat(futureResult.getOrElse(error)).isEqualTo("Hello from Baeldung");
|
||||
assertThat(futureResult.get())
|
||||
.isEqualTo("Hello from Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureFails_thenGetErrorMessage() throws InterruptedException {
|
||||
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Thread.sleep(20);
|
||||
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
|
||||
String errorMessage = errorMessageFuture.getOrElse(error);
|
||||
public void whenFutureFails_thenGetErrorMessage() {
|
||||
Future<String> future = Future.of(() -> "Hello".substring(-1))
|
||||
.recover(x -> "fallback value");
|
||||
|
||||
assertThat(errorMessage).isEqualTo("String index out of range: -1");
|
||||
assertThat(future.get())
|
||||
.isEqualTo("fallback value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException {
|
||||
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Thread.sleep(20);
|
||||
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
|
||||
String errorMessage = errorMessageFuture.getOrElse(error);
|
||||
public void whenFutureFails_thenGetAnotherFuture() {
|
||||
Future<String> future = Future.of(() -> "Hello".substring(-1))
|
||||
.recoverWith(x -> Future.of(() -> "fallback value"));
|
||||
|
||||
assertThat(errorMessage).isEqualTo("String index out of range: -1");
|
||||
assertThat(future.get())
|
||||
.isEqualTo("fallback value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException {
|
||||
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
|
||||
Thread.sleep(20);
|
||||
Future<String> errorMessageFuture = future1.fallbackTo(future2);
|
||||
public void whenBothFuturesFail_thenGetErrorMessage() {
|
||||
Future<String> f1 = Future.of(() -> "Hello".substring(-1));
|
||||
Future<String> f2 = Future.of(() -> "Hello".substring(-2));
|
||||
|
||||
Future<String> errorMessageFuture = f1.fallbackTo(f2);
|
||||
Future<Throwable> errorMessage = errorMessageFuture.failed();
|
||||
|
||||
|
||||
assertThat(
|
||||
errorMessage.getOrElse(new Throwable()).getMessage())
|
||||
errorMessage.get().getMessage())
|
||||
.isEqualTo("String index out of range: -1");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue