From e075e4026df491b68648c262c9a43eef3974b5bc Mon Sep 17 00:00:00 2001 From: Roger Schildmeijer Date: Thu, 5 Nov 2020 11:46:16 +0100 Subject: [PATCH] Add support for CompletionStage tasks in the ConcurrencyReducer --- .../java/com/spotify/futures/ConcurrencyReducer.java | 6 +++--- .../com/spotify/futures/ConcurrencyReducerTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/spotify/futures/ConcurrencyReducer.java b/src/main/java/com/spotify/futures/ConcurrencyReducer.java index 81d3189..e8383a7 100644 --- a/src/main/java/com/spotify/futures/ConcurrencyReducer.java +++ b/src/main/java/com/spotify/futures/ConcurrencyReducer.java @@ -164,13 +164,13 @@ private void invoke(final CompletableFuture response, future.whenComplete( (result, t) -> { - if (result != null) { + if (t != null) { limit.release(); - response.complete(result); + response.completeExceptionally(t); pump(); } else { limit.release(); - response.completeExceptionally(t); + response.complete(result); pump(); } }); diff --git a/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java b/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java index 3219725..db11b83 100644 --- a/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java +++ b/src/test/java/com/spotify/futures/ConcurrencyReducerTest.java @@ -54,6 +54,16 @@ public void testNullJob() throws Exception { limiter.add(null); } + @Test() + public void testVoidJob() { + final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10); + final CompletionStage task = CompletableFuture.completedFuture(null); + assertTrue(task.toCompletableFuture().isDone()); + + final CompletableFuture stage = limiter.add(() -> task); + assertTrue(stage.isDone()); + } + @Test public void testJobReturnsNull() throws Exception { final ConcurrencyReducer limiter = ConcurrencyReducer.create(1, 10);