Merge branch 'future-vavr' of https://github.com/ahmedtawila/tutorials
This commit is contained in:
commit
3bafafd4e9
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.vavr.future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.vavr.concurrent.Future;
|
||||
|
||||
public class Tester {
|
||||
|
||||
@Test
|
||||
public void start() {
|
||||
Future<Integer> resultFuture = Future.of(() -> addOne(4));
|
||||
Integer result = resultFuture.get();
|
||||
|
||||
assertEquals(5, (int) result);
|
||||
}
|
||||
|
||||
public Integer addOne(Integer num) {
|
||||
return num + 1;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<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!";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue