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:
parent
8068854227
commit
263e3741b2
1 changed files with 87 additions and 29 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue