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 @@ -27,7 +27,6 @@
import fiftyone.pipeline.core.data.EvidenceKeyFilterWhitelist;
import fiftyone.pipeline.engines.Constants;
import fiftyone.pipeline.engines.fiftyone.data.ComponentMetaData;
import fiftyone.pipeline.engines.fiftyone.data.ProfileMetaData;
import fiftyone.pipeline.engines.fiftyone.data.ValueMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -146,12 +145,18 @@ private static void outputEvidenceKeyDetails(IPIntelligenceOnPremiseEngine engin

private static void outputProfileDetails(IPIntelligenceOnPremiseEngine engine,
PrintWriter output) {
// Group the profiles by component and then output the number of profiles
// for each component.
Map<String, List<ProfileMetaData>> groups =
// Group the profiles by component and then output the number of profiles
// for each component. Count with a reducing collector rather than
// collecting every ProfileMetaData into a List: an on-premise IP
// Intelligence data file has an enormous number of profiles, so
// materializing them all just to call size() exhausts the heap
// (OutOfMemoryError). counting() retains only the per-group total.
Map<String, Long> groups =
StreamSupport.stream(engine.getProfiles().spliterator(), false)
.collect(Collectors.groupingBy(p -> p.getComponent().getName()));
groups.forEach((k,v)->output.format("%s Profiles: %d\n", k , v.size()));
.collect(Collectors.groupingBy(
p -> p.getComponent().getName(),
Collectors.counting()));
groups.forEach((k,v)->output.format("%s Profiles: %d\n", k , v));
}

// Output the component name as well as a list of all the associated properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

public class GettingStartedApiTest {
private static Server SERVER;
private static final int PORT = GettingStartedApi.DEFAULT_PORT;
private static int PORT;

@BeforeClass
public static void startJetty() throws Exception {
Expand All @@ -56,7 +56,10 @@ public static void startJetty() throws Exception {

Map<String, String> initParams = new HashMap<>();
initParams.put(GettingStartedApi.DATA_FILE_INIT_PARAM, dataFilePath);
SERVER = EmbedJetty.startServlet("/*", PORT, GettingStartedApi.class, initParams);
// Bind an OS-assigned ephemeral port (0) to avoid intermittent
// "Address already in use" failures from a fixed port not yet released.
SERVER = EmbedJetty.startServlet("/*", 0, GettingStartedApi.class, initParams);
PORT = EmbedJetty.boundPort(SERVER);
}

private static String get(String path, int expectedCode) throws Exception {
Expand Down Expand Up @@ -108,8 +111,6 @@ public void testUnknownPathReturns404() throws Exception {

@AfterClass
public static void stopJetty() throws Exception {
if (SERVER != null) {
SERVER.stop();
}
EmbedJetty.stopAndJoin(SERVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

public class GettingStartedWebCloudMixedTest {
private static Server SERVER;
private static int PORT;

@BeforeClass
public static void startJetty() throws Exception {
Expand All @@ -51,15 +52,18 @@ public static void startJetty() throws Exception {
// Intelligence properties as this example uses both engines.
System.setProperty(KeyHelper.TEST_RESOURCE_KEY, resourceKey);

// Bind an OS-assigned ephemeral port (0) to avoid intermittent
// "Address already in use" failures from a fixed port not yet released.
SERVER = EmbedJetty.startWebApp(
GettingStartedWebCloudMixed.getResourceBase(), 8084);
GettingStartedWebCloudMixed.getResourceBase(), 0);
PORT = EmbedJetty.boundPort(SERVER);
}

@Test
public void testWebCloudMixed() throws Exception {

HttpURLConnection connection =
(HttpURLConnection) new URL("http://localhost:8084/").openConnection();
(HttpURLConnection) new URL("http://localhost:" + PORT + "/").openConnection();

int code = connection.getResponseCode();

Expand Down Expand Up @@ -91,8 +95,6 @@ public void testWebCloudMixed() throws Exception {

@AfterClass
public static void stopJetty() throws Exception {
if (SERVER != null) {
SERVER.stop();
}
EmbedJetty.stopAndJoin(SERVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

public class GettingStartedWebCloudTest {
private static Server SERVER;
private static int PORT;

@BeforeClass
public static void startJetty() throws Exception {
Expand All @@ -51,14 +52,17 @@ public static void startJetty() throws Exception {
// Make the resource key available to the pipeline configuration file
System.setProperty(KeyHelper.TEST_RESOURCE_KEY, resourceKey);

SERVER = EmbedJetty.startWebApp(getFilePath(getResourceBase()).getAbsolutePath(), 8083);
// Bind an OS-assigned ephemeral port (0) to avoid intermittent
// "Address already in use" failures from a fixed port not yet released.
SERVER = EmbedJetty.startWebApp(getFilePath(getResourceBase()).getAbsolutePath(), 0);
PORT = EmbedJetty.boundPort(SERVER);
}

@Test
public void testWebCloud() throws Exception {

HttpURLConnection connection =
(HttpURLConnection) new URL("http://localhost:8083/").openConnection();
(HttpURLConnection) new URL("http://localhost:" + PORT + "/").openConnection();

int code = connection.getResponseCode();

Expand Down Expand Up @@ -93,8 +97,6 @@ public void testWebCloud() throws Exception {

@AfterClass
public static void stopJetty() throws Exception {
if (SERVER != null) {
SERVER.stop();
}
EmbedJetty.stopAndJoin(SERVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

public class GettingStartedWebMixedTest {
private static Server SERVER;
private static int PORT;

/**
* Resolve the device detection data file. It is not part of this
Expand Down Expand Up @@ -77,15 +78,18 @@ public static void startJetty() throws Exception {
ipiDataFilePath != null);
System.setProperty("TestDataFile", ipiDataFilePath);

// Bind an OS-assigned ephemeral port (0) to avoid intermittent
// "Address already in use" failures from a fixed port not yet released.
SERVER = EmbedJetty.startWebApp(
GettingStartedWebMixed.getResourceBase(), 8081);
GettingStartedWebMixed.getResourceBase(), 0);
PORT = EmbedJetty.boundPort(SERVER);
}

@Test
public void testWebMixed() throws Exception {

HttpURLConnection connection =
(HttpURLConnection) new URL("http://localhost:8081/").openConnection();
(HttpURLConnection) new URL("http://localhost:" + PORT + "/").openConnection();

int code = connection.getResponseCode();

Expand Down Expand Up @@ -117,8 +121,6 @@ public void testWebMixed() throws Exception {

@AfterClass
public static void stopJetty() throws Exception {
if (SERVER != null) {
SERVER.stop();
}
EmbedJetty.stopAndJoin(SERVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

public class GettingStartedWebOnPremTest {
private static Server SERVER;
private static int PORT;

@BeforeClass
public static void startJetty() throws Exception {
Expand All @@ -51,14 +52,17 @@ public static void startJetty() throws Exception {
dataFilePath != null);
System.setProperty("TestDataFile", dataFilePath);

SERVER = EmbedJetty.startWebApp(getFilePath(getResourceBase()).getAbsolutePath(), 8081);
// Bind an OS-assigned ephemeral port (0) to avoid intermittent
// "Address already in use" failures from a fixed port not yet released.
SERVER = EmbedJetty.startWebApp(getFilePath(getResourceBase()).getAbsolutePath(), 0);
PORT = EmbedJetty.boundPort(SERVER);
}

@Test
public void testWebOnPrem() throws Exception {

HttpURLConnection connection =
(HttpURLConnection) new URL("http://localhost:8081/").openConnection();
(HttpURLConnection) new URL("http://localhost:" + PORT + "/").openConnection();

int code = connection.getResponseCode();

Expand Down Expand Up @@ -96,6 +100,6 @@ public void testWebOnPrem() throws Exception {

@AfterClass
public static void stopJetty() throws Exception {
SERVER.stop();
EmbedJetty.stopAndJoin(SERVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public static Server startWebApp(String resourceBase, int port) throws Exception
// Link the context to the server.
server.setHandler(context);

// Ensure the connector socket is released promptly and gracefully when
// the server is stopped, so a subsequent bind on the same port (e.g.
// when tests run back-to-back) does not hit "Address already in use".
server.setStopAtShutdown(true);
server.setStopTimeout(5000);

server.start();

// Wait for the server to be fully started and ready to accept connections
Expand All @@ -66,6 +72,32 @@ public static Server startWebApp(String resourceBase, int port) throws Exception
return server;
}

/**
* Returns the local TCP port the server's first connector is actually
* listening on. When the server is started with port {@code 0} the OS
* assigns a free ephemeral port; tests should read it back via this method
* rather than assuming a fixed port.
*
* @param server a started Jetty server
* @return the bound local port
*/
public static int boundPort(Server server) {
return ((ServerConnector) server.getConnectors()[0]).getLocalPort();
}

/**
* Stops the server and blocks until it has fully terminated, ensuring the
* listening socket is released before the method returns.
*
* @param server the server to stop (ignored if null)
*/
public static void stopAndJoin(Server server) throws Exception {
if (server != null) {
server.stop();
server.join();
}
}

/**
* Wait for the server to be fully started and ready to accept connections.
* This includes waiting for all connectors to be started and the context to be available.
Expand Down Expand Up @@ -141,6 +173,11 @@ public static Server startServlet(String contextPath, int port,
// Link the context to the server.
server.setHandler(context);

// Release the connector socket promptly and gracefully on stop (see
// startWebApp for rationale).
server.setStopAtShutdown(true);
server.setStopTimeout(5000);

server.start();

// Wait for the server to be fully started and ready to accept connections
Expand Down
Loading