Skip to content

Commit

Permalink
Remove usage of Handler/AsyncResult idiom.
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Sep 12, 2024
1 parent a718167 commit 467171f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 34 deletions.
21 changes: 8 additions & 13 deletions vertx-grpc/src/main/java/io/vertx/grpc/VertxServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +106,7 @@ private ActualServer(VertxInternal vertx,
.build();
}

void start(ContextInternal context, Handler<AsyncResult<Void>> completionHandler) {
void start(ContextInternal context, Completable<Void> completionHandler) {
boolean start = count.getAndIncrement() == 0;
context.runOnContext(v -> {
if (contextLocal.get() == null) {
Expand All @@ -125,7 +120,7 @@ void start(ContextInternal context, Handler<AsyncResult<Void>> completionHandler
return null;
}).onComplete(completionHandler);
} else {
completionHandler.handle(Future.succeededFuture());
completionHandler.succeed();
}
});
}
Expand Down Expand Up @@ -171,21 +166,21 @@ void stop(ContextInternal context, Promise<Void> promise) {

@Override
public VertxServer start() throws IOException {
return start(ar -> {});
return start((res, err) -> {});
}

public VertxServer start(Handler<AsyncResult<Void>> completionHandler) {
public VertxServer start(Completable<Void> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public Future<HelloReply> 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())");
}
Expand All @@ -64,7 +64,7 @@ public Future<HelloReply> sayHello(HelloRequest request) {
test.complete();
});
} else {
should.fail(ar.cause());
should.fail(err);
}
});
}
Expand Down
12 changes: 3 additions & 9 deletions vertx-grpc/src/test/java/io/vertx/ext/grpc/GrpcTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,15 @@ Future<Void> startServer(BindableService service, VertxServerBuilder builder) {

Future<Void> startServer(ServerServiceDefinition service) {
Promise<Void> 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<AsyncResult<Void>> completionHandler) {
void startServer(ServerServiceDefinition service, Completable<Void> completionHandler) {
startServer(service, VertxServerBuilder.forPort(vertx, port), completionHandler);
}

void startServer(ServerServiceDefinition service, VertxServerBuilder builder, Handler<AsyncResult<Void>> completionHandler) {
void startServer(ServerServiceDefinition service, VertxServerBuilder builder, Completable<Void> completionHandler) {
server = builder
.addService(service)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,10 +27,17 @@ public void testNativeTransportDisabled(TestContext ctx) {
}

private void testInternal(TestContext ctx, Vertx vertx) {
Handler<AsyncResult<Void>> 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() {
Expand Down
12 changes: 6 additions & 6 deletions vertx-grpc/src/test/java/io/vertx/ext/grpc/RpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public Future<HelloReply> 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;
}

Expand All @@ -151,9 +151,9 @@ public Future<HelloReply> 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();
Expand Down

0 comments on commit 467171f

Please sign in to comment.