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

Improve available port discovery in tests #3555

Merged
merged 1 commit into from
Feb 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import org.eclipse.microprofile.config.Config;
Expand All @@ -35,9 +36,11 @@
*/
public final class AvailablePortFinder {
private static final Logger LOGGER = LoggerFactory.getLogger(AvailablePortFinder.class);
private static final Map<Integer, String> RESERVED_PORTS = new ConcurrentHashMap<>();
private static final String[] QUARKUS_PORT_PROPERTIES = new String[] {
"quarkus.http.test-port",
"quarkus.http.test-ssl-port",
"quarkus.https.test-port",
};

/**
Expand All @@ -54,15 +57,23 @@ private AvailablePortFinder() {
* @return the available port
*/
public static int getNextAvailable() {
// Using AvailablePortFinder in native applications can be problematic
// E.g The reserved port may be allocated at build time and preserved indefinitely at runtime. I.e it never changes on each execution of the native application
logWarningIfNativeApplication();

while (true) {
try (ServerSocket ss = new ServerSocket()) {
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress((InetAddress) null, 0), 1);

int port = ss.getLocalPort();
if (!isQuarkusReservedPort(port)) {
LOGGER.info("getNextAvailable() -> {}", port);
return port;
String callerClassName = getCallerClassName();
String value = RESERVED_PORTS.putIfAbsent(port, callerClassName);
Copy link
Contributor

@ppalaga ppalaga Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this making some assumptions about whether this class can concurrently exist in multiple JVMs or multiple class loaders within a single maven invocation? IIRC surefire is forking a new JVM for each Maven module. So if building with mvnd, the AvailablePortFinder class (and thus also separate RESERVED_PORTS instances) may exist in several concurrent JVMs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a bit more? It's not entirely clear to me why it matters in this context.

All it's doing is trying to track which class reserved a specific port. Arguably we don't need to do that. It just provides some nice logging output and a way to clear out stale entries from the RESERVED_PORTS map.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - after re-reading. It's about separate RESERVED_PORTS instances.

In which case, yes, I suppose the map contents would be different between concurrent JVMs or class loaders.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining. If it is just for logging, indeed, multiple RESERVED_PORTS instances in concurrent per-module JVMs do not matter.

if (value == null) {
LOGGER.info("{} reserved port {}", callerClassName, port);
return port;
}
}
} catch (IOException e) {
throw new IllegalStateException("Cannot find free port", e);
Expand Down Expand Up @@ -90,6 +101,16 @@ public static <T> Map<String, T> reserveNetworkPorts(Function<Integer, T> conver
return reservedPorts;
}

public static void releaseReservedPorts() {
String callerClassName = getCallerClassName();
RESERVED_PORTS.entrySet()
.stream()
.filter(entry -> entry.getValue().equals(callerClassName))
.peek(entry -> LOGGER.info("Releasing port {} reserved by {}", entry.getKey(), entry.getValue()))
.map(Map.Entry::getKey)
.forEach(RESERVED_PORTS::remove);
}

private static boolean isQuarkusReservedPort(int port) {
Config config = ConfigProvider.getConfig();
for (String property : QUARKUS_PORT_PROPERTIES) {
Expand All @@ -103,4 +124,19 @@ private static boolean isQuarkusReservedPort(int port) {
}
return false;
}

private static String getCallerClassName() {
return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(s -> s.map(StackWalker.StackFrame::getClassName)
.filter(className -> !className.equals(AvailablePortFinder.class.getName()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we make AvailablePortFinder class final, to better match the assumption made on the above line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we make AvailablePortFinder class final

The class declaration is:

public final class AvailablePortFinder {
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering on className should be enough then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class declaration is:

public final class AvailablePortFinder {
}

I am blind. Sorry for the noise.

.findFirst()
aldettinger marked this conversation as resolved.
Show resolved Hide resolved
.orElseThrow(IllegalStateException::new));
}

private static void logWarningIfNativeApplication() {
if (System.getProperty("org.graalvm.nativeimage.kind") != null) {
LOGGER.warn("Usage of AvailablePortFinder in the native application is discouraged. "
+ "Pass the reserved port to the native application under test with QuarkusTestResource or via an HTTP request");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public Map<String, String> start() {

@Override
public void stop() {
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void stop() {
if (specificNettyServer != null) {
specificNettyServer.close();
}
AvailablePortFinder.releaseReservedPorts();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public void stop() {
} catch (Exception e) {
LOGGER.warn("Failed delete usr file: {}, {}", usrFile, e);
}

AvailablePortFinder.releaseReservedPorts();
}

protected ListenerFactory createListenerFactory(int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ public void stop() {
} catch (Exception e) {
LOGGER.warn("Failed delete sftp home: {}, {}", sftpHome, e);
}

AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public void stop() {
} catch (Exception e) {
LOGGER.error("Could not stop gRPC server", e);
}
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public Map<String, String> start() {

@Override
public void stop() {
// Noop
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ public void stop() {
if (container != null) {
container.stop();
}
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public Map<String, String> start() {

@Override
public void stop() {
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ public void stop() {
} catch (Exception e) {
// ignored
}
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public Map<String, String> start() {

@Override
public void stop() {

AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.camel.component.mllp.MllpComponent;
import org.apache.camel.component.mllp.MllpConstants;
import org.apache.camel.component.mock.MockEndpoint;
import org.eclipse.microprofile.config.ConfigProvider;

@Path("/mllp")
@ApplicationScoped
Expand Down Expand Up @@ -64,7 +65,8 @@ public void sendInvalidMessageToMllp(String message) throws Exception {
@POST
@Produces(MediaType.TEXT_PLAIN)
public String getWithCharsetFromMsh18(String message) {
String mllpHostPort = String.format("mllp:%s:%d", MllpRoutes.MLLP_HOST, MllpRoutes.MLLP_PORT);
Integer mllpPort = ConfigProvider.getConfig().getValue("mllp.test.port", Integer.class);
String mllpHostPort = String.format("mllp:%s:%d", MllpRoutes.MLLP_HOST, mllpPort);
Exchange exchange = producerTemplate.request(mllpHostPort, e -> e.getMessage().setBody(message));
String ack = exchange.getMessage().getHeader(MllpConstants.MLLP_ACKNOWLEDGEMENT_STRING, String.class);
return ack.split("\r")[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,29 @@
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mllp.MllpConstants;
import org.apache.camel.component.mllp.MllpInvalidMessageException;
import org.apache.camel.quarkus.test.AvailablePortFinder;
import org.eclipse.microprofile.config.ConfigProvider;

@RegisterForReflection(targets = MllpInvalidMessageException.class, fields = false)
public class MllpRoutes extends RouteBuilder {

public static final String MLLP_HOST = "localhost";
public static final int MLLP_PORT = AvailablePortFinder.getNextAvailable();

@Override
public void configure() throws Exception {
Integer mllpPort = ConfigProvider.getConfig().getValue("mllp.test.port", Integer.class);

onException(MllpInvalidMessageException.class)
.to("mock:invalid");

fromF("mllp://%s:%d?validatePayload=true", MLLP_HOST, MLLP_PORT)
fromF("mllp://%s:%d?validatePayload=true", MLLP_HOST, mllpPort)
.convertBodyTo(String.class);

from("direct:validMessage")
.toF("mllp://%s:%d", MLLP_HOST, MLLP_PORT)
.toF("mllp://%s:%d", MLLP_HOST, mllpPort)
.setBody(header(MllpConstants.MLLP_ACKNOWLEDGEMENT));

from("direct:invalidMessage")
.toF("mllp://%s:%d?exchangePattern=InOnly", MLLP_HOST, MLLP_PORT)
.toF("mllp://%s:%d?exchangePattern=InOnly", MLLP_HOST, mllpPort)
.setBody(header(MllpConstants.MLLP_ACKNOWLEDGEMENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package org.apache.camel.quarkus.component.mllp.it;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.containsString;

@QuarkusTest
@QuarkusTestResource(MllpTestResource.class)
class MllpTest {

private static final String HL7_MESSAGE = "MSH|^~\\&|REQUESTING|ICE|INHOUSE|RTH00|20210331095020||ORM^O01|1|D|2.3|||AL|NE||\r"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.mllp.it;

import java.util.Collections;
import java.util.Map;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.test.AvailablePortFinder;

public class MllpTestResource implements QuarkusTestResourceLifecycleManager {

@Override
public Map<String, String> start() {
return Collections.singletonMap("mllp.test.port", Integer.toString(AvailablePortFinder.getNextAvailable()));
}

@Override
public void stop() {
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void stop() {
if (context != null) {
context.shutdown();
}
AvailablePortFinder.releaseReservedPorts();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public Map<String, String> start() {

@Override
public void stop() {
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void start() {

public void stop() {
server.stop();
AvailablePortFinder.releaseReservedPorts();
}

public int getHttpPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public Map<String, String> start() {

@Override
public void stop() {
AvailablePortFinder.releaseReservedPorts();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public Map<String, String> start() {

@Override
public void stop() {

AvailablePortFinder.releaseReservedPorts();
}
}