From d32d3edcc93fbbab1ed368275812d54ab5dc33b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Melo?= Date: Mon, 13 Mar 2017 08:13:45 +0000 Subject: [PATCH] Java 9 CompletableFuture API Improvements (#1384) --- core-java-9/README.md | 1 + .../future/CompletableFutureTest.java | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/concurrent/future/CompletableFutureTest.java diff --git a/core-java-9/README.md b/core-java-9/README.md index 53ad79e59c..6e58383a5e 100644 --- a/core-java-9/README.md +++ b/core-java-9/README.md @@ -8,3 +8,4 @@ - [Java 9 Stream API Improvements](http://www.baeldung.com/java-9-stream-api) - [Java 9 Convenience Factory Methods for Collections](http://www.baeldung.com/java-9-collections-factory-methods) - [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors) +- [Java 9 CompletableFuture API Improvements](http://www.baeldung.com/java9-completablefuture-api-improvements/) \ No newline at end of file diff --git a/core-java-9/src/test/java/com/baeldung/java9/concurrent/future/CompletableFutureTest.java b/core-java-9/src/test/java/com/baeldung/java9/concurrent/future/CompletableFutureTest.java new file mode 100644 index 0000000000..b71c211177 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/concurrent/future/CompletableFutureTest.java @@ -0,0 +1,74 @@ +package com.baeldung.java9.concurrent.future; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class CompletableFutureTest { + @Test + public void testDelay () throws Exception { + Object input = new Object(); + CompletableFuture future = new CompletableFuture<>(); + future.completeAsync(() -> input, CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS)); + + Thread.sleep(100); + + assertFalse(future.isDone()); + + Thread.sleep(1000); + assertTrue(future.isDone()); + assertSame(input, future.get()); + } + + @Test + public void testTimeoutTriggered () throws Exception { + CompletableFuture future = new CompletableFuture<>(); + future.orTimeout(1, TimeUnit.SECONDS); + + Thread.sleep(1100); + + assertTrue(future.isDone()); + + try { + future.get(); + } catch (ExecutionException e) { + assertTrue(e.getCause() instanceof TimeoutException); + } + } + + @Test + public void testTimeoutNotTriggered () throws Exception { + Object input = new Object(); + CompletableFuture future = new CompletableFuture<>(); + + future.orTimeout(1, TimeUnit.SECONDS); + + Thread.sleep(100); + + future.complete(input); + + Thread.sleep(1000); + + assertTrue(future.isDone()); + assertSame(input, future.get()); + } + + + + @Test + public void completeOnTimeout () throws Exception { + Object input = new Object(); + CompletableFuture future = new CompletableFuture<>(); + future.completeOnTimeout(input, 1, TimeUnit.SECONDS); + + Thread.sleep(1100); + + assertTrue(future.isDone()); + assertSame(input, future.get()); + } +}