From e1e6550a49f7aa01a968a0e57a6a9bc7103d7446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20H=C6=B0ng?= Date: Mon, 28 Aug 2017 03:44:06 +0700 Subject: [PATCH] BAEL-1080 Introduction to Future in Vavr (#2488) * Add example of different types of bean injection * BAEL-1080 Introduction to Future and Pattern Matching in Vavr * Update future with Pattern Matching * remove unused files * Update future unit test of cancelled Future * remove unused import * delete unused files * using await() on future to wait until future completed instead of using while(true) * Add Awaitability to FutureTest, avoid using while loop * format code --- vavr/pom.xml | 8 + .../baeldung/vavr/future/FutureUnitTest.java | 203 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java diff --git a/vavr/pom.xml b/vavr/pom.xml index 426155263c..53cd07ddf7 100644 --- a/vavr/pom.xml +++ b/vavr/pom.xml @@ -40,6 +40,13 @@ org.springframework.boot spring-boot-starter-test + + + org.awaitility + awaitility + ${awaitility.version} + test + @@ -69,6 +76,7 @@ 1.8 0.9.0 4.12 + 3.0.0 diff --git a/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java new file mode 100644 index 0000000000..84621e3a68 --- /dev/null +++ b/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java @@ -0,0 +1,203 @@ +package com.baeldung.vavr.future; + +import static io.vavr.API.$; +import static io.vavr.API.Case; +import static io.vavr.API.Match; +import static io.vavr.Predicates.exists; +import static io.vavr.Predicates.forAll; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CancellationException; +import java.util.function.Predicate; + +import org.junit.Test; + +import io.vavr.collection.List; +import io.vavr.concurrent.Future; + +public class FutureUnitTest { + + private final String SUCCESS = "Success"; + private final String FAILURE = "Failure"; + + @Test + public void givenFunctionReturnInteger_WhenCallWithFuture_ShouldReturnFunctionValue() { + Future future = Future.of(() -> 1); + + assertEquals(1, future.get() + .intValue()); + } + + @Test + public void givenFunctionGetRemoteHttpResourceAsString_WhenCallSuccessWithFuture_ShouldReturnContentValueAsString() { + String url = "http://resource"; + String content = "Content from " + url; + Future future = Future.of(() -> getResource(url)); + + assertEquals(content, future.get()); + } + + @Test + public void givenFunctionThrowException_WhenCallWithFuture_ShouldReturnFailure() { + Future future = Future.of(() -> getResourceThrowException("")); + future.await(); + + assertTrue(future.isFailure()); + } + + @Test + public void givenAFutureReturnZero_WhenCheckFutureWithExistEvenValue_ShouldReturnRight() { + Future future = Future.of(() -> 2); + boolean result = future.exists(i -> i % 2 == 0); + + assertTrue(result); + } + + @Test + public void givenFunction_WhenCallWithFutureAndRegisterConsumerForSuccess_ShouldCallConsumerToStoreValue() { + final int[] store = new int[] { 0 }; + Future future = Future.of(() -> 1); + future.onSuccess(i -> { + store[0] = i; + }); + await().until(() -> store[0] == 1); + } + + @Test + public void givenFunctionThrowException_WhenCallWithFutureAndRegisterConsumerForFailer_ShouldCallConsumerToStoreException() { + final Throwable[] store = new Throwable[] { null }; + Future future = Future.of(() -> getResourceThrowException("")); + future.onFailure(err -> store[0] = err); + await().until(() -> RuntimeException.class.isInstance(store[0])); + } + + @Test + public void givenAFuture_WhenAddAndThenConsumer_ShouldCallConsumerWithResultOfFutureAction() { + int[] store1 = new int[1]; + int[] store2 = new int[1]; + Future future = Future.of(() -> 1); + Future andThenFuture = future.andThen(i -> store1[0] = i.get() + 1) + .andThen(i -> store2[0] = store1[0] + 1); + andThenFuture.await(); + + assertEquals(2, store1[0]); + assertEquals(3, store2[0]); + } + + @Test + public void givenAFailureFuture_WhenCallOrElseFunction_ShouldReturnNewFuture() { + Future future = Future.failed(new RuntimeException()); + Future future2 = future.orElse(Future.of(() -> 2)); + + assertEquals(2, future2.get() + .intValue()); + } + + @Test(expected = CancellationException.class) + public void givenAFuture_WhenCallCancel_ShouldReturnCancellationException() { + long waitTime = 1000; + Future future = Future.of(() -> { + Thread.sleep(waitTime); + return 1; + }); + future.cancel(); + future.await(); + future.get(); + } + + @Test + public void givenAFuture_WhenCallFallBackWithSuccessFuture_ShouldReturnFutureResult() { + String expectedResult = "take this"; + Future future = Future.of(() -> expectedResult); + Future secondFuture = Future.of(() -> "take that"); + Future futureResult = future.fallbackTo(secondFuture); + futureResult.await(); + + assertEquals(expectedResult, futureResult.get()); + } + + @Test + public void givenAFuture_WhenCallFallBackWithFailureFuture_ShouldReturnValueOfFallBackFuture() { + String expectedResult = "take that"; + Future future = Future.failed(new RuntimeException()); + Future fallbackFuture = Future.of(() -> expectedResult); + Future futureResult = future.fallbackTo(fallbackFuture); + futureResult.await(); + + assertEquals(expectedResult, futureResult.get()); + } + + @Test + public void givenGetResourceWithFuture_WhenWaitAndMatchWithPredicate_ShouldReturnSuccess() { + String url = "http://resource"; + Future future = Future.of(() -> getResource(url)); + future.await(); + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); + + assertEquals(SUCCESS, s); + } + + @Test + public void givenAFailedFuture_WhenWaitAndMatchWithPredicateCheckSuccess_ShouldReturnFailed() { + Future future = Future.failed(new RuntimeException()); + future.await(); + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); + + assertEquals(FAILURE, s); + } + + @Test + public void givenAFuture_WhenMatchWithFuturePredicate_ShouldReturnSuccess() { + Future future = Future.of(() -> { + Thread.sleep(10); + return 1; + }); + Predicate> predicate = f -> f.exists(i -> i % 2 == 1); + String s = Match(future).of(Case($(predicate), "Even"), Case($(), "Odd")); + + assertEquals("Even", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithExistEvenNumberPredicate_ShouldReturnSuccess() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i % 2 == 0); + String s = Match(futures).of(Case($(exists(predicate0)), "Even"), Case($(), "Odd")); + + assertEquals("Even", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberBiggerThanZeroPredicate_ShouldReturnSuccess() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i > 0); + String s = Match(futures).of(Case($(forAll(predicate0)), "Positive numbers"), Case($(), "None")); + + assertEquals("Positive numbers", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberSmallerThanZeroPredicate_ShouldReturnFailed() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i < 0); + String s = Match(futures).of(Case($(forAll(predicate0)), "Negative numbers"), Case($(), "None")); + + assertEquals("None", s); + } + + private String getResource(String url) throws InterruptedException { + Thread.sleep(10); + return "Content from " + url; + } + + private String getResourceThrowException(String url) { + throw new RuntimeException("Exception when get resource " + url); + } + + private List> getFutureOfFirst3Number() { + List> futures = List.of(Future.of(() -> 1), Future.of(() -> 2), Future.of(() -> 3)); + return futures; + } +}