Skip to content

Commit

Permalink
chore(action): Free system port function (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
boddissattva authored Apr 10, 2024
1 parent 78692e6 commit 63b4fc6
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

public class NetworkFunctions {

@SpelFunction
public static int freeSystemPort() {
return SocketUtils.freePortFromSystem();
}

@SpelFunction
public static int tcpPort() {
return findAvailableTcpPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ void hostIpReaching() throws Exception {
final String ip = NetworkFunctions.hostIpReaching("127.0.0.2");
assertThat(ip).isEqualTo("127.0.0.1");
}

@Test
void freeSystemPort() {
assertThat(NetworkFunctions.freeSystemPort()).isGreaterThan(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.chutneytesting.action.jakarta;

import com.chutneytesting.tools.SocketUtils;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServers;
Expand All @@ -26,6 +27,7 @@
public class ActiveMQTestSupport {

private static ActiveMQServer server;
private static int serverPort;

static String keyStorePath = ActiveMQTestSupport.class.getResource("/security/server.jks").getPath().toString();
static String keyStorePassword = "server";
Expand All @@ -35,14 +37,13 @@ public class ActiveMQTestSupport {

@BeforeAll
public static void setUp() throws Exception {


serverPort = SocketUtils.freePortFromSystem();
server = ActiveMQServers.newActiveMQServer(new ConfigurationImpl()
.setPersistenceEnabled(false)
.setJournalDirectory("target/data/journal")
.setSecurityEnabled(false)
.addAcceptorConfiguration("ssl",
"tcp://localhost:61617?" +
"tcp://localhost:" + serverPort + "?" +
"sslEnabled=true" +
"&keyStorePath=" + keyStorePath +
"&keyStorePassword=" + keyStorePassword +
Expand All @@ -58,5 +59,9 @@ public static void setUp() throws Exception {
public static void tearDown() throws Exception {
server.stop();
}

protected String serverUri() {
return "tcp://localhost:" + serverPort;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
public class JakartaSenderActionWithActiveMqIntegrationTest extends ActiveMQTestSupport {
@Test
public void failedSSL2WayAskWithOneWayProvided() {

String body = "messageBody";
String destination = "dynamicQueues/testD";
Map<String, String> headers = new HashMap<>();

TestTarget target = TestTarget.TestTargetBuilder.builder()
.withTargetId("id")
.withUrl("tcp://localhost:61617?" +
.withUrl(serverUri() + "?" +
"sslEnabled=true" +
"&keyStorePath=" + keyStorePath +
"&keyStorePassword=" + keyStorePassword +
Expand All @@ -60,5 +59,4 @@ public void failedSSL2WayAskWithOneWayProvided() {
assertThat(result.status).isEqualTo(Success);
assertThat(result.outputs.get("textMessage")).isEqualTo("messageBody");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

public class ReporterTest {

Expand Down
3 changes: 0 additions & 3 deletions chutney/engine/src/test/resources/junit-platform.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package com.chutneytesting.tools;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.Semaphore;
import javax.net.ServerSocketFactory;

/**
Expand Down Expand Up @@ -64,6 +66,28 @@ public class SocketUtils {
public SocketUtils() {
}

private static final Semaphore systemPortLock = new Semaphore(1);

/**
* Find free TCP port automatically allocated
*
* @return an available TCP port number.
* @throws RuntimeException if exception instantiating the socket
*/
public static int freePortFromSystem() {
try {
systemPortLock.acquire();
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
return socket.getLocalPort();
}
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
} finally {
systemPortLock.release();
}
}

/**
* Find an available TCP port randomly selected from the range
* [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
Expand Down
12 changes: 12 additions & 0 deletions docs/docs/documentation/functions/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ Following functions help you to :

# TcpPort

!!! note "int freeSystemPort()"

Find a free port given by the undelying system.

**Returns** :

* Returns an available TCP Port number

**Examples** :

SpEL : `${#freeSystemPort()}`

!!! note "int tcpPort()"

Find an available TCP port randomly selected from the range [1024, 65535].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ fun micrometerRegistry(registryClassName: String, elEval: Boolean = true): Strin
}


fun freeSystemPort(elEval: Boolean = true): String {
return chutneyFunction("freeSystemPort", elEval)
}

fun tcpPort(elEval: Boolean = true): String {
return chutneyFunction("tcpPort", elEval)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class ChutneyFunctionsTest {

assertELWrap(micrometerRegistry("className"))

assertELWrap(freeSystemPort())
assertELWrap(tcpPort())
assertELWrap(tcpPorts(2))
assertELWrap(tcpPortMin(2))
Expand Down

0 comments on commit 63b4fc6

Please sign in to comment.