diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-01 16:57:23 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-02 10:10:26 +0100 |
commit | 653a83398e536c0413a4f2ee50981cbe3a6f12f7 (patch) | |
tree | 24109e06e74623da945b69ca260ac738a1e9f9d7 /vespajlib/src/test | |
parent | f453ae5a89d9c5a4cd5926f8443a0acd564cbc86 (diff) |
Add helper for combining multiple completable futures
Diffstat (limited to 'vespajlib/src/test')
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/concurrent/CompletableFuturesTest.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/concurrent/CompletableFuturesTest.java b/vespajlib/src/test/java/com/yahoo/concurrent/CompletableFuturesTest.java new file mode 100644 index 00000000000..cf9c36537d9 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/concurrent/CompletableFuturesTest.java @@ -0,0 +1,66 @@ +package com.yahoo.concurrent;// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author bjorncs + */ +class CompletableFuturesTest { + + @Test + public void firstof_completes_when_first_futures_has_completed() { + CompletableFuture<String> f1 = new CompletableFuture<>(); + CompletableFuture<String> f2 = new CompletableFuture<>(); + CompletableFuture<String> f3 = new CompletableFuture<>(); + CompletableFuture<String> result = CompletableFutures.firstOf(List.of(f1, f2, f3)); + f1.complete("success"); + assertTrue(result.isDone()); + assertFalse(result.isCompletedExceptionally()); + assertEquals("success", result.join()); + } + + @Test + public void firstof_completes_if_any_futures_completes() { + CompletableFuture<String> f1 = new CompletableFuture<>(); + CompletableFuture<String> f2 = new CompletableFuture<>(); + CompletableFuture<String> f3 = new CompletableFuture<>(); + CompletableFuture<String> result = CompletableFutures.firstOf(List.of(f1, f2, f3)); + f1.completeExceptionally(new Throwable("t1")); + f2.completeExceptionally(new Throwable("t2")); + f3.complete("success"); + assertTrue(result.isDone()); + assertFalse(result.isCompletedExceptionally()); + assertEquals("success", result.join()); + } + + @Test + public void firstof_completes_exceptionally_when_all_futures_have_complete_exceptionally() { + CompletableFuture<String> f1 = new CompletableFuture<>(); + CompletableFuture<String> f2 = new CompletableFuture<>(); + CompletableFuture<String> f3 = new CompletableFuture<>(); + CompletableFuture<String> result = CompletableFutures.firstOf(List.of(f1, f2, f3)); + f1.completeExceptionally(new Throwable("t1")); + f2.completeExceptionally(new Throwable("t2")); + f3.completeExceptionally(new Throwable("t3")); + assertTrue(result.isDone()); + assertTrue(result.isCompletedExceptionally()); + try { + result.join(); + fail("Exception expected"); + } catch (CompletionException e) { + Throwable cause = e.getCause(); + assertEquals("t1", cause.getMessage()); + assertEquals(2, cause.getSuppressed().length); + } + } + +}
\ No newline at end of file |