Java 9 CompletableFuture API Improvements (#1384)
This commit is contained in:
parent
b7e74ce721
commit
d32d3edcc9
|
@ -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/)
|
|
@ -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<Object> 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<Object> 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<Object> 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<Object> future = new CompletableFuture<>();
|
||||
future.completeOnTimeout(input, 1, TimeUnit.SECONDS);
|
||||
|
||||
Thread.sleep(1100);
|
||||
|
||||
assertTrue(future.isDone());
|
||||
assertSame(input, future.get());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue