From 1c6ece18d92f048e6263e79f8f04a60860f982c6 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Sat, 16 Dec 2017 21:33:15 +0200 Subject: [PATCH] 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();