/
FutureConvertersTest.java
429 lines (397 loc) · 16.3 KB
/
FutureConvertersTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.compat.java8;
import org.junit.Test;
import scala.concurrent.Future;
import scala.concurrent.Promise;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.*;
import static org.junit.Assert.assertSame;
import static scala.compat.java8.FutureConverters.*;
public class FutureConvertersTest {
@Test
public void testToScalaSuccess() {
final CompletableFuture<String> cs = new CompletableFuture<>();
final Future<String> f = toScala(cs);
assertFalse("f must not yet be completed", f.isCompleted());
cs.complete("Hello");
assertTrue("f must be completed by now", f.isCompleted());
assertEquals("Hello", f.value().get().get());
}
@Test
public void testToScalaFailure() {
final CompletableFuture<String> cs = new CompletableFuture<>();
final Future<String> f = toScala(cs);
assertFalse("f must not yet be completed", f.isCompleted());
final Exception ex = new RuntimeException("Hello");
cs.completeExceptionally(ex);
assertTrue("f must be completed by now", f.isCompleted());
assertEquals(ex, f.value().get().failed().get());
}
@Test
public void testToJavaSuccess() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletableFuture<String> cp = (CompletableFuture<String>) cs;
assertFalse("cs must not yet be completed", cp.isDone());
p.success("Hello");
assertTrue("cs must be completed by now", cp.isDone());
assertEquals("Hello", cp.get());
}
@Test
public void testToJavaFailure() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletableFuture<String> cp = (CompletableFuture<String>) cs;
assertFalse("cs must not yet be completed", cp.isDone());
final Exception ex = new RuntimeException("Hello");
p.failure(ex);
assertTrue("cs must be completed by now", cp.isDone());
assertEquals("exceptionally equals", ex.toString(), cp.exceptionally(x -> x.toString()).get());
Throwable thr = null;
try {
cp.get();
} catch (Throwable t) {
thr = t;
}
assertNotNull("get() must throw", thr);
assertEquals("thrown exception must be wrapped", ExecutionException.class, thr.getClass());
assertEquals("wrapper must contain the right exception", ex, thr.getCause());
}
@Test
public void testToJavaThenApply() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<String> second = cs.thenApply(x -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
return x;
});
p.success("Hello");
latch.countDown();
assertEquals("Hello", second.toCompletableFuture().get());
}
@Test
public void testToJavaThenAccept() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.thenAccept(x -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaThenRun() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.thenRun(() -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaThenCombine() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<Integer> other = CompletableFuture.completedFuture(42);
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Integer> second = cs.thenCombine(other, (x, y) -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
return x.length() + y;
});
p.success("Hello");
latch.countDown();
assertEquals((Integer) 47, second.toCompletableFuture().get());
}
@Test
public void testToJavaThenAcceptBoth() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<Integer> other = CompletableFuture.completedFuture(42);
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.thenAcceptBoth(other, (x, y) -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaRunAfterBoth() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<Integer> other = CompletableFuture.completedFuture(42);
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.runAfterBoth(other, () -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaApplyToEither() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<String> other = new CompletableFuture<>();
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Integer> second = cs.applyToEither(other, x -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
return x.length();
});
p.success("Hello");
latch.countDown();
assertEquals((Integer) 5, second.toCompletableFuture().get());
}
@Test
public void testToJavaAcceptEither() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<String> other = new CompletableFuture<>();
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.acceptEither(other, x -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaRunAfterEither() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CompletionStage<String> other = new CompletableFuture<>();
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Void> second = cs.runAfterEither(other, () -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
p.success("Hello");
latch.countDown();
assertNull("result must be Void", second.toCompletableFuture().get());
}
@Test
public void testToJavaThenCompose() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<String> second = cs.thenCompose(x -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception e) {
throw new RuntimeException(e);
}
return CompletableFuture.completedFuture(x);
});
p.success("Hello");
latch.countDown();
assertEquals("Hello", second.toCompletableFuture().get());
}
@Test
public void testToJavaWhenComplete() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<String> second = cs.whenComplete((v, e) -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
p.success("Hello");
latch.countDown();
assertEquals("Hello", second.toCompletableFuture().get());
}
@Test
public void testToJavaHandle() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<Integer> second = cs.handle((v, e) -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return v.length();
});
p.success("Hello");
latch.countDown();
assertEquals((Integer) 5, second.toCompletableFuture().get());
}
@Test
public void testToJavaExceptionally() throws InterruptedException,
ExecutionException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
final CountDownLatch latch = new CountDownLatch(1);
final CompletionStage<String> second = cs.exceptionally(e -> {
try {
assertTrue("latch must succeed", latch.await(1, SECONDS));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return e.getMessage();
});
p.failure(new RuntimeException("Hello"));
latch.countDown();
assertEquals("Hello", second.toCompletableFuture().get());
}
@Test
public void testToJavaThenComposeWithToJavaThenAccept() throws InterruptedException,
ExecutionException, TimeoutException {
// Test case from https://github.com/scala/scala-java8-compat/issues/29
final Promise<String> p1 = promise();
final CompletableFuture<String> future = new CompletableFuture<>();
CompletableFuture.supplyAsync(() -> "Hello").
thenCompose(x -> toJava(p1.future())).handle((x, t) -> future.complete(x));
p1.success("Hello");
assertEquals("Hello", future.get(1000, MILLISECONDS));
}
@Test
public void testToJavaToCompletableFuture() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
CompletableFuture<String> cf = cs.toCompletableFuture();
assertEquals("notyet", cf.getNow("notyet"));
p.success("done");
assertEquals("done", cf.get());
}
@Test
public void testToJavaToCompletableFutureDoesNotMutateUnderlyingPromise() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
CompletableFuture<String> cf = cs.toCompletableFuture();
assertEquals("notyet", cf.getNow("notyet"));
cf.complete("done");
assertEquals("done", cf.get());
assertFalse(sf.isCompleted());
assertFalse(p.isCompleted());
}
@Test
public void testToJavaToCompletableFutureJavaCompleteCalledAfterScalaComplete() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
CompletableFuture<String> cf = cs.toCompletableFuture();
assertEquals("notyet", cf.getNow("notyet"));
p.success("scaladone");
assertEquals("scaladone", cf.get());
cf.complete("javadone");
assertEquals("scaladone", cf.get());
}
@Test
public void testToJavaToCompletableFutureJavaCompleteCalledBeforeScalaComplete() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
CompletableFuture<String> cf = cs.toCompletableFuture();
assertEquals("notyet", cf.getNow("notyet"));
cf.complete("javadone");
assertEquals("javadone", cf.get());
p.success("scaladone");
assertEquals("javadone", cf.get());
}
@Test
public void testToJavaToCompletableFutureJavaObtrudeCalledBeforeScalaComplete() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
CompletableFuture<String> cf = cs.toCompletableFuture();
try {
cf.obtrudeValue("");
fail();
} catch (UnsupportedOperationException iae) {
// okay
}
try {
cf.obtrudeException(new Exception());
fail();
} catch (UnsupportedOperationException iae) {
// okay
}
}
@Test
public void testToJavaAndBackAvoidsWrappers() {
final Promise<String> p = promise();
final Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
Future<String> sf1 = toScala(cs);
assertSame(sf, sf1);
}
@Test
public void testToScalaAndBackAvoidsWrappers() {
final CompletableFuture<String> cf = new CompletableFuture<>();
final Future<String> f = toScala(cf);
CompletionStage<String> cs1 = toJava(f);
assertSame(cf, cs1);
}
}