Future in Vavr
This commit is contained in:
parent
53a84bf789
commit
a523437dc0
@ -1,57 +0,0 @@
|
||||
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<String> resultFuture = Future.of(() -> appendData(initialValue));
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertEquals("Welcome to Baeldung!", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenSuccess() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> 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<String> 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<String> 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!";
|
||||
}
|
||||
}
|
12
vavr/src/main/java/com/vavr/future/Util.java
Normal file
12
vavr/src/main/java/com/vavr/future/Util.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.vavr.future;
|
||||
|
||||
public class Util {
|
||||
|
||||
public static String appendData(String initial) {
|
||||
return initial + "Baeldung!";
|
||||
}
|
||||
|
||||
public static int divideByZero(int num) {
|
||||
return num / 0;
|
||||
}
|
||||
}
|
137
vavr/src/test/java/com/vavr/future/FutureTest.java
Normal file
137
vavr/src/test/java/com/vavr/future/FutureTest.java
Normal file
@ -0,0 +1,137 @@
|
||||
package com.vavr.future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.vavr.Tuple;
|
||||
import io.vavr.Tuple2;
|
||||
import io.vavr.concurrent.Future;
|
||||
import io.vavr.control.Option;
|
||||
import io.vavr.control.Try;
|
||||
|
||||
public class FutureTest {
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenCorrect1() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertEquals("Welcome to Baeldung!", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenCorrect2() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
resultFuture.await();
|
||||
Option<Try<String>> futureOption = resultFuture.getValue();
|
||||
Try<String> futureTry = futureOption.get();
|
||||
String result = futureTry.get();
|
||||
|
||||
assertEquals("Welcome to Baeldung!", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenSuccess() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.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<String> resultFuture = Future.of(() -> Util.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<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
resultFuture = resultFuture.await();
|
||||
String result = resultFuture.get();
|
||||
|
||||
assertEquals("Welcome to Baeldung!", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenGetThrowable1() {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
Future<Throwable> throwableFuture = resultFuture.failed();
|
||||
Throwable throwable = throwableFuture.get();
|
||||
|
||||
assertEquals("/ by zero", throwable.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenGetThrowable2() {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
resultFuture.await();
|
||||
Option<Throwable> throwableOption = resultFuture.getCause();
|
||||
Throwable throwable = throwableOption.get();
|
||||
|
||||
assertEquals("/ by zero", throwable.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivideByZero_thenCorrect() throws InterruptedException {
|
||||
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
|
||||
resultFuture.await();
|
||||
|
||||
assertTrue(resultFuture.isCompleted());
|
||||
assertFalse(resultFuture.isSuccess());
|
||||
assertTrue(resultFuture.isFailure());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenFutureNotEmpty() {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
resultFuture.await();
|
||||
|
||||
assertFalse(resultFuture.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallZip_thenCorrect() {
|
||||
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
|
||||
.zip(Future.of(() -> new Integer(5)));
|
||||
future.await();
|
||||
|
||||
assertEquals(Tuple.of("John", new Integer(5)), future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendData_thenFutureNotEmptyd() throws InterruptedException, ExecutionException {
|
||||
String initialValue = "Welcome to ";
|
||||
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
|
||||
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
|
||||
|
||||
assertEquals("Welcome to Baeldung!", convertedFuture.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallMap_thenCorrect() {
|
||||
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
|
||||
.map(a -> "Hello " + a);
|
||||
futureResult.await();
|
||||
|
||||
assertEquals("Hello from Baeldung", futureResult.get());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user