From 467171f606d394871a1ea2d844dd6a1925ea900d Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Thu, 12 Sep 2024 16:26:53 +0200 Subject: [PATCH] Remove usage of Handler/AsyncResult idiom. --- .../main/java/io/vertx/grpc/VertxServer.java | 21 +++++++------------ .../vertx/ext/grpc/CommandDecoratorTest.java | 6 +++--- .../java/io/vertx/ext/grpc/GrpcTestBase.java | 12 +++-------- .../vertx/ext/grpc/NativeTransportTest.java | 12 ++++++++--- .../test/java/io/vertx/ext/grpc/RpcTest.java | 12 +++++------ 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/vertx-grpc/src/main/java/io/vertx/grpc/VertxServer.java b/vertx-grpc/src/main/java/io/vertx/grpc/VertxServer.java index 19e4573e..19d7a916 100644 --- a/vertx-grpc/src/main/java/io/vertx/grpc/VertxServer.java +++ b/vertx-grpc/src/main/java/io/vertx/grpc/VertxServer.java @@ -18,12 +18,7 @@ import io.grpc.Server; import io.grpc.netty.NettyServerBuilder; import io.netty.handler.ssl.SslContext; -import io.vertx.core.AsyncResult; -import io.vertx.core.Closeable; -import io.vertx.core.Future; -import io.vertx.core.Handler; -import io.vertx.core.Promise; -import io.vertx.core.VertxException; +import io.vertx.core.*; import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpVersion; import io.vertx.core.internal.ContextInternal; @@ -111,7 +106,7 @@ private ActualServer(VertxInternal vertx, .build(); } - void start(ContextInternal context, Handler> completionHandler) { + void start(ContextInternal context, Completable completionHandler) { boolean start = count.getAndIncrement() == 0; context.runOnContext(v -> { if (contextLocal.get() == null) { @@ -125,7 +120,7 @@ void start(ContextInternal context, Handler> completionHandler return null; }).onComplete(completionHandler); } else { - completionHandler.handle(Future.succeededFuture()); + completionHandler.succeed(); } }); } @@ -171,21 +166,21 @@ void stop(ContextInternal context, Promise promise) { @Override public VertxServer start() throws IOException { - return start(ar -> {}); + return start((res, err) -> {}); } - public VertxServer start(Handler> completionHandler) { + public VertxServer start(Completable completionHandler) { if (id.port > 0) { actual = map.computeIfAbsent(id, id -> new ActualServer(context.owner(), id, options, builder, commandDecorator)); } else { actual = new ActualServer(context.owner(), id, options, builder, commandDecorator); } - actual.start(context, ar1 -> { - if (ar1.succeeded()) { + actual.start(context, (res, err) -> { + if (err == null) { hook = this::shutdown; context.addCloseHook(hook); } - completionHandler.handle(ar1); + completionHandler.complete(res, err); }); return this; } diff --git a/vertx-grpc/src/test/java/io/vertx/ext/grpc/CommandDecoratorTest.java b/vertx-grpc/src/test/java/io/vertx/ext/grpc/CommandDecoratorTest.java index 2b769320..9a600c70 100644 --- a/vertx-grpc/src/test/java/io/vertx/ext/grpc/CommandDecoratorTest.java +++ b/vertx-grpc/src/test/java/io/vertx/ext/grpc/CommandDecoratorTest.java @@ -38,8 +38,8 @@ public Future sayHello(HelloRequest request) { } }).build(); - server.start(ar -> { - if (ar.succeeded()) { + server.start((res, err) -> { + if (err == null) { if (server.getRawServer() == null) { should.fail("The underlying server not exposed (server.getRawServer())"); } @@ -64,7 +64,7 @@ public Future sayHello(HelloRequest request) { test.complete(); }); } else { - should.fail(ar.cause()); + should.fail(err); } }); } diff --git a/vertx-grpc/src/test/java/io/vertx/ext/grpc/GrpcTestBase.java b/vertx-grpc/src/test/java/io/vertx/ext/grpc/GrpcTestBase.java index 22227118..1f260274 100644 --- a/vertx-grpc/src/test/java/io/vertx/ext/grpc/GrpcTestBase.java +++ b/vertx-grpc/src/test/java/io/vertx/ext/grpc/GrpcTestBase.java @@ -72,21 +72,15 @@ Future startServer(BindableService service, VertxServerBuilder builder) { Future startServer(ServerServiceDefinition service) { Promise promise = Promise.promise(); - startServer(service, ar -> { - if (ar.succeeded()) { - promise.complete(); - } else { - promise.fail(ar.cause()); - } - }); + startServer(service,promise); return promise.future(); } - void startServer(ServerServiceDefinition service, Handler> completionHandler) { + void startServer(ServerServiceDefinition service, Completable completionHandler) { startServer(service, VertxServerBuilder.forPort(vertx, port), completionHandler); } - void startServer(ServerServiceDefinition service, VertxServerBuilder builder, Handler> completionHandler) { + void startServer(ServerServiceDefinition service, VertxServerBuilder builder, Completable completionHandler) { server = builder .addService(service) .build() diff --git a/vertx-grpc/src/test/java/io/vertx/ext/grpc/NativeTransportTest.java b/vertx-grpc/src/test/java/io/vertx/ext/grpc/NativeTransportTest.java index ab93434d..9aa2cc0b 100644 --- a/vertx-grpc/src/test/java/io/vertx/ext/grpc/NativeTransportTest.java +++ b/vertx-grpc/src/test/java/io/vertx/ext/grpc/NativeTransportTest.java @@ -1,8 +1,7 @@ package io.vertx.ext.grpc; import examples.GreeterGrpc; -import io.vertx.core.Vertx; -import io.vertx.core.VertxOptions; +import io.vertx.core.*; import io.vertx.core.impl.VertxBootstrapImpl; import io.vertx.core.spi.transport.Transport; import io.vertx.ext.unit.TestContext; @@ -28,10 +27,17 @@ public void testNativeTransportDisabled(TestContext ctx) { } private void testInternal(TestContext ctx, Vertx vertx) { + Handler> latch = ctx.asyncAssertSuccess(); VertxServerBuilder.forPort(vertx, 0) .addService(new GreeterGrpc.GreeterImplBase() { }) .build() - .start(ctx.asyncAssertSuccess()); + .start((result, failure) -> { + if (failure == null) { + latch.handle(Future.failedFuture(failure)); + } else { + latch.handle(Future.succeededFuture()); + } + }); } private void assumeNativeTransport() { diff --git a/vertx-grpc/src/test/java/io/vertx/ext/grpc/RpcTest.java b/vertx-grpc/src/test/java/io/vertx/ext/grpc/RpcTest.java index 007eace0..3eeff786 100644 --- a/vertx-grpc/src/test/java/io/vertx/ext/grpc/RpcTest.java +++ b/vertx-grpc/src/test/java/io/vertx/ext/grpc/RpcTest.java @@ -140,9 +140,9 @@ public Future sayHello(HelloRequest request) { server = VertxServerBuilder.forPort(vertx, port) .addService(ServerInterceptors.intercept(service, BlockingServerInterceptor.wrap(vertx, blockingInterceptor))) .build() - .start(ar -> { - if (ar.failed()) { - should.fail(ar.cause()); + .start((res, err) -> { + if (err != null) { + should.fail(err); return; } @@ -151,9 +151,9 @@ public Future sayHello(HelloRequest request) { .build(); VertxGreeterGrpc.GreeterVertxStub stub = VertxGreeterGrpc.newVertxStub(channel); - stub.sayHello(HelloRequest.newBuilder().setName("Julien").build()).onComplete(should.asyncAssertFailure(err -> { - should.assertTrue(err instanceof StatusRuntimeException); - StatusRuntimeException sre = (StatusRuntimeException) err; + stub.sayHello(HelloRequest.newBuilder().setName("Julien").build()).onComplete(should.asyncAssertFailure(failure -> { + should.assertTrue(failure instanceof StatusRuntimeException); + StatusRuntimeException sre = (StatusRuntimeException) failure; should.assertEquals(Status.ABORTED, sre.getStatus()); should.assertEquals("mdvalue", sre.getTrailers().get(mdKey)); test.complete();