Skip to content

Commit 1c6ece1

Browse files
author
Ahmed Tawila
committed
wait for some time before retrieving values
1 parent d305392 commit 1c6ece1

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,32 @@
1717
public class FutureTest {
1818

1919
@Test
20-
public void whenChangeExecutorService_thenCorrect() {
20+
public void whenChangeExecutorService_thenCorrect() throws InterruptedException {
2121
String initialValue = "Welcome to ";
2222
Future<String> resultFuture = Future.of(
2323
Executors.newSingleThreadExecutor(),
2424
() -> Util.appendData(initialValue));
25+
Thread.sleep(20);
2526
String result = resultFuture.get();
2627

2728
assertThat(result).isEqualTo("Welcome to Baeldung!");
2829
}
2930

3031
@Test
31-
public void whenAppendData_thenCorrect1() {
32+
public void whenAppendData_thenCorrect1() throws InterruptedException {
3233
String initialValue = "Welcome to ";
3334
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
35+
Thread.sleep(20);
3436
String result = resultFuture.get();
3537

3638
assertThat(result).isEqualTo("Welcome to Baeldung!");
3739
}
3840

3941
@Test
40-
public void whenAppendData_thenCorrect2() {
42+
public void whenAppendData_thenCorrect2() throws InterruptedException {
4143
String initialValue = "Welcome to ";
4244
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
45+
Thread.sleep(20);
4346
resultFuture.await();
4447
Option<Try<String>> futureOption = resultFuture.getValue();
4548
Try<String> futureTry = futureOption.get();
@@ -49,49 +52,54 @@ public void whenAppendData_thenCorrect2() {
4952
}
5053

5154
@Test
52-
public void whenAppendData_thenSuccess() {
55+
public void whenAppendData_thenSuccess() throws InterruptedException {
5356
String initialValue = "Welcome to ";
5457
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
5558
.onSuccess(finalResult -> System.out.println("Successfully Completed - Result: " + finalResult))
5659
.onFailure(finalResult -> System.out.println("Failed - Result: " + finalResult));
60+
Thread.sleep(20);
5761
String result = resultFuture.get();
5862

5963
assertThat(result).isEqualTo("Welcome to Baeldung!");
6064
}
6165

6266
@Test
63-
public void whenChainingCallbacks_thenCorrect() {
67+
public void whenChainingCallbacks_thenCorrect() throws InterruptedException {
6468
String initialValue = "Welcome to ";
6569
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue))
6670
.andThen(finalResult -> System.out.println("Completed - 1: " + finalResult))
6771
.andThen(finalResult -> System.out.println("Completed - 2: " + finalResult));
72+
Thread.sleep(20);
6873
String result = resultFuture.get();
6974

7075
assertThat(result).isEqualTo("Welcome to Baeldung!");
7176
}
7277

7378
@Test
74-
public void whenCallAwait_thenCorrect() {
79+
public void whenCallAwait_thenCorrect() throws InterruptedException {
7580
String initialValue = "Welcome to ";
7681
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
82+
Thread.sleep(20);
7783
resultFuture = resultFuture.await();
7884
String result = resultFuture.get();
7985

8086
assertThat(result).isEqualTo("Welcome to Baeldung!");
8187
}
8288

8389
@Test
84-
public void whenDivideByZero_thenGetThrowable1() {
90+
public void whenDivideByZero_thenGetThrowable1() throws InterruptedException {
8591
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
92+
Thread.sleep(20);
8693
Future<Throwable> throwableFuture = resultFuture.failed();
8794
Throwable throwable = throwableFuture.get();
8895

8996
assertThat(throwable.getMessage()).isEqualTo("/ by zero");
9097
}
9198

9299
@Test
93-
public void whenDivideByZero_thenGetThrowable2() {
100+
public void whenDivideByZero_thenGetThrowable2() throws InterruptedException {
94101
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
102+
Thread.sleep(20);
95103
resultFuture.await();
96104
Option<Throwable> throwableOption = resultFuture.getCause();
97105
Throwable throwable = throwableOption.get();
@@ -102,6 +110,7 @@ public void whenDivideByZero_thenGetThrowable2() {
102110
@Test
103111
public void whenDivideByZero_thenCorrect() throws InterruptedException {
104112
Future<Integer> resultFuture = Future.of(() -> Util.divideByZero(10));
113+
Thread.sleep(20);
105114
resultFuture.await();
106115

107116
assertThat(resultFuture.isCompleted()).isTrue();
@@ -110,18 +119,20 @@ public void whenDivideByZero_thenCorrect() throws InterruptedException {
110119
}
111120

112121
@Test
113-
public void whenAppendData_thenFutureNotEmpty() {
122+
public void whenAppendData_thenFutureNotEmpty() throws InterruptedException {
114123
String initialValue = "Welcome to ";
115124
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
125+
Thread.sleep(20);
116126
resultFuture.await();
117127

118128
assertThat(resultFuture.isEmpty()).isFalse();
119129
}
120130

121131
@Test
122-
public void whenCallZip_thenCorrect() {
132+
public void whenCallZip_thenCorrect() throws InterruptedException {
123133
Future<Tuple2<String, Integer>> future = Future.of(() -> "John")
124134
.zip(Future.of(() -> new Integer(5)));
135+
Thread.sleep(20);
125136
future.await();
126137

127138
assertThat(future.get()).isEqualTo(Tuple.of("John", new Integer(5)));
@@ -131,42 +142,47 @@ public void whenCallZip_thenCorrect() {
131142
public void whenConvertToCompletableFuture_thenCorrect() throws InterruptedException, ExecutionException {
132143
String initialValue = "Welcome to ";
133144
Future<String> resultFuture = Future.of(() -> Util.appendData(initialValue));
145+
Thread.sleep(20);
134146
CompletableFuture<String> convertedFuture = resultFuture.toCompletableFuture();
135147

136148
assertThat(convertedFuture.get()).isEqualTo("Welcome to Baeldung!");
137149
}
138150

139151
@Test
140-
public void whenCallMap_thenCorrect() {
152+
public void whenCallMap_thenCorrect() throws InterruptedException {
141153
Future<String> futureResult = Future.of(() -> new StringBuilder("from Baeldung"))
142154
.map(a -> "Hello " + a);
155+
Thread.sleep(20);
143156
futureResult.await();
144157

145158
assertThat(futureResult.get()).isEqualTo("Hello from Baeldung");
146159
}
147160

148161
@Test
149-
public void whenFutureFails_thenGetErrorMessage() {
162+
public void whenFutureFails_thenGetErrorMessage() throws InterruptedException {
150163
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
164+
Thread.sleep(20);
151165
Future<String> errorMessageFuture = resultFuture.recover(Throwable::getMessage);
152166
String errorMessage = errorMessageFuture.get();
153167

154168
assertThat(errorMessage).isEqualTo("String index out of range: -1");
155169
}
156170

157171
@Test
158-
public void whenFutureFails_thenGetAnotherFuture() {
172+
public void whenFutureFails_thenGetAnotherFuture() throws InterruptedException {
159173
Future<String> resultFuture = Future.of(() -> Util.getSubstringMinusOne("Hello"));
174+
Thread.sleep(20);
160175
Future<String> errorMessageFuture = resultFuture.recoverWith(a -> Future.of(a::getMessage));
161176
String errorMessage = errorMessageFuture.get();
162177

163178
assertThat(errorMessage).isEqualTo("String index out of range: -1");
164179
}
165180

166181
@Test
167-
public void whenBothFuturesFail_thenGetErrorMessage() {
182+
public void whenBothFuturesFail_thenGetErrorMessage() throws InterruptedException {
168183
Future<String> future1 = Future.of(() -> Util.getSubstringMinusOne("Hello"));
169184
Future<String> future2 = Future.of(() -> Util.getSubstringMinusTwo("Hello"));
185+
Thread.sleep(20);
170186
Future<String> errorMessageFuture = future1.fallbackTo(future2);
171187
Future<Throwable> errorMessage = errorMessageFuture.failed();
172188

0 commit comments

Comments
 (0)