Skip to content

Commit 60aa59a

Browse files
neeetmanbpkroth
andauthored
Fix Port Binding Issue During Testing (#382)
This PR addresses the issue where tests would fail if the designated port was already in use. ``` [INFO ] 2023-10-25 07:18:47,185 [main] com.oltpbenchmark.benchmarks.resourcestresser.TestResourceStresserBenchmark setUp - starting HSQLDB server for test [testGetTransactionType] on port [9099] [Server@191774d6]: [Thread[HSQLDB Server @191774d6,5,main]]: run()/openServerSocket(): java.net.BindException: Address already in use ``` Changes: - Modified the `setUp` method in `TestResourceStresserBenchmark` class. - Added logic to try different ports using `ServerSocket` to find an available port. - ~~The system will attempt up to `MAX_TRIES` to find an open port before throwing an exception.~~ - The the system will make a thorough effort in finding an available port. --------- Co-authored-by: Brian Kroth <[email protected]>
1 parent e6e8d0a commit 60aa59a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/test/java/com/oltpbenchmark/api/AbstractTestCase.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.ArrayList;
4040
import java.util.List;
4141
import java.util.concurrent.atomic.AtomicInteger;
42+
import java.net.ServerSocket;
43+
import java.net.BindException;
4244

4345
public abstract class AbstractTestCase<T extends BenchmarkModule> {
4446

@@ -69,6 +71,7 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> {
6971
protected final String ddlOverridePath;
7072

7173
private static final AtomicInteger portCounter = new AtomicInteger(9001);
74+
private static final int MAX_PORT_NUMBER = 65535;
7275

7376

7477
public AbstractTestCase(boolean createDatabase, boolean loadDatabase) {
@@ -99,7 +102,7 @@ public final void setUp() throws Exception {
99102
HsqlProperties props = new HsqlProperties();
100103
//props.setProperty("server.remote_open", true);
101104

102-
int port = portCounter.incrementAndGet();
105+
int port = findAvailablePort();
103106

104107
LOG.info("starting HSQLDB server for test [{}] on port [{}]", name.getMethodName(), port);
105108

@@ -163,6 +166,23 @@ public final void setUp() throws Exception {
163166
}
164167
}
165168

169+
private int findAvailablePort() throws IOException {
170+
while (true) {
171+
int port = portCounter.incrementAndGet();
172+
173+
if (port > MAX_PORT_NUMBER) {
174+
throw new IOException("No available port found up to " + MAX_PORT_NUMBER);
175+
}
176+
177+
try (ServerSocket testSocket = new ServerSocket(port)) {
178+
return port;
179+
} catch (BindException e) {
180+
// This port is already in use. Continue to next port.
181+
LOG.warn("Port {} is already in use. Trying next port.", port);
182+
}
183+
}
184+
}
185+
166186
protected TransactionTypes proceduresToTransactionTypes(List<Class<? extends Procedure>> procedures) {
167187
TransactionTypes txnTypes = new TransactionTypes(new ArrayList<>());
168188

0 commit comments

Comments
 (0)