From 1c6ece18d92f048e6263e79f8f04a60860f982c6 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Sat, 16 Dec 2017 21:33:15 +0200 Subject: [PATCH 1/2] wait for some time before retrieving values --- .../com/baeldung/vavr/future/FutureTest.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java index 1f2a3761eb..2d2e6939f1 100644 --- a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java @@ -17,29 +17,32 @@ import io.vavr.control.Try; public class FutureTest { @Test - public void whenChangeExecutorService_thenCorrect() { + public void whenChangeExecutorService_thenCorrect() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of( Executors.newSingleThreadExecutor(), () -> Util.appendData(initialValue)); + Thread.sleep(20); String result = resultFuture.get(); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @Test - public void whenAppendData_thenCorrect1() { + public void whenAppendData_thenCorrect1() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); + Thread.sleep(20); String result = resultFuture.get(); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @Test - public void whenAppendData_thenCorrect2() { + public void whenAppendData_thenCorrect2() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); + Thread.sleep(20); resultFuture.await(); Option> futureOption = resultFuture.getValue(); Try futureTry = futureOption.get(); @@ -49,31 +52,34 @@ public class FutureTest { } @Test - public void whenAppendData_thenSuccess() { + public void whenAppendData_thenSuccess() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)) .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); + Thread.sleep(20); String result = resultFuture.get(); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @Test - public void whenChainingCallbacks_thenCorrect() { + public void whenChainingCallbacks_thenCorrect() throws InterruptedException { String initialValue = "Welcome to "; Future 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.get(); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @Test - public void whenCallAwait_thenCorrect() { + public void whenCallAwait_thenCorrect() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); + Thread.sleep(20); resultFuture = resultFuture.await(); String result = resultFuture.get(); @@ -81,8 +87,9 @@ public class FutureTest { } @Test - public void whenDivideByZero_thenGetThrowable1() { + public void whenDivideByZero_thenGetThrowable1() throws InterruptedException { Future resultFuture = Future.of(() -> Util.divideByZero(10)); + Thread.sleep(20); Future throwableFuture = resultFuture.failed(); Throwable throwable = throwableFuture.get(); @@ -90,8 +97,9 @@ public class FutureTest { } @Test - public void whenDivideByZero_thenGetThrowable2() { + public void whenDivideByZero_thenGetThrowable2() throws InterruptedException { Future resultFuture = Future.of(() -> Util.divideByZero(10)); + Thread.sleep(20); resultFuture.await(); Option throwableOption = resultFuture.getCause(); Throwable throwable = throwableOption.get(); @@ -102,6 +110,7 @@ public class FutureTest { @Test public void whenDivideByZero_thenCorrect() throws InterruptedException { Future resultFuture = Future.of(() -> Util.divideByZero(10)); + Thread.sleep(20); resultFuture.await(); assertThat(resultFuture.isCompleted()).isTrue(); @@ -110,18 +119,20 @@ public class FutureTest { } @Test - public void whenAppendData_thenFutureNotEmpty() { + public void whenAppendData_thenFutureNotEmpty() throws InterruptedException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); + Thread.sleep(20); resultFuture.await(); assertThat(resultFuture.isEmpty()).isFalse(); } @Test - public void whenCallZip_thenCorrect() { + public void whenCallZip_thenCorrect() throws InterruptedException { Future> future = Future.of(() -> "John") .zip(Future.of(() -> new Integer(5))); + Thread.sleep(20); future.await(); assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5))); @@ -131,23 +142,26 @@ public class FutureTest { public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); + Thread.sleep(20); CompletableFuture convertedFuture = resultFuture.toCompletableFuture(); assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!"); } @Test - public void whenCallMap_thenCorrect() { + public void whenCallMap_thenCorrect() throws InterruptedException { Future futureResult = Future.of(() -> new StringBuilder("from Baeldung")) .map(a -> "Hello " + a); + Thread.sleep(20); futureResult.await(); assertThat(futureResult.get()).isEqualTo("Hello from Baeldung"); } @Test - public void whenFutureFails_thenGetErrorMessage() { + public void whenFutureFails_thenGetErrorMessage() throws InterruptedException { Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); + Thread.sleep(20); Future errorMessageFuture = resultFuture.recover(Throwable::getMessage); String errorMessage = errorMessageFuture.get(); @@ -155,8 +169,9 @@ public class FutureTest { } @Test - public void whenFutureFails_thenGetAnotherFuture() { + public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException { Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); + Thread.sleep(20); Future errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage)); String errorMessage = errorMessageFuture.get(); @@ -164,9 +179,10 @@ public class FutureTest { } @Test - public void whenBothFuturesFail_thenGetErrorMessage() { + public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException { Future future1 = Future.of(() -> Util.getSubstringMinusOne("Hello")); Future future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello")); + Thread.sleep(20); Future errorMessageFuture = future1.fallbackTo(future2); Future errorMessage = errorMessageFuture.failed(); From ff2cf57772a8317f857d9d250986475660d310a9 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Sat, 30 Dec 2017 23:23:08 +0200 Subject: [PATCH 2/2] BAEL-1080 Introduction to Future in Vavr --- .../com/baeldung/vavr/future/FutureTest.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java index 2d2e6939f1..dd678bcad0 100644 --- a/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java +++ b/vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java @@ -15,6 +15,8 @@ import io.vavr.control.Option; import io.vavr.control.Try; public class FutureTest { + + private static final String error = "Failed to get underlying value."; @Test public void whenChangeExecutorService_thenCorrect() throws InterruptedException { @@ -23,7 +25,7 @@ public class FutureTest { Executors.newSingleThreadExecutor(), () -> Util.appendData(initialValue)); Thread.sleep(20); - String result = resultFuture.get(); + String result = resultFuture.getOrElse(error); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -33,7 +35,7 @@ public class FutureTest { String initialValue = "Welcome to "; Future resultFuture = Future.of(() -> Util.appendData(initialValue)); Thread.sleep(20); - String result = resultFuture.get(); + String result = resultFuture.getOrElse(new String(error)); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -46,7 +48,7 @@ public class FutureTest { resultFuture.await(); Option> futureOption = resultFuture.getValue(); Try futureTry = futureOption.get(); - String result = futureTry.get(); + String result = futureTry.getOrElse(error); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -58,7 +60,7 @@ public class FutureTest { .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); Thread.sleep(20); - String result = resultFuture.get(); + String result = resultFuture.getOrElse(error); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -70,7 +72,7 @@ public class FutureTest { .andThen(finalResult -> System.out.println("Completed - 1: " + finalResult)) .andThen(finalResult -> System.out.println("Completed - 2: " + finalResult)); Thread.sleep(20); - String result = resultFuture.get(); + String result = resultFuture.getOrElse(error); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -81,7 +83,7 @@ public class FutureTest { Future resultFuture = Future.of(() -> Util.appendData(initialValue)); Thread.sleep(20); resultFuture = resultFuture.await(); - String result = resultFuture.get(); + String result = resultFuture.getOrElse(error); assertThat(result).isEqualTo("Welcome to Baeldung!"); } @@ -91,7 +93,7 @@ public class FutureTest { Future resultFuture = Future.of(() -> Util.divideByZero(10)); Thread.sleep(20); Future throwableFuture = resultFuture.failed(); - Throwable throwable = throwableFuture.get(); + Throwable throwable = throwableFuture.getOrElse(new Throwable()); assertThat(throwable.getMessage()).isEqualTo("/ by zero"); } @@ -102,7 +104,7 @@ public class FutureTest { Thread.sleep(20); resultFuture.await(); Option throwableOption = resultFuture.getCause(); - Throwable throwable = throwableOption.get(); + Throwable throwable = throwableOption.getOrElse(new Throwable()); assertThat(throwable.getMessage()).isEqualTo("/ by zero"); } @@ -135,7 +137,8 @@ public class FutureTest { Thread.sleep(20); future.await(); - assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5))); + assertThat(future.getOrElse(new Tuple2(error, 0))) + .isEqualTo(Tuple.of("John", new Integer(5))); } @Test @@ -155,7 +158,7 @@ public class FutureTest { Thread.sleep(20); futureResult.await(); - assertThat(futureResult.get()).isEqualTo("Hello from Baeldung"); + assertThat(futureResult.getOrElse(error)).isEqualTo("Hello from Baeldung"); } @Test @@ -163,7 +166,7 @@ public class FutureTest { Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); Thread.sleep(20); Future errorMessageFuture = resultFuture.recover(Throwable::getMessage); - String errorMessage = errorMessageFuture.get(); + String errorMessage = errorMessageFuture.getOrElse(error); assertThat(errorMessage).isEqualTo("String index out of range: -1"); } @@ -173,7 +176,7 @@ public class FutureTest { Future resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello")); Thread.sleep(20); Future 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"); } @@ -187,7 +190,7 @@ public class FutureTest { Future errorMessage = errorMessageFuture.failed(); assertThat( - errorMessage.get().getMessage()) + errorMessage.getOrElse(new Throwable()).getMessage()) .isEqualTo("String index out of range: -1"); } }