Merge pull request #3259 from ahmedtawila/master

BAEL-1080 Introduction to Future in Vavr
This commit is contained in:
Loredana Crusoveanu 2017-12-31 09:47:31 +02:00 committed by GitHub
commit 393577b064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 27 deletions

View File

@ -15,86 +15,96 @@ import io.vavr.control.Option;
import io.vavr.control.Try; import io.vavr.control.Try;
public class FutureTest { public class FutureTest {
private static final String error = "Failed to get underlying value.";
@Test @Test
public void whenChangeExecutorService_thenCorrect() { public void whenChangeExecutorService_thenCorrect() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of( Future<String> resultFuture = Future.of(
Executors.newSingleThreadExecutor(), Executors.newSingleThreadExecutor(),
() -> Util.appendData(initialValue)); () -> Util.appendData(initialValue));
String result = resultFuture.get(); Thread.sleep(20);
String result = resultFuture.getOrElse(error);
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenAppendData_thenCorrect1() { public void whenAppendData_thenCorrect1() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)); Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
String result = resultFuture.get(); Thread.sleep(20);
String result = resultFuture.getOrElse(new String(error));
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenAppendData_thenCorrect2() { public void whenAppendData_thenCorrect2() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)); Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
Thread.sleep(20);
resultFuture.await(); resultFuture.await();
Option<Try<String>> futureOption = resultFuture.getValue(); Option<Try<String>> futureOption = resultFuture.getValue();
Try<String> futureTry = futureOption.get(); Try<String> futureTry = futureOption.get();
String result = futureTry.get(); String result = futureTry.getOrElse(error);
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenAppendData_thenSuccess() { public void whenAppendData_thenSuccess() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)) Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
String result = resultFuture.get(); Thread.sleep(20);
String result = resultFuture.getOrElse(error);
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenChainingCallbacks_thenCorrect() { public void whenChainingCallbacks_thenCorrect() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)) Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult)) .andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult)); .andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
String result = resultFuture.get(); Thread.sleep(20);
String result = resultFuture.getOrElse(error);
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenCallAwait_thenCorrect() { public void whenCallAwait_thenCorrect() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)); Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
Thread.sleep(20);
resultFuture = resultFuture.await(); resultFuture = resultFuture.await();
String result = resultFuture.get(); String result = resultFuture.getOrElse(error);
assertThat(result).isEqualTo("Welcome to Baeldung!"); assertThat(result).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenDivideByZero_thenGetThrowable1() { public void whenDivideByZero_thenGetThrowable1() throws InterruptedException {
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10)); Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
Thread.sleep(20);
Future<Throwable> throwableFuture = resultFuture.failed(); Future<Throwable> throwableFuture = resultFuture.failed();
Throwable throwable = throwableFuture.get(); Throwable throwable = throwableFuture.getOrElse(new Throwable());
assertThat(throwable.getMessage()).isEqualTo("/ by zero"); assertThat(throwable.getMessage()).isEqualTo("/ by zero");
} }
@Test @Test
public void whenDivideByZero_thenGetThrowable2() { public void whenDivideByZero_thenGetThrowable2() throws InterruptedException {
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10)); Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
Thread.sleep(20);
resultFuture.await(); resultFuture.await();
Option<Throwable> throwableOption = resultFuture.getCause(); Option<Throwable> throwableOption = resultFuture.getCause();
Throwable throwable = throwableOption.get(); Throwable throwable = throwableOption.getOrElse(new Throwable());
assertThat(throwable.getMessage()).isEqualTo("/ by zero"); assertThat(throwable.getMessage()).isEqualTo("/ by zero");
} }
@ -102,6 +112,7 @@ public class FutureTest {
@Test @Test
public void whenDivideByZero_thenCorrect() throws InterruptedException { public void whenDivideByZero_thenCorrect() throws InterruptedException {
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10)); Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
Thread.sleep(20);
resultFuture.await(); resultFuture.await();
assertThat(resultFuture.isCompleted()).isTrue(); assertThat(resultFuture.isCompleted()).isTrue();
@ -110,68 +121,76 @@ public class FutureTest {
} }
@Test @Test
public void whenAppendData_thenFutureNotEmpty() { public void whenAppendData_thenFutureNotEmpty() throws InterruptedException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)); Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
Thread.sleep(20);
resultFuture.await(); resultFuture.await();
assertThat(resultFuture.isEmpty()).isFalse(); assertThat(resultFuture.isEmpty()).isFalse();
} }
@Test @Test
public void whenCallZip_thenCorrect() { public void whenCallZip_thenCorrect() throws InterruptedException {
Future<Tuple2<String, Integer>> future = Future.of(() -> "John") Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
.zip(Future.of(() -> new Integer(5))); .zip(Future.of(() -> new Integer(5)));
Thread.sleep(20);
future.await(); future.await();
assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5))); assertThat(future.getOrElse(new Tuple2<String, Integer>(error, 0)))
.isEqualTo(Tuple.of("John", new Integer(5)));
} }
@Test @Test
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException { public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
String initialValue = "Welcome to "; String initialValue = "Welcome to ";
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue)); Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
Thread.sleep(20);
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture(); CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!"); assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!");
} }
@Test @Test
public void whenCallMap_thenCorrect() { public void whenCallMap_thenCorrect() throws InterruptedException {
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung")) Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
.map(a -> "Hello " + a); .map(a -> "Hello " + a);
Thread.sleep(20);
futureResult.await(); futureResult.await();
assertThat(futureResult.get()).isEqualTo("Hello from Baeldung"); assertThat(futureResult.getOrElse(error)).isEqualTo("Hello from Baeldung");
} }
@Test @Test
public void whenFutureFails_thenGetErrorMessage() { public void whenFutureFails_thenGetErrorMessage() throws InterruptedException {
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
Thread.sleep(20);
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage); Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
String errorMessage = errorMessageFuture.get(); String errorMessage = errorMessageFuture.getOrElse(error);
assertThat(errorMessage).isEqualTo("String index out of range: -1"); assertThat(errorMessage).isEqualTo("String index out of range: -1");
} }
@Test @Test
public void whenFutureFails_thenGetAnotherFuture() { public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException {
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
Thread.sleep(20);
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage)); Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
String errorMessage = errorMessageFuture.get(); String errorMessage = errorMessageFuture.getOrElse(error);
assertThat(errorMessage).isEqualTo("String index out of range: -1"); assertThat(errorMessage).isEqualTo("String index out of range: -1");
} }
@Test @Test
public void whenBothFuturesFail_thenGetErrorMessage() { public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException {
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello")); Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello")); Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
Thread.sleep(20);
Future<String> errorMessageFuture = future1.fallbackTo(future2); Future<String> errorMessageFuture = future1.fallbackTo(future2);
Future<Throwable> errorMessage = errorMessageFuture.failed(); Future<Throwable> errorMessage = errorMessageFuture.failed();
assertThat( assertThat(
errorMessage.get().getMessage()) errorMessage.getOrElse(new Throwable()).getMessage())
.isEqualTo("String index out of range: -1"); .isEqualTo("String index out of range: -1");
} }
} }