feat(test): Use dynamic port allocation in integration tests (#133)

- Add TestUtil class with findAvailablePort() method to the mcp-test module
- Add findAvailablePort() method to TomcatTestUtil classes
- Replace hardcoded port numbers with dynamic port allocation

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
a.darafeyeu 2025-04-09 19:43:29 +02:00 • committed by Christian Tzolov
commit 263e3741b2

View file

@ -0,0 +1,31 @@
/*
* Copyright 2025 - 2025 the original author or authors.
*/
package io.modelcontextprotocol.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
public class TestUtil {
TestUtil() {
// Prevent instantiation
}
/**
* Finds an available port on the local machine.
* @return an available port number
* @throws IllegalStateException if no available port can be found
*/
public static int findAvailablePort() {
try (final ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress(0));
return socket.getLocalPort();
}
catch (final IOException e) {
throw new IllegalStateException("Cannot bind to an available port!", e);
}
}
}