Skip to content

Commit 46d8b30

Browse files
committed
Test case for toJava / thenCompose that now works
Since `toCompletableFuture` stopped throwing an `UnsupportedOperationException` in the previous commit, this use case is now supported. Fixes #29
1 parent 7bc38cb commit 46d8b30

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/test/java/scala/compat/java8/FutureConvertersTest.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
import scala.concurrent.Future;
88
import scala.concurrent.Promise;
99

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.*;
1411

12+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1513
import static java.util.concurrent.TimeUnit.SECONDS;
1614
import static org.junit.Assert.*;
1715
import static scala.compat.java8.FutureConverters.*;
@@ -319,6 +317,30 @@ public void testToJavaExceptionally() throws InterruptedException,
319317
assertEquals("Hello", second.toCompletableFuture().get());
320318
}
321319

320+
@Test
321+
public void testToJavaThenComposeWithToJavaThenAccept() throws InterruptedException,
322+
ExecutionException, TimeoutException {
323+
final Promise<String> p1 = promise();
324+
final CompletableFuture<String> future = new CompletableFuture<>();
325+
326+
final CompletionStage<String> second =
327+
CompletableFuture.supplyAsync(() -> "Hello").
328+
thenCompose(x -> toJava(p1.future()));
329+
330+
second.handle((x, t) -> {
331+
if (t != null) {
332+
t.printStackTrace();
333+
future.completeExceptionally(t);
334+
} else {
335+
future.complete(x);
336+
}
337+
return null;
338+
});
339+
340+
p1.success("Hello");
341+
assertEquals("Hello", future.get(1000, MILLISECONDS));
342+
}
343+
322344
@Test
323345
public void testToJavaToCompletableFuture() throws ExecutionException, InterruptedException {
324346
final Promise<String> p = promise();

0 commit comments

Comments
 (0)