diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-12-09 16:25:27 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-12-10 10:28:28 +0100 |
commit | 134af4f00cb0302f1ba465e51973fe44e1646e19 (patch) | |
tree | 5724df56111808aca6c33161e6f2d6740be0f8dd /jdisc_core | |
parent | 4bc0e6916d4532f00b735a79905e40f3b8eb51ea (diff) |
Remove use of Guava ListenableFuture from com.yahoo.jdisc.handler
This change is not 100% API compatible. Many classes from this package inherited types from Guava.
The classes will have the same methods as before, but their type has obviously changed.
Two options; merge now with the small risk of breakage or wait for Vespa 8 branch.
Diffstat (limited to 'jdisc_core')
16 files changed, 145 insertions, 153 deletions
diff --git a/jdisc_core/abi-spec.json b/jdisc_core/abi-spec.json index 86c016d520c..d1b676b330f 100644 --- a/jdisc_core/abi-spec.json +++ b/jdisc_core/abi-spec.json @@ -531,7 +531,7 @@ "com.yahoo.jdisc.handler.FastContentOutputStream": { "superClass": "com.yahoo.jdisc.handler.AbstractContentOutputStream", "interfaces": [ - "com.google.common.util.concurrent.ListenableFuture" + "java.util.concurrent.Future" ], "attributes": [ "public" @@ -553,9 +553,8 @@ "fields": [] }, "com.yahoo.jdisc.handler.FastContentWriter": { - "superClass": "java.lang.Object", + "superClass": "java.util.concurrent.CompletableFuture", "interfaces": [ - "com.google.common.util.concurrent.ListenableFuture", "java.lang.AutoCloseable" ], "attributes": [ @@ -570,17 +569,12 @@ "public void close()", "public void addListener(java.lang.Runnable, java.util.concurrent.Executor)", "public boolean cancel(boolean)", - "public boolean isCancelled()", - "public boolean isDone()", - "public java.lang.Boolean get()", - "public java.lang.Boolean get(long, java.util.concurrent.TimeUnit)", - "public bridge synthetic java.lang.Object get(long, java.util.concurrent.TimeUnit)", - "public bridge synthetic java.lang.Object get()" + "public boolean isCancelled()" ], "fields": [] }, "com.yahoo.jdisc.handler.FutureCompletion": { - "superClass": "com.google.common.util.concurrent.AbstractFuture", + "superClass": "java.util.concurrent.CompletableFuture", "interfaces": [ "com.yahoo.jdisc.handler.CompletionHandler" ], @@ -593,12 +587,13 @@ "public void completed()", "public void failed(java.lang.Throwable)", "public final boolean cancel(boolean)", - "public final boolean isCancelled()" + "public final boolean isCancelled()", + "public void addListener(java.lang.Runnable, java.util.concurrent.Executor)" ], "fields": [] }, "com.yahoo.jdisc.handler.FutureResponse": { - "superClass": "com.google.common.util.concurrent.AbstractFuture", + "superClass": "java.util.concurrent.CompletableFuture", "interfaces": [ "com.yahoo.jdisc.handler.ResponseHandler" ], @@ -609,6 +604,7 @@ "methods": [ "public void <init>()", "public void <init>(com.yahoo.jdisc.handler.ContentChannel)", + "public void addListener(java.lang.Runnable, java.util.concurrent.Executor)", "public void <init>(com.yahoo.jdisc.handler.ResponseHandler)", "public com.yahoo.jdisc.handler.ContentChannel handleResponse(com.yahoo.jdisc.Response)", "public final boolean cancel(boolean)", @@ -682,7 +678,7 @@ "com.yahoo.jdisc.handler.RequestDispatch": { "superClass": "java.lang.Object", "interfaces": [ - "com.google.common.util.concurrent.ListenableFuture", + "java.util.concurrent.Future", "com.yahoo.jdisc.handler.ResponseHandler" ], "attributes": [ @@ -695,7 +691,7 @@ "protected java.lang.Iterable requestContent()", "public final com.yahoo.jdisc.handler.ContentChannel connect()", "public final com.yahoo.jdisc.handler.FastContentWriter connectFastWriter()", - "public final com.google.common.util.concurrent.ListenableFuture dispatch()", + "public final java.util.concurrent.CompletableFuture dispatch()", "public void addListener(java.lang.Runnable, java.util.concurrent.Executor)", "public final boolean cancel(boolean)", "public final boolean isCancelled()", @@ -725,8 +721,10 @@ "fields": [] }, "com.yahoo.jdisc.handler.ResponseDispatch": { - "superClass": "com.google.common.util.concurrent.ForwardingListenableFuture", - "interfaces": [], + "superClass": "java.lang.Object", + "interfaces": [ + "java.util.concurrent.Future" + ], "attributes": [ "public", "abstract" @@ -737,16 +735,18 @@ "protected java.lang.Iterable responseContent()", "public final com.yahoo.jdisc.handler.ContentChannel connect(com.yahoo.jdisc.handler.ResponseHandler)", "public final com.yahoo.jdisc.handler.FastContentWriter connectFastWriter(com.yahoo.jdisc.handler.ResponseHandler)", - "public final com.google.common.util.concurrent.ListenableFuture dispatch(com.yahoo.jdisc.handler.ResponseHandler)", - "protected final com.google.common.util.concurrent.ListenableFuture delegate()", + "public final java.util.concurrent.CompletableFuture dispatch(com.yahoo.jdisc.handler.ResponseHandler)", "public final boolean cancel(boolean)", "public final boolean isCancelled()", + "public boolean isDone()", + "public java.lang.Boolean get()", + "public java.lang.Boolean get(long, java.util.concurrent.TimeUnit)", "public static varargs com.yahoo.jdisc.handler.ResponseDispatch newInstance(int, java.nio.ByteBuffer[])", "public static com.yahoo.jdisc.handler.ResponseDispatch newInstance(int, java.lang.Iterable)", "public static varargs com.yahoo.jdisc.handler.ResponseDispatch newInstance(com.yahoo.jdisc.Response, java.nio.ByteBuffer[])", "public static com.yahoo.jdisc.handler.ResponseDispatch newInstance(com.yahoo.jdisc.Response, java.lang.Iterable)", - "protected bridge synthetic java.util.concurrent.Future delegate()", - "protected bridge synthetic java.lang.Object delegate()" + "public bridge synthetic java.lang.Object get(long, java.util.concurrent.TimeUnit)", + "public bridge synthetic java.lang.Object get()" ], "fields": [] }, diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java index 54e50df5a25..e001db2ab81 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentOutputStream.java @@ -1,12 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ListenableFuture; - import java.nio.ByteBuffer; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -20,7 +19,7 @@ import java.util.concurrent.TimeoutException; * * @author Simon Thoresen Hult */ -public class FastContentOutputStream extends AbstractContentOutputStream implements ListenableFuture<Boolean> { +public class FastContentOutputStream extends AbstractContentOutputStream implements Future<Boolean> { private final FastContentWriter out; @@ -78,7 +77,6 @@ public class FastContentOutputStream extends AbstractContentOutputStream impleme return out.get(timeout, unit); } - @Override public void addListener(Runnable listener, Executor executor) { out.addListener(listener, executor); } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java index 596ae07f1d5..7c278c67d59 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FastContentWriter.java @@ -1,16 +1,11 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.SettableFuture; - import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Objects; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -25,13 +20,12 @@ import java.util.concurrent.atomic.AtomicInteger; * * @author Simon Thoresen Hult */ -public class FastContentWriter implements ListenableFuture<Boolean>, AutoCloseable { +public class FastContentWriter extends CompletableFuture<Boolean> implements AutoCloseable { private final AtomicBoolean closed = new AtomicBoolean(false); private final AtomicInteger numPendingCompletions = new AtomicInteger(); private final CompletionHandler completionHandler = new SimpleCompletionHandler(); private final ContentChannel out; - private final SettableFuture<Boolean> future = SettableFuture.create(); /** * <p>Creates a new FastContentWriter that encapsulates a given {@link ContentChannel}.</p> @@ -87,7 +81,7 @@ public class FastContentWriter implements ListenableFuture<Boolean>, AutoCloseab try { out.write(buf, completionHandler); } catch (Throwable t) { - future.setException(t); + completeExceptionally(t); throw t; } } @@ -103,14 +97,13 @@ public class FastContentWriter implements ListenableFuture<Boolean>, AutoCloseab try { out.close(completionHandler); } catch (Throwable t) { - future.setException(t); + completeExceptionally(t); throw t; } } - @Override public void addListener(Runnable listener, Executor executor) { - future.addListener(listener, executor); + whenCompleteAsync((__, ___) -> listener.run(), executor); } @Override @@ -123,34 +116,19 @@ public class FastContentWriter implements ListenableFuture<Boolean>, AutoCloseab return false; } - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public Boolean get() throws InterruptedException, ExecutionException { - return future.get(); - } - - @Override - public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return future.get(timeout, unit); - } - private class SimpleCompletionHandler implements CompletionHandler { @Override public void completed() { numPendingCompletions.decrementAndGet(); if (closed.get() && numPendingCompletions.get() == 0) { - future.set(true); + complete(true); } } @Override public void failed(Throwable t) { - future.setException(t); + completeExceptionally(t); } } } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java index ab989b89b1f..a188be6145f 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureCompletion.java @@ -1,7 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.AbstractFuture; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; /** * <p>This class provides an implementation of {@link CompletionHandler} that allows you to wait for either {@link @@ -13,16 +14,16 @@ import com.google.common.util.concurrent.AbstractFuture; * * @author Simon Thoresen Hult */ -public final class FutureCompletion extends AbstractFuture<Boolean> implements CompletionHandler { +public final class FutureCompletion extends CompletableFuture<Boolean> implements CompletionHandler { @Override public void completed() { - set(true); + complete(true); } @Override public void failed(Throwable t) { - setException(t); + completeExceptionally(t); } @Override @@ -34,4 +35,6 @@ public final class FutureCompletion extends AbstractFuture<Boolean> implements C public final boolean isCancelled() { return false; } + + public void addListener(Runnable r, Executor e) { whenCompleteAsync((__, ___) -> r.run(), e); } } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java index 9d0204f33c5..ba304d9e2de 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java @@ -1,39 +1,46 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.JdkFutureAdapters; -import com.google.common.util.concurrent.ListenableFuture; +import com.yahoo.concurrent.CompletableFutures; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * <p>This class implements a Future<Boolean> that is conjunction of zero or more other Future<Boolean>s, * i.e. it evaluates to <code>true</code> if, and only if, all its operands evaluate to <code>true</code>. To use this class, - * simply create an instance of it and add operands to it using the {@link #addOperand(ListenableFuture)} method.</p> - * TODO: consider rewriting usage of FutureConjunction to use CompletableFuture instead. + * simply create an instance of it and add operands to it using the {@link #addOperand(CompletableFuture)} method.</p> * * @author Simon Thoresen Hult */ -final class FutureConjunction implements ListenableFuture<Boolean> { +final class FutureConjunction implements Future<Boolean> { - private final List<ListenableFuture<Boolean>> operands = new LinkedList<>(); + private final List<CompletableFuture<Boolean>> operands = new LinkedList<>(); /** - * <p>Adds a ListenableFuture<Boolean> to this conjunction. This can be called at any time, even after having called + * <p>Adds a {@link CompletableFuture} to this conjunction. This can be called at any time, even after having called * {@link #get()} previously.</p> * * @param operand The operand to add to this conjunction. */ - public void addOperand(ListenableFuture<Boolean> operand) { + public void addOperand(CompletableFuture<Boolean> operand) { operands.add(operand); } - @Override public void addListener(Runnable listener, Executor executor) { - Futures.allAsList(operands).addListener(listener, executor); + CompletableFutures.allOf(operands) + .whenCompleteAsync((__, ___) -> listener.run(), executor); + } + + CompletableFuture<Boolean> completableFuture() { + return CompletableFutures.allOf(operands) + .thenApply(ops -> ops.stream().allMatch(bool -> bool)); } @Override diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java index b8073865667..2284c563f50 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java @@ -1,16 +1,18 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.AbstractFuture; import com.yahoo.jdisc.Response; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; + /** * This class provides an implementation of {@link ResponseHandler} that allows you to wait for a {@link Response} to * be returned. * * @author Simon Thoresen Hult */ -public final class FutureResponse extends AbstractFuture<Response> implements ResponseHandler { +public final class FutureResponse extends CompletableFuture<Response> implements ResponseHandler { private final ResponseHandler handler; @@ -38,6 +40,8 @@ public final class FutureResponse extends AbstractFuture<Response> implements Re }); } + public void addListener(Runnable r, Executor e) { whenCompleteAsync((__, ___) -> r.run(), e); } + /** * <p>Constructs a new FutureResponse that calls the given {@link ResponseHandler} when {@link * #handleResponse(Response)} is invoked.</p> @@ -50,7 +54,7 @@ public final class FutureResponse extends AbstractFuture<Response> implements Re @Override public ContentChannel handleResponse(Response response) { - set(response); + complete(response); return handler.handleResponse(response); } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java index c85aa6375af..c1457290904 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/RequestDispatch.java @@ -1,19 +1,20 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; +import com.yahoo.jdisc.References; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.ResourceReference; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.SharedResource; -import com.yahoo.jdisc.References; import java.nio.ByteBuffer; import java.util.Collections; -import java.util.concurrent.*; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * <p>This class provides a convenient way of safely dispatching a {@link Request}. Using this class you do not have to @@ -46,7 +47,7 @@ import java.util.List; * * @author Simon Thoresen Hult */ -public abstract class RequestDispatch implements ListenableFuture<Response>, ResponseHandler { +public abstract class RequestDispatch implements Future<Response>, ResponseHandler { private final FutureConjunction completions = new FutureConjunction(); private final FutureResponse futureResponse = new FutureResponse(this); @@ -106,22 +107,26 @@ public abstract class RequestDispatch implements ListenableFuture<Response>, Res * * @return A Future that can be waited for. */ - public final ListenableFuture<Response> dispatch() { + public final CompletableFuture<Response> dispatch() { try (FastContentWriter writer = new FastContentWriter(connect())) { for (ByteBuffer buf : requestContent()) { writer.write(buf); } completions.addOperand(writer); } - return this; + return CompletableFuture.allOf(completions.completableFuture(), futureResponse) + .thenApply(__ -> { + try { + return futureResponse.get(); + } catch (InterruptedException | ExecutionException e) { + throw new IllegalStateException(e); // Should not happens since both futures are complete + } + }); } - @Override public void addListener(Runnable listener, Executor executor) { - List<ListenableFuture<?>> combined = new ArrayList<>(2); - combined.add(completions); - combined.add(futureResponse); - Futures.allAsList(combined).addListener(listener, executor); + CompletableFuture.allOf(completions.completableFuture(), futureResponse) + .whenCompleteAsync((__, ___) -> listener.run(), executor); } @Override diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java index 377c8ecf4a9..9387171c1ac 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/ResponseDispatch.java @@ -1,15 +1,17 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ForwardingListenableFuture; -import com.google.common.util.concurrent.ListenableFuture; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.SharedResource; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * <p>This class provides a convenient way of safely dispatching a {@link Response}. It is similar in use to {@link @@ -34,7 +36,7 @@ import java.util.concurrent.Future; * * @author Simon Thoresen Hult */ -public abstract class ResponseDispatch extends ForwardingListenableFuture<Boolean> { +public abstract class ResponseDispatch implements Future<Boolean> { private final FutureConjunction completions = new FutureConjunction(); @@ -90,19 +92,14 @@ public abstract class ResponseDispatch extends ForwardingListenableFuture<Boolea * @param responseHandler The ResponseHandler to dispatch to. * @return A Future that can be waited for. */ - public final ListenableFuture<Boolean> dispatch(ResponseHandler responseHandler) { + public final CompletableFuture<Boolean> dispatch(ResponseHandler responseHandler) { try (FastContentWriter writer = new FastContentWriter(connect(responseHandler))) { for (ByteBuffer buf : responseContent()) { writer.write(buf); } completions.addOperand(writer); } - return this; - } - - @Override - protected final ListenableFuture<Boolean> delegate() { - return completions; + return completions.completableFuture(); } @Override @@ -115,6 +112,15 @@ public abstract class ResponseDispatch extends ForwardingListenableFuture<Boolea return false; } + @Override public boolean isDone() { return completions.isDone(); } + + @Override public Boolean get() throws InterruptedException, ExecutionException { return completions.get(); } + + @Override + public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + return completions.get(timeout, unit); + } + /** * <p>Factory method for creating a ResponseDispatch with a {@link Response} that has the given status code, and * ByteBuffer content.</p> diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FastContentWriterTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FastContentWriterTestCase.java index aa6c4ce3b1b..45bc230896f 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FastContentWriterTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FastContentWriterTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.MoreExecutors; import org.junit.Test; import org.mockito.Mockito; @@ -188,7 +187,7 @@ public class FastContentWriterTestCase { ReadableContentChannel buf = new ReadableContentChannel(); FastContentWriter out = new FastContentWriter(buf); RunnableLatch listener = new RunnableLatch(); - out.addListener(listener, MoreExecutors.directExecutor()); + out.addListener(listener, Runnable::run); out.write(new byte[] { 6, 9 }); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureCompletionTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureCompletionTestCase.java index 6c3803f4f56..ef63b200b5f 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureCompletionTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureCompletionTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.MoreExecutors; import org.junit.Test; import java.util.concurrent.ExecutionException; @@ -91,14 +90,14 @@ public class FutureCompletionTestCase { public void requireThatCompletionCanBeListenedTo() throws InterruptedException { FutureCompletion completion = new FutureCompletion(); RunnableLatch listener = new RunnableLatch(); - completion.addListener(listener, MoreExecutors.directExecutor()); + completion.addListener(listener, Runnable::run); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); completion.completed(); assertTrue(listener.await(600, TimeUnit.SECONDS)); completion = new FutureCompletion(); listener = new RunnableLatch(); - completion.addListener(listener, MoreExecutors.directExecutor()); + completion.addListener(listener, Runnable::run); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); completion.failed(new Throwable()); assertTrue(listener.await(600, TimeUnit.SECONDS)); diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureConjunctionTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureConjunctionTestCase.java index 346b06e0f23..1aa78a16dfc 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureConjunctionTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureConjunctionTestCase.java @@ -1,41 +1,37 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.AbstractFuture; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; import org.junit.Test; -import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Simon Thoresen Hult */ public class FutureConjunctionTestCase { - private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); + private final ExecutorService executor = Executors.newCachedThreadPool(); @Test public void requireThatAllFuturesAreWaitedFor() throws Exception { final CountDownLatch latch = new CountDownLatch(1); FutureConjunction future = new FutureConjunction(); - future.addOperand(executor.submit(new Callable<Boolean>() { - - @Override - public Boolean call() throws Exception { - return latch.await(600, TimeUnit.SECONDS); - } - })); + CompletableFuture<Boolean> cf = new CompletableFuture<>(); + cf.completeAsync(() -> { + try { return latch.await(600, TimeUnit.SECONDS); } + catch (InterruptedException e) { return false; } + }, executor); + future.addOperand(cf); try { future.get(100, TimeUnit.MILLISECONDS); fail(); @@ -118,7 +114,7 @@ public class FutureConjunctionTestCase { public void requireThatConjunctionCanBeListenedTo() throws InterruptedException { FutureConjunction conjunction = new FutureConjunction(); RunnableLatch listener = new RunnableLatch(); - conjunction.addListener(listener, MoreExecutors.directExecutor()); + conjunction.addListener(listener, Runnable::run); assertTrue(listener.await(600, TimeUnit.SECONDS)); conjunction = new FutureConjunction(); @@ -127,7 +123,7 @@ public class FutureConjunctionTestCase { FutureBoolean bar = new FutureBoolean(); conjunction.addOperand(bar); listener = new RunnableLatch(); - conjunction.addListener(listener, MoreExecutors.directExecutor()); + conjunction.addListener(listener, Runnable::run); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); foo.set(true); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); @@ -140,7 +136,7 @@ public class FutureConjunctionTestCase { bar = new FutureBoolean(); conjunction.addOperand(bar); listener = new RunnableLatch(); - conjunction.addListener(listener, MoreExecutors.directExecutor()); + conjunction.addListener(listener, Runnable::run); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); bar.set(true); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); @@ -190,14 +186,14 @@ public class FutureConjunctionTestCase { return foo.isCancelled(); } - private static class FutureBoolean extends AbstractFuture<Boolean> { + private static class FutureBoolean extends CompletableFuture<Boolean> { public boolean set(Boolean val) { - return super.set(val); + return super.complete(val); } } - private static class MyFuture extends AbstractFuture<Boolean> { + private static class MyFuture extends CompletableFuture<Boolean> { final boolean value; final boolean isDone; @@ -236,19 +232,19 @@ public class FutureConjunctionTestCase { return value; } - static ListenableFuture<Boolean> newInstance(boolean value) { + static CompletableFuture<Boolean> newInstance(boolean value) { return new MyFuture(value, false, false, false); } - static ListenableFuture<Boolean> newIsDone(boolean isDone) { + static CompletableFuture<Boolean> newIsDone(boolean isDone) { return new MyFuture(false, isDone, false, false); } - static ListenableFuture<Boolean> newCanCancel(boolean canCancel) { + static CompletableFuture<Boolean> newCanCancel(boolean canCancel) { return new MyFuture(false, false, canCancel, false); } - static ListenableFuture<Boolean> newIsCancelled(boolean isCancelled) { + static CompletableFuture<Boolean> newIsCancelled(boolean isCancelled) { return new MyFuture(false, false, false, isCancelled); } } diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureResponseTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureResponseTestCase.java index 440698257a4..398f288e307 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureResponseTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/FutureResponseTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.MoreExecutors; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.test.NonWorkingContentChannel; import org.junit.Test; @@ -73,7 +72,7 @@ public class FutureResponseTestCase { public void requireThatResponseCanBeListenedTo() throws InterruptedException { FutureResponse response = new FutureResponse(); RunnableLatch listener = new RunnableLatch(); - response.addListener(listener, MoreExecutors.directExecutor()); + response.addListener(listener, Runnable::run); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); response.handleResponse(new Response(Response.Status.OK)); assertTrue(listener.await(600, TimeUnit.SECONDS)); diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/RequestDispatchTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/RequestDispatchTestCase.java index 3b49d1f349e..6ec78f01733 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/RequestDispatchTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/RequestDispatchTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.MoreExecutors; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.application.ContainerBuilder; @@ -17,12 +16,12 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Simon Thoresen Hult @@ -218,7 +217,7 @@ public class RequestDispatchTestCase { protected Request newRequest() { return new Request(driver, URI.create("http://localhost/")); } - }.dispatch().addListener(listener, MoreExecutors.directExecutor()); + }.dispatch().whenComplete((__, ___) -> listener.run()); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); ContentChannel responseContent = ResponseDispatch.newInstance(Response.Status.OK) .connect(requestHandler.responseHandler); diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ResponseDispatchTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ResponseDispatchTestCase.java index f9a5c22837f..4006ab072cb 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ResponseDispatchTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ResponseDispatchTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.MoreExecutors; import com.yahoo.jdisc.Response; import org.junit.Test; @@ -14,13 +13,13 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Simon Thoresen Hult @@ -179,7 +178,7 @@ public class ResponseDispatchTestCase { ReadableContentChannel responseContent = new ReadableContentChannel(); ResponseDispatch.newInstance(6, ByteBuffer.allocate(9)) .dispatch(new MyResponseHandler(responseContent)) - .addListener(listener, MoreExecutors.directExecutor()); + .whenComplete((__, ___) -> listener.run()); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); assertNotNull(responseContent.read()); assertFalse(listener.await(100, TimeUnit.MILLISECONDS)); diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ThreadedRequestHandlerTestCase.java b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ThreadedRequestHandlerTestCase.java index 71f207bbbff..f639877b87b 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ThreadedRequestHandlerTestCase.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/handler/ThreadedRequestHandlerTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.handler; -import com.google.common.util.concurrent.ListenableFuture; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.application.ContainerBuilder; @@ -12,18 +11,19 @@ import org.junit.Test; import java.net.URI; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Simon Thoresen Hult @@ -159,8 +159,8 @@ public class ThreadedRequestHandlerTestCase { return driver; } - private static ListenableFuture<Response> dispatchRequest(final CurrentContainer container, final String uri, - final ByteBuffer... content) { + private static CompletableFuture<Response> dispatchRequest(final CurrentContainer container, final String uri, + final ByteBuffer... content) { return new RequestDispatch() { @Override diff --git a/jdisc_core/src/test/java/com/yahoo/jdisc/test/ServerProviderConformanceTestTest.java b/jdisc_core/src/test/java/com/yahoo/jdisc/test/ServerProviderConformanceTestTest.java index 01b1e72d0b6..c9c7ec1db48 100644 --- a/jdisc_core/src/test/java/com/yahoo/jdisc/test/ServerProviderConformanceTestTest.java +++ b/jdisc_core/src/test/java/com/yahoo/jdisc/test/ServerProviderConformanceTestTest.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.jdisc.test; -import com.google.common.util.concurrent.SettableFuture; import com.google.inject.Inject; import com.google.inject.Module; import com.google.inject.util.Modules; @@ -20,6 +19,7 @@ import java.net.URI; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Collections; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -571,7 +571,7 @@ public class ServerProviderConformanceTestTest extends ServerProviderConformance try { request = new Request(server.container, URI.create("http://localhost/")); } catch (Throwable t) { - responseHandler.response.set(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)); + responseHandler.response.complete(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)); return responseHandler; } try { @@ -581,7 +581,7 @@ public class ServerProviderConformanceTestTest extends ServerProviderConformance } tryClose(out); } catch (Throwable t) { - responseHandler.response.set(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)); + responseHandler.response.complete(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)); // Simulate handling the failure. t.getMessage(); return responseHandler; @@ -594,13 +594,13 @@ public class ServerProviderConformanceTestTest extends ServerProviderConformance private static class MyResponseHandler implements ResponseHandler { - final SettableFuture<Response> response = SettableFuture.create(); - final SettableFuture<String> content = SettableFuture.create(); + final CompletableFuture<Response> response = new CompletableFuture<>(); + final CompletableFuture<String> content = new CompletableFuture<>(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); @Override public ContentChannel handleResponse(final Response response) { - this.response.set(response); + this.response.complete(response); return new ContentChannel() { @Override @@ -613,7 +613,7 @@ public class ServerProviderConformanceTestTest extends ServerProviderConformance @Override public void close(final CompletionHandler handler) { - content.set(new String(out.toByteArray(), StandardCharsets.UTF_8)); + content.complete(new String(out.toByteArray(), StandardCharsets.UTF_8)); tryComplete(handler); } }; |