|
7 | 7 | import scala.concurrent.Future;
|
8 | 8 | import scala.concurrent.Promise;
|
9 | 9 |
|
10 |
| -import java.util.concurrent.CompletableFuture; |
11 |
| -import java.util.concurrent.CompletionStage; |
12 |
| -import java.util.concurrent.CountDownLatch; |
13 |
| -import java.util.concurrent.ExecutionException; |
| 10 | +import java.util.concurrent.*; |
14 | 11 |
|
| 12 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
15 | 13 | import static java.util.concurrent.TimeUnit.SECONDS;
|
16 | 14 | import static org.junit.Assert.*;
|
17 | 15 | import static scala.compat.java8.FutureConverters.*;
|
@@ -318,4 +316,86 @@ public void testToJavaExceptionally() throws InterruptedException,
|
318 | 316 | latch.countDown();
|
319 | 317 | assertEquals("Hello", second.toCompletableFuture().get());
|
320 | 318 | }
|
| 319 | + |
| 320 | + @Test |
| 321 | + public void testToJavaThenComposeWithToJavaThenAccept() throws InterruptedException, |
| 322 | + ExecutionException, TimeoutException { |
| 323 | + // Test case from https://github.com/scala/scala-java8-compat/issues/29 |
| 324 | + final Promise<String> p1 = promise(); |
| 325 | + final CompletableFuture<String> future = new CompletableFuture<>(); |
| 326 | + |
| 327 | + CompletableFuture.supplyAsync(() -> "Hello"). |
| 328 | + thenCompose(x -> toJava(p1.future())).handle((x, t) -> future.complete(x)); |
| 329 | + p1.success("Hello"); |
| 330 | + assertEquals("Hello", future.get(1000, MILLISECONDS)); |
| 331 | + } |
| 332 | + |
| 333 | + @Test |
| 334 | + public void testToJavaToCompletableFuture() throws ExecutionException, InterruptedException { |
| 335 | + final Promise<String> p = promise(); |
| 336 | + final CompletionStage<String> cs = toJava(p.future()); |
| 337 | + CompletableFuture<String> cf = cs.toCompletableFuture(); |
| 338 | + assertEquals("notyet", cf.getNow("notyet")); |
| 339 | + p.success("done"); |
| 340 | + assertEquals("done", cf.get()); |
| 341 | + } |
| 342 | + |
| 343 | + @Test |
| 344 | + public void testToJavaToCompletableFutureDoesNotMutateUnderlyingPromise() throws ExecutionException, InterruptedException { |
| 345 | + final Promise<String> p = promise(); |
| 346 | + Future<String> sf = p.future(); |
| 347 | + final CompletionStage<String> cs = toJava(sf); |
| 348 | + CompletableFuture<String> cf = cs.toCompletableFuture(); |
| 349 | + assertEquals("notyet", cf.getNow("notyet")); |
| 350 | + cf.complete("done"); |
| 351 | + assertEquals("done", cf.get()); |
| 352 | + assertFalse(sf.isCompleted()); |
| 353 | + assertFalse(p.isCompleted()); |
| 354 | + } |
| 355 | + |
| 356 | + @Test |
| 357 | + public void testToJavaToCompletableFutureJavaCompleteCalledAfterScalaComplete() throws ExecutionException, InterruptedException { |
| 358 | + final Promise<String> p = promise(); |
| 359 | + Future<String> sf = p.future(); |
| 360 | + final CompletionStage<String> cs = toJava(sf); |
| 361 | + CompletableFuture<String> cf = cs.toCompletableFuture(); |
| 362 | + assertEquals("notyet", cf.getNow("notyet")); |
| 363 | + p.success("scaladone"); |
| 364 | + assertEquals("scaladone", cf.get()); |
| 365 | + cf.complete("javadone"); |
| 366 | + assertEquals("scaladone", cf.get()); |
| 367 | + } |
| 368 | + |
| 369 | + @Test |
| 370 | + public void testToJavaToCompletableFutureJavaCompleteCalledBeforeScalaComplete() throws ExecutionException, InterruptedException { |
| 371 | + final Promise<String> p = promise(); |
| 372 | + Future<String> sf = p.future(); |
| 373 | + final CompletionStage<String> cs = toJava(sf); |
| 374 | + CompletableFuture<String> cf = cs.toCompletableFuture(); |
| 375 | + assertEquals("notyet", cf.getNow("notyet")); |
| 376 | + cf.complete("javadone"); |
| 377 | + assertEquals("javadone", cf.get()); |
| 378 | + p.success("scaladone"); |
| 379 | + assertEquals("javadone", cf.get()); |
| 380 | + } |
| 381 | + |
| 382 | + @Test |
| 383 | + public void testToJavaToCompletableFutureJavaObtrudeCalledBeforeScalaComplete() throws ExecutionException, InterruptedException { |
| 384 | + final Promise<String> p = promise(); |
| 385 | + Future<String> sf = p.future(); |
| 386 | + final CompletionStage<String> cs = toJava(sf); |
| 387 | + CompletableFuture<String> cf = cs.toCompletableFuture(); |
| 388 | + try { |
| 389 | + cf.obtrudeValue(""); |
| 390 | + fail(); |
| 391 | + } catch (UnsupportedOperationException iae) { |
| 392 | + // okay |
| 393 | + } |
| 394 | + try { |
| 395 | + cf.obtrudeException(new Exception()); |
| 396 | + fail(); |
| 397 | + } catch (UnsupportedOperationException iae) { |
| 398 | + // okay |
| 399 | + } |
| 400 | + } |
321 | 401 | }
|
0 commit comments