Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public void start() throws Exception {
.addService(ProtoReflectionServiceV1.newInstance())
.build()
.start();
// port 0 asks the OS to pick one; adopt what it actually bound
port = server.getPort();
LOGGER.info("Server started, listening on " + port);
Runtime
.getRuntime()
Expand Down Expand Up @@ -208,6 +210,11 @@ public TikaGrpcServer setPort(Integer port) {
return this;
}

/** The bound port once {@link #start()} has run; the requested port before that. */
public Integer getPort() {
return port;
}

public TikaGrpcServer setSecure(boolean secure) {
this.secure = secure;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package org.apache.tika.pipes.grpc;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.time.Duration;
Expand All @@ -43,6 +41,7 @@
import org.apache.commons.io.FileUtils;
import org.awaitility.Awaitility;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -78,23 +77,18 @@ class PipesBiDirectionalStreamingIntegrationTest {
String httpFetcherId = "httpFetcherIdHere";
List<String> files = Arrays.asList("014760.docx", "017091.docx", "017097.docx", "018367.docx");

static int findAvailablePort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}

@BeforeAll
static void setUpHttpServer() throws Exception {
// Specify the folder from which files will be served
httpServerPort = findAvailablePort();
httpServer = new Server(httpServerPort);
// bind port 0 and read back what the OS gave us: a port picked and released
// before binding is a port another process can take in between
httpServer = new Server(0);

ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirAllowed(true);
resourceHandler.setBaseResourceAsString("src/test/resources/test-files");
httpServer.setHandler(resourceHandler);
httpServer.start();
httpServerPort = ((ServerConnector) httpServer.getConnectors()[0]).getLocalPort();

httpServerUrl = "http://" + InetAddress
.getByName("localhost")
Expand All @@ -103,8 +97,6 @@ static void setUpHttpServer() throws Exception {

@BeforeAll
static void setUpGrpcServer() throws Exception {
grpcPort = findAvailablePort();

// Read the template config
String configContent = FileUtils.readFileToString(tikaConfigTemplate, StandardCharsets.UTF_8);

Expand All @@ -131,13 +123,14 @@ static void setUpGrpcServer() throws Exception {

grpcServer = new TikaGrpcServer();
grpcServer.setTikaConfig(tikaConfig);
grpcServer.setPort(grpcPort);
grpcServer.setPort(0);
grpcServer.setSecure(true);
grpcServer.setCertChain(Paths.get("src", "test", "resources", "certs", "server1.pem").toFile());
grpcServer.setPrivateKey(Paths.get("src", "test", "resources", "certs", "server1.key").toFile());
grpcServer.setTrustCertCollection(Paths.get("src", "test", "resources", "certs", "ca.pem").toFile());
grpcServer.setClientAuthRequired(true);
grpcServer.start();
grpcPort = grpcServer.getPort();

String target = InetAddress
.getByName("localhost")
Expand Down
Loading