Skip to content

Commit 393577b

Browse files
authored
Merge pull request eugenp#3259 from ahmedtawila/master
BAEL-1080 Introduction to Future in Vavr
2 parents c7ba3a0 + ff2cf57 commit 393577b

1 file changed

Lines changed: 46 additions & 27 deletions

File tree

‎vavr/src/test/java/com/baeldung/vavr/future/FutureTest.java‎

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,104 @@
1515
import io.vavr.control.Try;
1616

1717
public class FutureTest {
18+
19+
private static final String error = "Failed to get underlying value.";
1820

1921
@Test
20-
public void whenChangeExecutorService_thenCorrect() {
22+
public void whenChangeExecutorService_thenCorrect() throws InterruptedException {
2123
String initialValue = "Welcome to ";
2224
Future<String> resultFuture = Future.of(
2325
Executors.newSingleThreadExecutor(),
2426
() -> Util.appendData(initialValue));
25-
String result = resultFuture.get();
27+
Thread.sleep(20);
28+
String result = resultFuture.getOrElse(error);
2629

2730
assertThat(result).isEqualTo("Welcome to Baeldung!");
2831
}
2932

3033
@Test
31-
public void whenAppendData_thenCorrect1() {
34+
public void whenAppendData_thenCorrect1() throws InterruptedException {
3235
String initialValue = "Welcome to ";
3336
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
34-
String result = resultFuture.get();
37+
Thread.sleep(20);
38+
String result = resultFuture.getOrElse(new String(error));
3539

3640
assertThat(result).isEqualTo("Welcome to Baeldung!");
3741
}
3842

3943
@Test
40-
public void whenAppendData_thenCorrect2() {
44+
public void whenAppendData_thenCorrect2() throws InterruptedException {
4145
String initialValue = "Welcome to ";
4246
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
47+
Thread.sleep(20);
4348
resultFuture.await();
4449
Option<Try<String>> futureOption = resultFuture.getValue();
4550
Try<String> futureTry = futureOption.get();
46-
String result = futureTry.get();
51+
String result = futureTry.getOrElse(error);
4752

4853
assertThat(result).isEqualTo("Welcome to Baeldung!");
4954
}
5055

5156
@Test
52-
public void whenAppendData_thenSuccess() {
57+
public void whenAppendData_thenSuccess() throws InterruptedException {
5358
String initialValue = "Welcome to ";
5459
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
5560
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
5661
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
57-
String result = resultFuture.get();
62+
Thread.sleep(20);
63+
String result = resultFuture.getOrElse(error);
5864

5965
assertThat(result).isEqualTo("Welcome to Baeldung!");
6066
}
6167

6268
@Test
63-
public void whenChainingCallbacks_thenCorrect() {
69+
public void whenChainingCallbacks_thenCorrect() throws InterruptedException {
6470
String initialValue = "Welcome to ";
6571
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
6672
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
6773
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
68-
String result = resultFuture.get();
74+
Thread.sleep(20);
75+
String result = resultFuture.getOrElse(error);
6976

7077
assertThat(result).isEqualTo("Welcome to Baeldung!");
7178
}
7279

7380
@Test
74-
public void whenCallAwait_thenCorrect() {
81+
public void whenCallAwait_thenCorrect() throws InterruptedException {
7582
String initialValue = "Welcome to ";
7683
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
84+
Thread.sleep(20);
7785
resultFuture = resultFuture.await();
78-
String result = resultFuture.get();
86+
String result = resultFuture.getOrElse(error);
7987

8088
assertThat(result).isEqualTo("Welcome to Baeldung!");
8189
}
8290

8391
@Test
84-
public void whenDivideByZero_thenGetThrowable1() {
92+
public void whenDivideByZero_thenGetThrowable1() throws InterruptedException {
8593
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
94+
Thread.sleep(20);
8695
Future<Throwable> throwableFuture = resultFuture.failed();
87-
Throwable throwable = throwableFuture.get();
96+
Throwable throwable = throwableFuture.getOrElse(new Throwable());
8897

8998
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
9099
}
91100

92101
@Test
93-
public void whenDivideByZero_thenGetThrowable2() {
102+
public void whenDivideByZero_thenGetThrowable2() throws InterruptedException {
94103
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
104+
Thread.sleep(20);
95105
resultFuture.await();
96106
Option<Throwable> throwableOption = resultFuture.getCause();
97-
Throwable throwable = throwableOption.get();
107+
Throwable throwable = throwableOption.getOrElse(new Throwable());
98108

99109
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
100110
}
101111

102112
@Test
103113
public void whenDivideByZero_thenCorrect() throws InterruptedException {
104114
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
115+
Thread.sleep(20);
105116
resultFuture.await();
106117

107118
assertThat(resultFuture.isCompleted()).isTrue();
@@ -110,68 +121,76 @@ public void whenDivideByZero_thenCorrect() throws InterruptedException {
110121
}
111122

112123
@Test
113-
public void whenAppendData_thenFutureNotEmpty() {
124+
public void whenAppendData_thenFutureNotEmpty() throws InterruptedException {
114125
String initialValue = "Welcome to ";
115126
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
127+
Thread.sleep(20);
116128
resultFuture.await();
117129

118130
assertThat(resultFuture.isEmpty()).isFalse();
119131
}
120132

121133
@Test
122-
public void whenCallZip_thenCorrect() {
134+
public void whenCallZip_thenCorrect() throws InterruptedException {
123135
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
124136
.zip(Future.of(() -> new Integer(5)));
137+
Thread.sleep(20);
125138
future.await();
126139

127-
assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5)));
140+
assertThat(future.getOrElse(new Tuple2<String, Integer>(error, 0)))
141+
.isEqualTo(Tuple.of("John", new Integer(5)));
128142
}
129143

130144
@Test
131145
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
132146
String initialValue = "Welcome to ";
133147
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
148+
Thread.sleep(20);
134149
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
135150

136151
assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!");
137152
}
138153

139154
@Test
140-
public void whenCallMap_thenCorrect() {
155+
public void whenCallMap_thenCorrect() throws InterruptedException {
141156
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
142157
.map(a -> "Hello " + a);
158+
Thread.sleep(20);
143159
futureResult.await();
144160

145-
assertThat(futureResult.get()).isEqualTo("Hello from Baeldung");
161+
assertThat(futureResult.getOrElse(error)).isEqualTo("Hello from Baeldung");
146162
}
147163

148164
@Test
149-
public void whenFutureFails_thenGetErrorMessage() {
165+
public void whenFutureFails_thenGetErrorMessage() throws InterruptedException {
150166
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
167+
Thread.sleep(20);
151168
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
152-
String errorMessage = errorMessageFuture.get();
169+
String errorMessage = errorMessageFuture.getOrElse(error);
153170

154171
assertThat(errorMessage).isEqualTo("String index out of range: -1");
155172
}
156173

157174
@Test
158-
public void whenFutureFails_thenGetAnotherFuture() {
175+
public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException {
159176
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
177+
Thread.sleep(20);
160178
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
161-
String errorMessage = errorMessageFuture.get();
179+
String errorMessage = errorMessageFuture.getOrElse(error);
162180

163181
assertThat(errorMessage).isEqualTo("String index out of range: -1");
164182
}
165183

166184
@Test
167-
public void whenBothFuturesFail_thenGetErrorMessage() {
185+
public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException {
168186
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
169187
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
188+
Thread.sleep(20);
170189
Future<String> errorMessageFuture = future1.fallbackTo(future2);
171190
Future<Throwable> errorMessage = errorMessageFuture.failed();
172191

173192
assertThat(
174-
errorMessage.get().getMessage())
193+
errorMessage.getOrElse(new Throwable()).getMessage())
175194
.isEqualTo("String index out of range: -1");
176195
}
177196
}

0 commit comments

Comments
 (0)