Skip to content

Commit

Permalink
Merge pull request #15911 from stuartwdouglas/grpc-port
Browse files Browse the repository at this point in the history
GRPC port 9001 for tests
  • Loading branch information
geoand authored Mar 22, 2021
2 parents 3306277 + 0a42b6c commit fd713d3
Show file tree
Hide file tree
Showing 37 changed files with 76 additions and 42 deletions.
3 changes: 2 additions & 1 deletion docs/src/main/asciidoc/grpc-getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ annotated method on a worker thread instead of the I/O thread (event-loop).
The services are _served_ by a _server_.
Available services (_CDI beans_) are automatically registered and exposed.

By default, the server is exposed on `localhost:9000`, and uses _plain-text_ (so no TLS).
By default, the server is exposed on `localhost:9000`, and uses _plain-text_ (so no TLS) when
running normally, and `localhost:9001` for tests.

Run the application using: `mvn quarkus:dev`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
Expand Down Expand Up @@ -95,6 +96,7 @@ void buildContainerBean(BuildProducer<AdditionalBeanBuildItem> beans,
@Record(value = ExecutionTime.RUNTIME_INIT)
ServiceStartBuildItem build(GrpcServerRecorder recorder, GrpcConfiguration config,
ShutdownContextBuildItem shutdown, List<BindableServiceBuildItem> bindables,
LaunchModeBuildItem launchModeBuildItem,
VertxBuildItem vertx) {

// Build the list of blocking methods per service implementation
Expand All @@ -106,7 +108,7 @@ ServiceStartBuildItem build(GrpcServerRecorder recorder, GrpcConfiguration confi
}

if (!bindables.isEmpty()) {
recorder.initializeGrpcServer(vertx.getVertx(), config, shutdown, blocking);
recorder.initializeGrpcServer(vertx.getVertx(), config, shutdown, blocking, launchModeBuildItem.getLaunchMode());
return new ServiceStartBuildItem(GRPC_SERVER);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GrpcServiceTestBase {

@BeforeEach
public void init() throws Exception {
channel = ManagedChannelBuilder.forAddress("localhost", 9000)
channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void init() throws Exception {
SslContext sslcontext = GrpcSslContexts.forClient()
.trustManager(createTrustAllTrustManager())
.build();
channel = NettyChannelBuilder.forAddress("localhost", 9000)
channel = NettyChannelBuilder.forAddress("localhost", 9001)
.sslContext(sslcontext)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void init() throws Exception {
SslContext sslcontext = GrpcSslContexts.forClient()
.trustManager(createTrustAllTrustManager())
.build();
channel = NettyChannelBuilder.forAddress("localhost", 9000)
channel = NettyChannelBuilder.forAddress("localhost", 9001)
.sslContext(sslcontext)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void init() throws Exception {
SslContext sslcontext = GrpcSslContexts.forClient()
.trustManager(createTrustAllTrustManager())
.build();
channel = NettyChannelBuilder.forAddress("localhost", 9000)
channel = NettyChannelBuilder.forAddress("localhost", 9001)
.sslContext(sslcontext)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class BlockingServiceTest {

@BeforeEach
public void init() {
channel = ManagedChannelBuilder.forAddress("localhost", 9000)
channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ServerInterceptorPriorityReversedTest {

@BeforeEach
public void init() {
channel = ManagedChannelBuilder.forAddress("localhost", 9000)
channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ServerInterceptorPriorityTest {

@BeforeEach
public void init() {
channel = ManagedChannelBuilder.forAddress("localhost", 9000)
channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ServerInterceptorRegistrationTest {

@BeforeEach
public void init() throws Exception {
channel = ManagedChannelBuilder.forAddress("localhost", 9000)
channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Set<String> getThreadsUsedFor100Requests() throws InterruptedException, Executio
List<Callable<String>> calls = new ArrayList<>();
for (int i = 0; i < requestNo; i++) {
calls.add(() -> {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9000)
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9001)
.usePlaintext()
.build();
HelloReply reply = GreeterGrpc.newBlockingStub(channel)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
quarkus.grpc.clients.reflection-service.host=localhost
quarkus.grpc.clients.reflection-service.port=9001
quarkus.grpc.clients.test-service.host=localhost
quarkus.grpc.clients.test-service.port=9001
quarkus.grpc.clients.greeter-service.host=localhost
quarkus.grpc.clients.greeter-service.port=9001
quarkus.grpc.server.enable-reflection-service=true
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
quarkus.grpc.clients.blocking-test.host=localhost
quarkus.grpc.clients.blocking-test.port=9001
quarkus.grpc.server.enable-reflection-service=true
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
quarkus.grpc.clients.greeter.host=localhost
quarkus.grpc.clients.service3.host=localhost
quarkus.grpc.clients.greeter.port=9001
quarkus.grpc.clients.service3.host=localhost
quarkus.grpc.clients.service3.port=9001
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
quarkus.grpc.clients.hello.host=localhost
quarkus.grpc.clients.hello.port=9001
quarkus.grpc.clients.hello.ssl.trust-store=src/test/resources/tls-from-file/ca.pem

quarkus.grpc.server.ssl.certificate=src/test/resources/tls-from-file/server.pem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
quarkus.grpc.clients.health-service.host=localhost
quarkus.grpc.clients.health-service.port=9001
quarkus.grpc.clients.foo.host=localhost
quarkus.grpc.server.port=9000
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
quarkus.grpc.clients.hello-service.host=localhost
quarkus.grpc.clients.hello-service.port=9001
quarkus.grpc.clients.hello-service.keep-alive-timeout=1s
quarkus.grpc.clients.hello-service-2.host=localhost
quarkus.grpc.clients.hello-service-2.retry=true
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
quarkus.grpc.server.instances=3
quarkus.grpc.clients.hello-service.host=localhost
quarkus.grpc.clients.hello-service.port=9001
quarkus.grpc.clients.hello-service.keep-alive-timeout=1s
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
quarkus.grpc.clients.reflection-service.host=localhost
quarkus.grpc.clients.reflection-service.port=9001
quarkus.grpc.server.enable-reflection-service=true
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
quarkus.grpc.server.instances=1
quarkus.grpc.clients.hello-service.host=localhost
quarkus.grpc.clients.hello-service.port=9001
quarkus.grpc.clients.hello-service.keep-alive-timeout=1s
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class GrpcServerRecorder {
public void initializeGrpcServer(RuntimeValue<Vertx> vertxSupplier,
GrpcConfiguration cfg,
ShutdownContext shutdown,
Map<String, List<String>> blockingMethodsPerServiceImplementationClass) {
Map<String, List<String>> blockingMethodsPerServiceImplementationClass, LaunchMode launchMode) {
GrpcContainer grpcContainer = Arc.container().instance(GrpcContainer.class).get();
if (grpcContainer == null) {
throw new IllegalStateException("gRPC not initialized, GrpcContainer not found");
Expand All @@ -83,29 +83,29 @@ public void initializeGrpcServer(RuntimeValue<Vertx> vertxSupplier,
this.blockingMethodsPerService = blockingMethodsPerServiceImplementationClass;

GrpcServerConfiguration configuration = cfg.server;
final boolean devMode = ProfileManager.getLaunchMode() == LaunchMode.DEVELOPMENT;

if (devMode) {
if (launchMode == LaunchMode.DEVELOPMENT) {
// start single server, not in a verticle, regardless of the configuration.instances
// for reason unknown to me, verticles occasionally get undeployed on dev mode reload
if (GrpcServerReloader.getServer() == null) {
devModeStart(grpcContainer, vertx, configuration, shutdown);
devModeStart(grpcContainer, vertx, configuration, shutdown, launchMode);
} else {
devModeReload(grpcContainer);
}
} else {
prodStart(grpcContainer, vertx, configuration);
prodStart(grpcContainer, vertx, configuration, launchMode);
}
}

private void prodStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerConfiguration configuration) {
private void prodStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerConfiguration configuration,
LaunchMode launchMode) {
CompletableFuture<Void> startResult = new CompletableFuture<>();

vertx.deployVerticle(
new Supplier<Verticle>() {
@Override
public Verticle get() {
return new GrpcServerVerticle(configuration, grpcContainer);
return new GrpcServerVerticle(configuration, grpcContainer, launchMode);
}
},
new DeploymentOptions().setInstances(configuration.instances),
Expand All @@ -115,7 +115,7 @@ public void handle(AsyncResult<String> result) {
if (result.failed()) {
startResult.completeExceptionally(result.cause());
} else {
GrpcServerRecorder.this.postStartup(grpcContainer, configuration);
GrpcServerRecorder.this.postStartup(grpcContainer, configuration, launchMode == LaunchMode.TEST);

startResult.complete(null);
}
Expand All @@ -134,7 +134,7 @@ public void handle(AsyncResult<String> result) {
}
}

private void postStartup(GrpcContainer grpcContainer, GrpcServerConfiguration configuration) {
private void postStartup(GrpcContainer grpcContainer, GrpcServerConfiguration configuration, boolean test) {
grpcContainer.getHealthStorage().stream().forEach(new Consumer<GrpcHealthStorage>() { //NOSONAR
@Override
public void accept(GrpcHealthStorage storage) {
Expand All @@ -152,22 +152,22 @@ public void accept(BindableService service) {
}
});
LOGGER.infof("gRPC Server started on %s:%d [SSL enabled: %s]",
configuration.host, configuration.port, !configuration.plainText);
configuration.host, test ? configuration.testPort : configuration.port, !configuration.plainText);
}

private void devModeStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerConfiguration configuration,
ShutdownContext shutdown) {
ShutdownContext shutdown, LaunchMode launchMode) {
CompletableFuture<Boolean> future = new CompletableFuture<>();

VertxServer vertxServer = buildServer(vertx, configuration, grpcContainer, true)
VertxServer vertxServer = buildServer(vertx, configuration, grpcContainer, launchMode)
.start(new Handler<AsyncResult<Void>>() { // NOSONAR
@Override
public void handle(AsyncResult<Void> ar) {
if (ar.failed()) {
LOGGER.error("Unable to start the gRPC server", ar.cause());
future.completeExceptionally(ar.cause());
} else {
postStartup(grpcContainer, configuration);
postStartup(grpcContainer, configuration, false);
future.complete(true);
grpcVerticleCount.incrementAndGet();
}
Expand Down Expand Up @@ -281,9 +281,10 @@ public static int getVerticleCount() {
}

private VertxServer buildServer(Vertx vertx, GrpcServerConfiguration configuration,
GrpcContainer grpcContainer, boolean devMode) {
GrpcContainer grpcContainer, LaunchMode launchMode) {
VertxServerBuilder builder = VertxServerBuilder
.forAddress(vertx, configuration.host, configuration.port);
.forAddress(vertx, configuration.host,
launchMode == LaunchMode.TEST ? configuration.testPort : configuration.port);

AtomicBoolean usePlainText = new AtomicBoolean();
builder.useSsl(new Handler<HttpServerOptions>() { // NOSONAR
Expand Down Expand Up @@ -352,7 +353,7 @@ public void handle(HttpServerOptions options) {
builder.intercept(serverInterceptor);
}

if (devMode) {
if (launchMode == LaunchMode.DEVELOPMENT) {
builder.commandDecorator(new Consumer<Runnable>() {
@Override
public void accept(Runnable command) {
Expand All @@ -374,20 +375,23 @@ public void handle(AsyncResult<Boolean> result) {
}

LOGGER.debugf("Starting gRPC Server on %s:%d [SSL enabled: %s]...",
configuration.host, configuration.port, !usePlainText.get());
configuration.host, launchMode == LaunchMode.TEST ? configuration.testPort : configuration.port,
!usePlainText.get());

return builder.build();
}

private class GrpcServerVerticle extends AbstractVerticle {
private final GrpcServerConfiguration configuration;
private final GrpcContainer grpcContainer;
private final LaunchMode launchMode;

private VertxServer grpcServer;

GrpcServerVerticle(GrpcServerConfiguration configuration, GrpcContainer grpcContainer) {
GrpcServerVerticle(GrpcServerConfiguration configuration, GrpcContainer grpcContainer, LaunchMode launchMode) {
this.configuration = configuration;
this.grpcContainer = grpcContainer;
this.launchMode = launchMode;
}

@Override
Expand All @@ -397,7 +401,7 @@ public void start(Promise<Void> startPromise) {
"Unable to find bean exposing the `BindableService` interface - not starting the gRPC server");
return;
}
grpcServer = buildServer(getVertx(), configuration, grpcContainer, false)
grpcServer = buildServer(getVertx(), configuration, grpcContainer, launchMode)
.start(new Handler<AsyncResult<Void>>() { // NOSONAR
@Override
public void handle(AsyncResult<Void> ar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class GrpcServerConfiguration {
@ConfigItem(defaultValue = "9000")
public int port;

/**
* The gRPC Server port used for tests.
*/
@ConfigItem(defaultValue = "9001")
public int testPort;

/**
* The gRPC server host.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
quarkus.grpc.clients.health-service.host=localhost
quarkus.grpc.clients.health-service.host=localhost
quarkus.grpc.clients.health-service.port=9001
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
quarkus.grpc.clients.health-service.host=localhost
quarkus.grpc.clients.health-service.port=9001
quarkus.grpc.server.health.enabled=false
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
quarkus.grpc.clients.hello.host=localhost
quarkus.grpc.clients.hello.host=localhost
%test.quarkus.grpc.clients.hello.port=9001
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
quarkus.grpc.clients.hello.host=localhost
%test.quarkus.grpc.clients.hello.port=9001
quarkus.grpc.clients.hello.ssl.certificate=src/main/resources/tls/client.pem
quarkus.grpc.clients.hello.ssl.key=src/main/resources/tls/client.key
quarkus.grpc.clients.hello.ssl.trust-store=src/main/resources/tls/ca.pem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void init() throws SSLException {
new File("src/main/resources/tls/client.key"));
SslContext context = builder.build();

channel = NettyChannelBuilder.forAddress("localhost", 9000)
channel = NettyChannelBuilder.forAddress("localhost", 9001)
.sslContext(context)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
quarkus.grpc.clients.hello.host=localhost
%test.quarkus.grpc.clients.hello.port=9001
quarkus.grpc.server.compression=gzip
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HelloWorldServiceTest {

@BeforeEach
public void init() {
channel = ManagedChannelBuilder.forAddress("localhost", 9000).usePlaintext().build();
channel = ManagedChannelBuilder.forAddress("localhost", 9001).usePlaintext().build();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
quarkus.grpc.clients.hello.host=localhost
quarkus.grpc.clients.hello.host=localhost
%test.quarkus.grpc.clients.hello.port=9001
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HelloWorldServiceTest {

@BeforeEach
public void init() {
channel = ManagedChannelBuilder.forAddress("localhost", 9000).usePlaintext().build();
channel = ManagedChannelBuilder.forAddress("localhost", 9001).usePlaintext().build();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
quarkus.grpc.clients.hello.host=localhost
quarkus.grpc.clients.hello.host=localhost
%test.quarkus.grpc.clients.hello.port=9001
Loading

0 comments on commit fd713d3

Please sign in to comment.