Skip to content
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

Support random test port #4140

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.quarkus.vertx.http;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.vertx.http.cors.BeanRegisteringRoute;

public class RandomPortTest {

@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(BeanRegisteringRoute.class)
.addAsResource(new StringAsset("quarkus.http.test-port=0"),
"application.properties"));

@TestHTTPResource("test")
URL url;

@Test
public void portShouldNotBeZero() {
assertThat(url.getPort()).isNotZero();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public Verticle get() {
return new WebDeploymentVerticle(httpConfiguration.determinePort(launchMode),
httpConfiguration.determineSslPort(launchMode), httpConfiguration.host, httpServerOptions,
sslConfig,
router);
router,
launchMode);
}
}, new DeploymentOptions().setInstances(ioThreads), new Handler<AsyncResult<String>>() {
@Override
Expand Down Expand Up @@ -368,15 +369,17 @@ private static class WebDeploymentVerticle extends AbstractVerticle {
private final HttpServerOptions httpOptions;
private final HttpServerOptions httpsOptions;
private final Router router;
private final LaunchMode launchMode;

public WebDeploymentVerticle(int port, int httpsPort, String host, HttpServerOptions httpOptions,
HttpServerOptions httpsOptions, Router router) {
HttpServerOptions httpsOptions, Router router, LaunchMode launchMode) {
this.port = port;
this.httpsPort = httpsPort;
this.host = host;
this.httpOptions = httpOptions;
this.httpsOptions = httpsOptions;
this.router = router;
this.launchMode = launchMode;
}

@Override
Expand All @@ -389,7 +392,14 @@ public void start(Future<Void> startFuture) {
startFuture.fail(event.cause());
} else {
// Port may be random, so set the actual port
httpOptions.setPort(event.result().actualPort());
int actualPort = event.result().actualPort();
if (actualPort != port) {
// Override quarkus.http.(test-)?port
System.setProperty(launchMode == LaunchMode.TEST ? "quarkus.http.test-port" : "quarkus.http.port",
String.valueOf(actualPort));
// Set in HttpOptions to output the port in the Timing class
httpOptions.setPort(actualPort);
}
if (remainingCount.decrementAndGet() == 0) {
startFuture.complete(null);
}
Expand All @@ -402,7 +412,14 @@ public void start(Future<Void> startFuture) {
if (event.cause() != null) {
startFuture.fail(event.cause());
} else {
httpsOptions.setPort(event.result().actualPort());
int actualPort = event.result().actualPort();
if (actualPort != httpsPort) {
// Override quarkus.https.(test-)?port
System.setProperty(launchMode == LaunchMode.TEST ? "quarkus.https.test-port" : "quarkus.https.port",
String.valueOf(actualPort));
// Set in HttpOptions to output the port in the Timing class
httpsOptions.setPort(actualPort);
}
if (remainingCount.decrementAndGet() == 0) {
startFuture.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static io.quarkus.test.common.PathTestHelper.getTestClassesLocation;

import java.io.Closeable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -112,19 +111,15 @@ private ExtensionState doJavaStart(ExtensionContext context, TestResourceManager
public void writeClass(boolean applicationClass, String className, byte[] data) throws IOException {
Path location = testWiringClassesDir.resolve(className.replace('.', '/') + ".class");
Files.createDirectories(location.getParent());
try (FileOutputStream out = new FileOutputStream(location.toFile())) {
out.write(data);
}
Files.write(location, data);
shutdownTasks.add(new DeleteRunnable(location));
}

@Override
public void writeResource(String name, byte[] data) throws IOException {
Path location = testWiringClassesDir.resolve(name);
Files.createDirectories(location.getParent());
try (FileOutputStream out = new FileOutputStream(location.toFile())) {
out.write(data);
}
Files.write(location, data);
shutdownTasks.add(new DeleteRunnable(location));
}
})
Expand Down Expand Up @@ -192,9 +187,7 @@ protected ClassLoader getClassLoader() {

Path location = testWiringClassesDir.resolve(resourceName);
Files.createDirectories(location.getParent());
try (FileOutputStream out = new FileOutputStream(location.toFile())) {
out.write(cw.toByteArray());
}
Files.write(location, cw.toByteArray());
shutdownTasks.add(new DeleteRunnable(location));
} catch (IOException ex) {
ex.printStackTrace();
Expand Down