-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve testing gRPC services with random ports #34135
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/GrpcTestPortUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.grpc.runtime; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.quarkus.grpc.runtime.config.GrpcServerConfiguration; | ||
import io.quarkus.grpc.runtime.config.SslServerConfig; | ||
import io.vertx.core.http.ClientAuth; | ||
|
||
public final class GrpcTestPortUtils { | ||
private GrpcTestPortUtils() { | ||
} | ||
|
||
public static int testPort(GrpcServerConfiguration serverConfiguration) { | ||
if (serverConfiguration.useSeparateServer) { | ||
if (serverConfiguration.testPort == 0) { | ||
return testPort("grpc.server"); | ||
} | ||
return serverConfiguration.testPort; | ||
} | ||
if (isHttpsConfigured(serverConfiguration.ssl) || !serverConfiguration.plainText) { | ||
int httpsTestPort = port("quarkus.http.test-ssl-port"); | ||
if (httpsTestPort == 0) { | ||
return testPort("https"); | ||
} | ||
return httpsTestPort; | ||
} | ||
return testPort("http"); | ||
} | ||
|
||
private static boolean isHttpsConfigured(SslServerConfig ssl) { | ||
return ssl.certificate.isPresent() || ssl.key.isPresent() || ssl.keyStore.isPresent() | ||
|| ssl.keyStoreType.isPresent() || ssl.keyStorePassword.isPresent() || ssl.trustStore.isPresent() | ||
|| ssl.trustStoreType.isPresent() || ssl.cipherSuites.isPresent() || ssl.clientAuth != ClientAuth.NONE | ||
|| !isDefaultProtocols(ssl.protocols); | ||
} | ||
|
||
private static boolean isDefaultProtocols(List<String> protocols) { | ||
return protocols.size() == 2 && protocols.contains("TLSv1.3") && protocols.contains("TLSv1.2"); | ||
} | ||
|
||
private static int testPort(String subProperty) { | ||
return port("quarkus." + subProperty + ".test-port"); | ||
} | ||
|
||
private static int port(String property) { | ||
return ConfigProvider.getConfig().getValue(property, Integer.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/PortSystemProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.vertx.http.runtime; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import io.quarkus.runtime.LaunchMode; | ||
|
||
public class PortSystemProperties { | ||
private final Map<String, String> portPropertiesToRestore = new HashMap<>(); | ||
|
||
public void set(String subProperty, int actualPort, LaunchMode launchMode) { | ||
String portPropertyValue = String.valueOf(actualPort); | ||
//we always set the .port property, even if we are in test mode, so this will always | ||
//reflect the current port | ||
String portPropertyName = "quarkus." + subProperty + ".port"; | ||
String prevPortPropertyValue = System.setProperty(portPropertyName, portPropertyValue); | ||
if (!Objects.equals(prevPortPropertyValue, portPropertyValue)) { | ||
portPropertiesToRestore.put(portPropertyName, prevPortPropertyValue); | ||
} | ||
if (launchMode == LaunchMode.TEST) { | ||
//we also set the test-port property in a test | ||
String testPropName = "quarkus." + subProperty + ".test-port"; | ||
String prevTestPropPrevValue = System.setProperty(testPropName, portPropertyValue); | ||
if (!Objects.equals(prevTestPropPrevValue, portPropertyValue)) { | ||
portPropertiesToRestore.put(testPropName, prevTestPropPrevValue); | ||
} | ||
} | ||
if (launchMode.isDevOrTest()) { | ||
// set the profile property as well to make sure we don't have any inconsistencies | ||
portPropertyName = "%" + launchMode.getDefaultProfile() + "." + portPropertyName; | ||
prevPortPropertyValue = System.setProperty(portPropertyName, portPropertyValue); | ||
if (!Objects.equals(prevPortPropertyValue, portPropertyValue)) { | ||
portPropertiesToRestore.put(portPropertyName, prevPortPropertyValue); | ||
} | ||
} | ||
} | ||
|
||
public void restore() { | ||
portPropertiesToRestore.forEach((key, value) -> { | ||
if (value == null) { | ||
System.clearProperty(key); | ||
} else { | ||
System.setProperty(key, value); | ||
} | ||
}); | ||
portPropertiesToRestore.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>quarkus-integration-tests-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-integration-test-grpc-test-random-port</artifactId> | ||
<name>Quarkus - Integration Tests - gRPC - Test Random Port</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-grpc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-grpc-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>generate-code</goal> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
23 changes: 23 additions & 0 deletions
23
...grpc-test-random-port/src/main/java/io/quarkus/grpc/examples/hello/HelloWorldService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.grpc.examples.hello; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import examples.HelloReply; | ||
import examples.HelloRequest; | ||
import examples.MutinyGreeterGrpc; | ||
import io.quarkus.grpc.GrpcService; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@GrpcService | ||
public class HelloWorldService extends MutinyGreeterGrpc.GreeterImplBase { | ||
|
||
AtomicInteger counter = new AtomicInteger(); | ||
|
||
@Override | ||
public Uni<HelloReply> sayHello(HelloRequest request) { | ||
int count = counter.incrementAndGet(); | ||
String name = request.getName(); | ||
return Uni.createFrom().item("Hello " + name) | ||
.map(res -> HelloReply.newBuilder().setMessage(res).setCount(count).build()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
integration-tests/grpc-test-random-port/src/main/proto/helloworld.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
syntax = "proto2"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "examples"; | ||
option java_outer_classname = "HelloWorldProto"; | ||
option objc_class_prefix = "HLW"; | ||
|
||
package helloworld; | ||
|
||
service Greeter { | ||
rpc SayHello (HelloRequest) returns (HelloReply) {} | ||
} | ||
|
||
message HelloRequest { | ||
required string name = 1; | ||
} | ||
|
||
message HelloReply { | ||
required string message = 1; | ||
optional int32 count = 2; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this was easy. :-)
Are we sure we captured all such log outputs -- with this "new" port?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an output in
buildServer
but that's before the server is started - so before the port is chosen.