|
39 | 39 | import java.util.ArrayList;
|
40 | 40 | import java.util.List;
|
41 | 41 | import java.util.concurrent.atomic.AtomicInteger;
|
| 42 | +import java.net.ServerSocket; |
| 43 | +import java.net.BindException; |
42 | 44 |
|
43 | 45 | public abstract class AbstractTestCase<T extends BenchmarkModule> {
|
44 | 46 |
|
@@ -69,6 +71,7 @@ public abstract class AbstractTestCase<T extends BenchmarkModule> {
|
69 | 71 | protected final String ddlOverridePath;
|
70 | 72 |
|
71 | 73 | private static final AtomicInteger portCounter = new AtomicInteger(9001);
|
| 74 | + private static final int MAX_PORT_NUMBER = 65535; |
72 | 75 |
|
73 | 76 |
|
74 | 77 | public AbstractTestCase(boolean createDatabase, boolean loadDatabase) {
|
@@ -99,7 +102,7 @@ public final void setUp() throws Exception {
|
99 | 102 | HsqlProperties props = new HsqlProperties();
|
100 | 103 | //props.setProperty("server.remote_open", true);
|
101 | 104 |
|
102 |
| - int port = portCounter.incrementAndGet(); |
| 105 | + int port = findAvailablePort(); |
103 | 106 |
|
104 | 107 | LOG.info("starting HSQLDB server for test [{}] on port [{}]", name.getMethodName(), port);
|
105 | 108 |
|
@@ -163,6 +166,23 @@ public final void setUp() throws Exception {
|
163 | 166 | }
|
164 | 167 | }
|
165 | 168 |
|
| 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 | + |
166 | 186 | protected TransactionTypes proceduresToTransactionTypes(List<Class<? extends Procedure>> procedures) {
|
167 | 187 | TransactionTypes txnTypes = new TransactionTypes(new ArrayList<>());
|
168 | 188 |
|
|
0 commit comments