Skip to content

Commit 83d5009

Browse files
committed
scan for ports
1 parent d9cf6ed commit 83d5009

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/DefaultEndPointsIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.elasticsearch.xpack.inference.services.elasticsearch.ElasticsearchInternalService;
1717
import org.hamcrest.Matchers;
1818
import org.junit.After;
19+
import org.junit.AfterClass;
1920
import org.junit.Before;
21+
import org.junit.BeforeClass;
2022

2123
import java.io.IOException;
2224
import java.util.ArrayList;
@@ -31,6 +33,16 @@
3133

3234
public class DefaultEndPointsIT extends InferenceBaseRestTest {
3335

36+
@BeforeClass
37+
public static void startModelServer() {
38+
mlModelServer.start();
39+
}
40+
41+
@AfterClass
42+
public static void stopModelServer() {
43+
mlModelServer.stop();
44+
}
45+
3446
private TestThreadPool threadPool;
3547

3648
@Before

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/InferenceBaseRestTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,20 @@
4444

4545
public class InferenceBaseRestTest extends ESRestTestCase {
4646

47-
static final int ML_MODEL_SERVER_PORT = 9999;
47+
@ClassRule(order = 0)
48+
public static MlModelServer mlModelServer = new MlModelServer();
4849

49-
@ClassRule
50+
@ClassRule(order = 1)
5051
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
5152
.distribution(DistributionType.DEFAULT)
5253
.setting("xpack.license.self_generated.type", "trial")
5354
.setting("xpack.security.enabled", "true")
54-
.setting("xpack.ml.model_repository", "http://localhost:" + ML_MODEL_SERVER_PORT)
55+
.setting("xpack.ml.model_repository", "http://localhost:" + mlModelServer.getPort())
5556
.plugin("inference-service-test")
5657
.user("x_pack_rest_user", "x-pack-test-password")
5758
.feature(FeatureFlag.INFERENCE_UNIFIED_API_ENABLED)
5859
.build();
5960

60-
private static MlModelServer mlModelServer;
61-
62-
@BeforeClass
63-
public static void startModelServer() throws Exception {
64-
mlModelServer = new MlModelServer(ML_MODEL_SERVER_PORT);
65-
}
66-
67-
@AfterClass
68-
public static void stopModelServer() {
69-
mlModelServer.close();
70-
}
71-
7261
@Override
7362
protected String getTestRestCluster() {
7463
return cluster.getHttpAddresses();

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/MlModelServer.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,53 @@
2727
* If the file is found, its content is returned, otherwise 404.
2828
* Respects a range header to serve partial content.
2929
*/
30-
class MlModelServer {
30+
public class MlModelServer {
3131

3232
private static final Logger logger = LogManager.getLogger(MlModelServer.class);
3333

34-
private final HttpServer mlModelServer;
35-
private final ExecutorService mlModelServerExecutor;
34+
private final int port;
35+
private final HttpServer server;
3636

37-
MlModelServer(int port) throws IOException {
37+
private ExecutorService executor;
38+
39+
public MlModelServer() {
40+
try {
41+
server = HttpServer.create();
42+
} catch (IOException e) {
43+
throw new RuntimeException("Could not create server", e);
44+
}
45+
server.createContext("/", this::handle);
46+
port = findUnusedPort();
47+
}
48+
49+
private int findUnusedPort() {
50+
Exception exception = null;
51+
for (int port = 10000; port < 11000; port++) {
52+
try {
53+
server.bind(new InetSocketAddress(port), 0);
54+
return port;
55+
} catch (IOException e) {
56+
exception = e;
57+
}
58+
}
59+
throw new RuntimeException("Could not find port", exception);
60+
}
61+
62+
public int getPort() {
63+
return port;
64+
}
65+
66+
public void start() {
3867
logger.info("Starting ML model server on port {}", port);
39-
mlModelServer = HttpServer.create(new InetSocketAddress(port), 10);
40-
mlModelServer.createContext("/", this::handle);
41-
mlModelServerExecutor = Executors.newCachedThreadPool();
42-
mlModelServer.setExecutor(mlModelServerExecutor);
43-
mlModelServer.start();
68+
executor = Executors.newCachedThreadPool();
69+
server.setExecutor(executor);
70+
server.start();
4471
}
4572

46-
void close() {
47-
logger.info("Stopping ML model server");
48-
mlModelServer.stop(5);
49-
mlModelServerExecutor.close();
73+
public void stop() {
74+
logger.info("Stopping ML model server in port {}", port);
75+
server.stop(1);
76+
executor.shutdown();
5077
}
5178

5279
private void handle(HttpExchange exchange) throws IOException {

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/TextEmbeddingCrudIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.inference.TaskType;
1313
import org.elasticsearch.plugins.Platforms;
14+
import org.junit.AfterClass;
15+
import org.junit.BeforeClass;
1416

1517
import java.io.IOException;
1618
import java.util.List;
@@ -22,6 +24,16 @@
2224
// See "https://github.com/elastic/elasticsearch/issues/105198".
2325
public class TextEmbeddingCrudIT extends InferenceBaseRestTest {
2426

27+
@BeforeClass
28+
public static void startModelServer() {
29+
mlModelServer.start();
30+
}
31+
32+
@AfterClass
33+
public static void stopModelServer() {
34+
mlModelServer.stop();
35+
}
36+
2537
public void testPutE5Small_withNoModelVariant() {
2638
{
2739
String inferenceEntityId = "testPutE5Small_withNoModelVariant";

0 commit comments

Comments
 (0)