From 1ad1431c768ea31975a11b0fd4a03a6b5158f8c6 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Sun, 3 Dec 2017 01:30:02 +0200 Subject: [PATCH] Future in Vavr --- .../src/main/java/com/vavr/future/Tester.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 vavr/src/main/java/com/vavr/future/Tester.java diff --git a/vavr/src/main/java/com/vavr/future/Tester.java b/vavr/src/main/java/com/vavr/future/Tester.java new file mode 100644 index 0000000000..438bc67abf --- /dev/null +++ b/vavr/src/main/java/com/vavr/future/Tester.java @@ -0,0 +1,57 @@ +package com.vavr.future; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.ExecutorService; + +import org.junit.Test; + +import io.vavr.concurrent.Future; + +public class Tester { + + @Test + public void whenAppendData_thenCorrect() { + String initialValue = "Welcome to "; + Future resultFuture = Future.of(() -> appendData(initialValue)); + String result = resultFuture.get(); + + assertEquals("Welcome to Baeldung!", result); + } + + @Test + public void whenAppendData_thenSuccess() { + String initialValue = "Welcome to "; + Future resultFuture = Future.of(() -> appendData(initialValue)) + .onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult)) + .onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult)); + String result = resultFuture.get(); + + assertEquals("Welcome to Baeldung!", result); + } + + @Test + public void whenChainingCallbacks_thenCorrect() { + String initialValue = "Welcome to "; + Future resultFuture = Future.of(() -> appendData(initialValue)) + .andThen(finalResult -> System.out.println("Completed - 1: " + finalResult)) + .andThen(finalResult -> System.out.println("Completed - 2: " + finalResult)); + String result = resultFuture.get(); + + assertEquals("Welcome to Baeldung!", result); + } + + @Test + public void whenCallAwait_thenCorrect() { + String initialValue = "Welcome to "; + Future resultFuture = Future.of(() -> appendData(initialValue)); + resultFuture = resultFuture.await(); + String result = resultFuture.get(); + + assertEquals("Welcome to Baeldung!", result); + } + + public String appendData(String initial) { + return initial + "Baeldung!"; + } +}