Skip to content

Commit

Permalink
Improve available port discovery in tests
Browse files Browse the repository at this point in the history
Fixes #3199
  • Loading branch information
jamesnetherton committed Feb 16, 2022
1 parent fcdff37 commit 39de94a
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 11 deletions.
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);
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()))
.findFirst()
.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();
}
}

0 comments on commit 39de94a

Please sign in to comment.