Future in Vavr
This commit is contained in:
parent
fec5927d90
commit
c489044f0b
|
@ -9,4 +9,12 @@ public class Util {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ 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;
|
||||
|
||||
|
@ -15,6 +16,17 @@ import io.vavr.control.Try;
|
|||
|
||||
public class FutureTest {
|
||||
|
||||
@Test
|
||||
public void whenChangeExecutorService_thenCorrect() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(
|
||||
Executors.newSingleThreadExecutor(),
|
||||
() -> Util.appendData(initialValue));
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenCorrect1() {
|
||||
String initialValue = "Welcome to ";
|
||||
|
@ -40,8 +52,8 @@ public class FutureTest {
|
|||
public void whenAppendData_thenSuccess() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
|
||||
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
|
||||
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
|
||||
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
|
||||
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
|
@ -51,8 +63,8 @@ public class FutureTest {
|
|||
public void whenChainingCallbacks_thenCorrect() {
|
||||
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));
|
||||
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
|
||||
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertThat(result).isEqualTo("Welcome to Baeldung!");
|
||||
|
@ -109,14 +121,14 @@ public class FutureTest {
|
|||
@Test
|
||||
public void whenCallZip_thenCorrect() {
|
||||
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
|
||||
.zip(Future.of(() -> new Integer(5)));
|
||||
.zip(Future.of(() -> new Integer(5)));
|
||||
future.await();
|
||||
|
||||
assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenFutureNotEmptyd() throws InterruptedException, ExecutionException {
|
||||
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
|
||||
|
@ -127,9 +139,39 @@ public class FutureTest {
|
|||
@Test
|
||||
public void whenCallMap_thenCorrect() {
|
||||
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
|
||||
.map(a -> "Hello " + a);
|
||||
.map(a -> "Hello " + a);
|
||||
futureResult.await();
|
||||
|
||||
assertThat(futureResult.get()).isEqualTo("Hello from Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureFails_thenGetErrorMessage() {
|
||||
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
|
||||
String errorMessage = errorMessageFuture.get();
|
||||
|
||||
assertThat(errorMessage).isEqualTo("String index out of range: -1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFutureFails_thenGetAnotherFuture() {
|
||||
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
|
||||
String errorMessage = errorMessageFuture.get();
|
||||
|
||||
assertThat(errorMessage).isEqualTo("String index out of range: -1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBothFuturesFail_thenGetErrorMessage() {
|
||||
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
|
||||
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
|
||||
Future<String> errorMessageFuture = future1.fallbackTo(future2);
|
||||
Future<Throwable> errorMessage = errorMessageFuture.failed();
|
||||
|
||||
assertThat(
|
||||
errorMessage.get().getMessage())
|
||||
.isEqualTo("String index out of range: -1");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue