diff --git a/integrations/oci/metrics/cdi/src/test/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsCdiExtensionTest.java b/integrations/oci/metrics/cdi/src/test/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsCdiExtensionTest.java index b3048f2fa5c..5529403a8ae 100644 --- a/integrations/oci/metrics/cdi/src/test/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsCdiExtensionTest.java +++ b/integrations/oci/metrics/cdi/src/test/java/io/helidon/integrations/oci/metrics/cdi/OciMetricsCdiExtensionTest.java @@ -15,38 +15,63 @@ */ package io.helidon.integrations.oci.metrics.cdi; -import com.oracle.bmc.Region; -import com.oracle.bmc.monitoring.Monitoring; -import com.oracle.bmc.monitoring.MonitoringPaginators; -import com.oracle.bmc.monitoring.MonitoringWaiters; -import com.oracle.bmc.monitoring.model.MetricDataDetails; -import com.oracle.bmc.monitoring.model.PostMetricDataDetails; -import com.oracle.bmc.monitoring.requests.*; -import com.oracle.bmc.monitoring.responses.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import io.helidon.config.Config; import io.helidon.integrations.oci.metrics.OciMetricsSupport; import io.helidon.metrics.api.RegistryFactory; import io.helidon.microprofile.config.ConfigCdiExtension; -import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.tests.junit5.AddBean; import io.helidon.microprofile.tests.junit5.AddConfig; import io.helidon.microprofile.tests.junit5.AddExtension; import io.helidon.microprofile.tests.junit5.DisableDiscovery; import io.helidon.microprofile.tests.junit5.HelidonTest; +import com.oracle.bmc.Region; +import com.oracle.bmc.monitoring.Monitoring; +import com.oracle.bmc.monitoring.MonitoringPaginators; +import com.oracle.bmc.monitoring.MonitoringWaiters; +import com.oracle.bmc.monitoring.model.MetricDataDetails; +import com.oracle.bmc.monitoring.model.PostMetricDataDetails; +import com.oracle.bmc.monitoring.requests.ChangeAlarmCompartmentRequest; +import com.oracle.bmc.monitoring.requests.CreateAlarmRequest; +import com.oracle.bmc.monitoring.requests.DeleteAlarmRequest; +import com.oracle.bmc.monitoring.requests.GetAlarmHistoryRequest; +import com.oracle.bmc.monitoring.requests.GetAlarmRequest; +import com.oracle.bmc.monitoring.requests.ListAlarmsRequest; +import com.oracle.bmc.monitoring.requests.ListAlarmsStatusRequest; +import com.oracle.bmc.monitoring.requests.ListMetricsRequest; +import com.oracle.bmc.monitoring.requests.PostMetricDataRequest; +import com.oracle.bmc.monitoring.requests.RemoveAlarmSuppressionRequest; +import com.oracle.bmc.monitoring.requests.RetrieveDimensionStatesRequest; +import com.oracle.bmc.monitoring.requests.SummarizeMetricsDataRequest; +import com.oracle.bmc.monitoring.requests.UpdateAlarmRequest; +import com.oracle.bmc.monitoring.responses.ChangeAlarmCompartmentResponse; +import com.oracle.bmc.monitoring.responses.CreateAlarmResponse; +import com.oracle.bmc.monitoring.responses.DeleteAlarmResponse; +import com.oracle.bmc.monitoring.responses.GetAlarmHistoryResponse; +import com.oracle.bmc.monitoring.responses.GetAlarmResponse; +import com.oracle.bmc.monitoring.responses.ListAlarmsResponse; +import com.oracle.bmc.monitoring.responses.ListAlarmsStatusResponse; +import com.oracle.bmc.monitoring.responses.ListMetricsResponse; +import com.oracle.bmc.monitoring.responses.PostMetricDataResponse; +import com.oracle.bmc.monitoring.responses.RemoveAlarmSuppressionResponse; +import com.oracle.bmc.monitoring.responses.RetrieveDimensionStatesResponse; +import com.oracle.bmc.monitoring.responses.SummarizeMetricsDataResponse; +import com.oracle.bmc.monitoring.responses.UpdateAlarmResponse; + import org.eclipse.microprofile.metrics.MetricRegistry; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.fail; @HelidonTest(resetPerTest = true) @AddBean(OciMetricsCdiExtensionTest.MockMonitoring.class) @@ -64,41 +89,40 @@ @AddConfig(key = "ocimetrics.namespace", value = OciMetricsCdiExtensionTest.MetricDataDetailsOCIParams.namespace) @AddConfig(key = "ocimetrics.resourceGroup", - value = OciMetricsCdiExtensionTest.MetricDataDetailsOCIParams.resourceGroup) + value = OciMetricsCdiExtensionTest.MetricDataDetailsOCIParams.resourceGroup) @AddConfig(key = "ocimetrics.initialDelay", value = "1") @AddConfig(key = "ocimetrics.delay", value = "2") -public class OciMetricsCdiExtensionTest { - private final RegistryFactory rf = RegistryFactory.getInstance(); - private final MetricRegistry baseMetricRegistry = rf.getRegistry(MetricRegistry.Type.BASE); - private final MetricRegistry vendorMetricRegistry = rf.getRegistry(MetricRegistry.Type.VENDOR); - private final MetricRegistry appMetricRegistry = rf.getRegistry(MetricRegistry.Type.APPLICATION); +class OciMetricsCdiExtensionTest { private static volatile int testMetricCount = 0; - // Use countDownLatch1 to signal the test that results to be asserted has been retrieved - private static CountDownLatch countDownLatch1 = new CountDownLatch(1); + private static CountDownLatch countDownLatch = new CountDownLatch(1); private static PostMetricDataDetails postMetricDataDetails; private static boolean activateOciMetricsSupportIsInvoked; + private final RegistryFactory rf = RegistryFactory.getInstance(); + private final MetricRegistry appMetricRegistry = rf.getRegistry(MetricRegistry.Type.APPLICATION); + private final MetricRegistry baseMetricRegistry = rf.getRegistry(MetricRegistry.Type.BASE); + private final MetricRegistry vendorMetricRegistry = rf.getRegistry(MetricRegistry.Type.VENDOR); @AfterEach - private void resetState() { + void resetState() { postMetricDataDetails = null; activateOciMetricsSupportIsInvoked = false; - countDownLatch1 = new CountDownLatch(1); + countDownLatch = new CountDownLatch(1); } @Test @AddConfig(key = "ocimetrics.enabled", value = "true") - public void testEnableOciMetrics() throws InterruptedException { + void testEnableOciMetrics() throws InterruptedException { validateOciMetricsSupport(true); } @Test - public void testEnableOciMetricsWithoutConfig() throws InterruptedException { + void testEnableOciMetricsWithoutConfig() throws InterruptedException { validateOciMetricsSupport(true); } @Test @AddConfig(key = "ocimetrics.enabled", value = "false") - public void testDisableOciMetrics() throws InterruptedException { + void testDisableOciMetrics() throws InterruptedException { validateOciMetricsSupport(false); } @@ -107,7 +131,12 @@ private void validateOciMetricsSupport(boolean enabled) throws InterruptedExcept vendorMetricRegistry.counter("vendorDummyCounter").inc(); appMetricRegistry.counter("appDummyCounter").inc(); // Wait for signal from metric update that testMetricCount has been retrieved - countDownLatch1.await(3, TimeUnit.SECONDS); + if (!countDownLatch.await(3, TimeUnit.SECONDS)) { + // If Oci Metrics is enabled, this means that countdown() of CountDownLatch was never triggered, and hence should fail + if (enabled) { + fail("CountDownLatch timed out"); + } + } if (enabled) { assertThat(activateOciMetricsSupportIsInvoked, is(true)); @@ -126,21 +155,33 @@ private void validateOciMetricsSupport(boolean enabled) throws InterruptedExcept } } + interface MetricDataDetailsOCIParams { + String compartmentId = "dummy.compartmentId"; + String namespace = "dummy-namespace"; + String resourceGroup = "dummy_resourceGroup"; + } + static class MockMonitoring implements Monitoring { @Override - public void setEndpoint(String s) {} + public String getEndpoint() { + return "http://www.DummyEndpoint.com"; + } @Override - public String getEndpoint() {return "http://www.DummyEndpoint.com";} + public void setEndpoint(String s) { + } @Override - public void setRegion(Region region) {} + public void setRegion(Region region) { + } @Override - public void setRegion(String s) {} + public void setRegion(String s) { + } @Override - public void refreshClient() {} + public void refreshClient() { + } @Override public ChangeAlarmCompartmentResponse changeAlarmCompartment(ChangeAlarmCompartmentRequest changeAlarmCompartmentRequest) { @@ -148,19 +189,29 @@ public ChangeAlarmCompartmentResponse changeAlarmCompartment(ChangeAlarmCompartm } @Override - public CreateAlarmResponse createAlarm(CreateAlarmRequest createAlarmRequest) {return null;} + public CreateAlarmResponse createAlarm(CreateAlarmRequest createAlarmRequest) { + return null; + } @Override - public DeleteAlarmResponse deleteAlarm(DeleteAlarmRequest deleteAlarmRequest) {return null;} + public DeleteAlarmResponse deleteAlarm(DeleteAlarmRequest deleteAlarmRequest) { + return null; + } @Override - public GetAlarmResponse getAlarm(GetAlarmRequest getAlarmRequest) {return null;} + public GetAlarmResponse getAlarm(GetAlarmRequest getAlarmRequest) { + return null; + } @Override - public GetAlarmHistoryResponse getAlarmHistory(GetAlarmHistoryRequest getAlarmHistoryRequest) {return null;} + public GetAlarmHistoryResponse getAlarmHistory(GetAlarmHistoryRequest getAlarmHistoryRequest) { + return null; + } @Override - public ListAlarmsResponse listAlarms(ListAlarmsRequest listAlarmsRequest) {return null;} + public ListAlarmsResponse listAlarms(ListAlarmsRequest listAlarmsRequest) { + return null; + } @Override public ListAlarmsStatusResponse listAlarmsStatus(ListAlarmsStatusRequest listAlarmsStatusRequest) { @@ -168,14 +219,16 @@ public ListAlarmsStatusResponse listAlarmsStatus(ListAlarmsStatusRequest listAla } @Override - public ListMetricsResponse listMetrics(ListMetricsRequest listMetricsRequest) {return null;} + public ListMetricsResponse listMetrics(ListMetricsRequest listMetricsRequest) { + return null; + } @Override public PostMetricDataResponse postMetricData(PostMetricDataRequest postMetricDataRequest) { postMetricDataDetails = postMetricDataRequest.getPostMetricDataDetails(); testMetricCount = postMetricDataDetails.getMetricData().size(); // Give signal that testMetricCount was retrieved - countDownLatch1.countDown(); + countDownLatch.countDown(); return PostMetricDataResponse.builder() .__httpStatusCode__(200) .build(); @@ -197,16 +250,23 @@ public SummarizeMetricsDataResponse summarizeMetricsData(SummarizeMetricsDataReq } @Override - public UpdateAlarmResponse updateAlarm(UpdateAlarmRequest updateAlarmRequest) {return null;} + public UpdateAlarmResponse updateAlarm(UpdateAlarmRequest updateAlarmRequest) { + return null; + } @Override - public MonitoringWaiters getWaiters() {return null;} + public MonitoringWaiters getWaiters() { + return null; + } @Override - public MonitoringPaginators getPaginators() {return null;} + public MonitoringPaginators getPaginators() { + return null; + } @Override - public void close() throws Exception {} + public void close() throws Exception { + } } static class MockOciMetricsBean extends OciMetricsBean { @@ -217,17 +277,4 @@ void activateOciMetricsSupport(Config config, OciMetricsSupport.Builder builder) super.activateOciMetricsSupport(config, builder); } } - - public interface MetricDataDetailsOCIParams { - String compartmentId = "dummy.compartmentId"; - String namespace = "dummy-namespace"; - String resourceGroup = "dummy_resourceGroup"; - } - - private static void delay(long millis) { - try { - Thread.sleep(millis); - } catch (InterruptedException ignore) { - } - } } diff --git a/integrations/oci/metrics/metrics/src/test/java/io/helidon/integrations/oci/metrics/OciMetricsSupportTest.java b/integrations/oci/metrics/metrics/src/test/java/io/helidon/integrations/oci/metrics/OciMetricsSupportTest.java index 8e57118d67b..f7462bab6c2 100644 --- a/integrations/oci/metrics/metrics/src/test/java/io/helidon/integrations/oci/metrics/OciMetricsSupportTest.java +++ b/integrations/oci/metrics/metrics/src/test/java/io/helidon/integrations/oci/metrics/OciMetricsSupportTest.java @@ -51,26 +51,23 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; -public class OciMetricsSupportTest { - private final OciMetricsSupport.NameFormatter nameFormatter = new OciMetricsSupport.NameFormatter() { }; +class OciMetricsSupportTest { private static final Monitoring monitoringClient = mock(Monitoring.class); - private final Type[] types = {Type.BASE, Type.VENDOR, Type.APPLICATION}; - private static volatile Double[] testMetricUpdateCounterValue = new Double[2]; private static volatile int testMetricCount = 0; - // Use CountDownLatch to signal when to start testing, for example, test only after results has been retrieved. - private static CountDownLatch countDownLatch1; private static int noOfExecutions; private static int noOfMetrics; - + private static String endPoint = "https://telemetry.DummyEndpoint.com"; + private static String postingEndPoint; + private final Type[] types = {Type.BASE, Type.VENDOR, Type.APPLICATION}; private final RegistryFactory rf = RegistryFactory.getInstance(); private final MetricRegistry baseMetricRegistry = rf.getRegistry(Type.BASE); private final MetricRegistry vendorMetricRegistry = rf.getRegistry(Type.VENDOR); private final MetricRegistry appMetricRegistry = rf.getRegistry(Type.APPLICATION); - private static String endPoint = "https://telemetry.DummyEndpoint.com"; - private static String postingEndPoint; @BeforeAll static void mockSetGetEndpoints() { @@ -84,9 +81,9 @@ static void mockSetGetEndpoints() { } @BeforeEach - private void beforeEach() { + void resetState() { // clear all registry - for (Type type: types) { + for (Type type : types) { MetricRegistry metricRegistry = rf.getRegistry(type); metricRegistry.removeMatching(new MetricFilter() { @Override @@ -95,13 +92,14 @@ public boolean matches(MetricID metricID, Metric metric) { } }); } + endPoint = "https://telemetry.DummyEndpoint.com"; } @Test - public void testMetricUpdate() throws InterruptedException { + void testMetricUpdate() throws InterruptedException { Counter counter = baseMetricRegistry.counter("DummyCounter"); - countDownLatch1 = new CountDownLatch(1); + CountDownLatch countDownLatch = new CountDownLatch(1); noOfExecutions = 0; doAnswer(invocationOnMock -> { @@ -118,7 +116,7 @@ public void testMetricUpdate() throws InterruptedException { counter.inc(); } else { // Give signal that multiple metric updates have been triggered - countDownLatch1.countDown(); + countDownLatch.countDown(); } return PostMetricDataResponse.builder() .__httpStatusCode__(200) @@ -141,10 +139,10 @@ public void testMetricUpdate() throws InterruptedException { WebServer webServer = createWebServer(routing); // Wait for metric updates to complete - countDownLatch1.await(10, java.util.concurrent.TimeUnit.SECONDS); + countDownLatchWait(countDownLatch); - // Test the 1st and 2nd metric counter updates assertThat(ociMetricsSupportBuilder.enabled(), is(true)); + // Test the 1st and 2nd metric counter updates assertThat(testMetricUpdateCounterValue[0].intValue(), is(equalTo(1))); assertThat(testMetricUpdateCounterValue[1].intValue(), is(equalTo(2))); @@ -152,14 +150,14 @@ public void testMetricUpdate() throws InterruptedException { } @Test - public void testEndpoint() throws InterruptedException { + void testEndpoint() throws InterruptedException { String originalEndPoint = endPoint; baseMetricRegistry.counter("baseDummyCounter1").inc(); vendorMetricRegistry.counter("vendorDummyCounter1").inc(); appMetricRegistry.counter("appDummyCounter1").inc(); - countDownLatch1 = new CountDownLatch(1); + CountDownLatch countDownLatch = new CountDownLatch(1); noOfExecutions = 0; doAnswer(invocationOnMock -> { @@ -167,7 +165,7 @@ public void testEndpoint() throws InterruptedException { PostMetricDataDetails postMetricDataDetails = postMetricDataRequest.getPostMetricDataDetails(); postingEndPoint = monitoringClient.getEndpoint(); // Give signal that metrics has been posted - countDownLatch1.countDown(); + countDownLatch.countDown(); return PostMetricDataResponse.builder() .__httpStatusCode__(200) .build(); @@ -187,19 +185,34 @@ public void testEndpoint() throws InterruptedException { WebServer webServer = createWebServer(routing); // Wait for metrics to be posted - countDownLatch1.await(10, java.util.concurrent.TimeUnit.SECONDS); + countDownLatchWait(countDownLatch); assertThat(ociMetricsSupportBuilder.enabled(), is(true)); // Verify that telemetry-ingestion endpoint is properly set during postin assertThat(postingEndPoint, startsWith("https://telemetry-ingestion.")); - // Verify that original endpoint is restored after metric posting - assertThat(monitoringClient.getEndpoint(), is(equalTo(originalEndPoint))); + // In a span of 10 seconds, verify that original endpoint is restored after metric posting + long start = System.currentTimeMillis(); + long end = start + 10 * 1000; + boolean endPointIsRestored = false; + while (System.currentTimeMillis() < end) { + if (monitoringClient.getEndpoint().equals(originalEndPoint)) { + endPointIsRestored = true; + break; + } + try { + Thread.sleep(100); + } catch (InterruptedException ie) { + fail("Failed with " + ie); + } + } + assertThat(endPointIsRestored, is(true)); + // assertThat(monitoringClient.getEndpoint(), is(equalTo(originalEndPoint))); + webServer.shutdown().await(10, java.util.concurrent.TimeUnit.SECONDS); } - @Test - public void testBatchSize() throws InterruptedException { + void testBatchSize() throws InterruptedException { baseMetricRegistry.counter("baseDummyCounter1").inc(); baseMetricRegistry.counter("baseDummyCounter2").inc(); baseMetricRegistry.counter("baseDummyCounter3").inc(); @@ -226,7 +239,7 @@ public void testBatchSize() throws InterruptedException { // Should be 4 int noOfBatches = Math.round(totalMetrics / batchSize) + (remainder > 0 ? 1 : 0); - countDownLatch1 = new CountDownLatch(1); + CountDownLatch countDownLatch = new CountDownLatch(1); noOfExecutions = 0; noOfMetrics = 0; @@ -238,7 +251,7 @@ public void testBatchSize() throws InterruptedException { noOfMetrics += metricSizeToPost; // Give signal that the last remaining metric in the last batch has been posted if (metricSizeToPost == remainder) { - countDownLatch1.countDown(); + countDownLatch.countDown(); } return PostMetricDataResponse.builder() .__httpStatusCode__(200) @@ -257,7 +270,6 @@ public void testBatchSize() throws InterruptedException { .batchSize(batchSize) .monitoringClient(monitoringClient); - Instant start = Instant.now(); Routing routing = createRouting(ociMetricsSupportBuilder); @@ -265,7 +277,7 @@ public void testBatchSize() throws InterruptedException { WebServer webServer = createWebServer(routing); // Wait for last batch to be completed - countDownLatch1.await(10, java.util.concurrent.TimeUnit.SECONDS); + countDownLatchWait(countDownLatch); Instant finish = Instant.now(); // Batch size of 3 for 10 metrics should yield 4 batches to post @@ -283,9 +295,7 @@ public void testBatchSize() throws InterruptedException { } @Test - public void testConfigSources() { - mockPostMetricDataAndGetTestMetricCount(); - + void testConfigSources() { baseMetricRegistry.counter("baseDummyCounter1").inc(); vendorMetricRegistry.counter("vendorDummyCounter1").inc(); @@ -303,9 +313,7 @@ public void testConfigSources() { } @Test - public void testMetricScope() { - mockPostMetricDataAndGetTestMetricCount(); - + void testMetricScope() { baseMetricRegistry.counter("baseDummyCounter1").inc(); vendorMetricRegistry.counter("vendorDummyCounter1").inc(); @@ -315,21 +323,19 @@ public void testMetricScope() { appMetricRegistry.counter("appDummyCounter2").inc(); appMetricRegistry.counter("appDummyCounter3").inc(); - validateMetricCount(new String[]{}, 6); - validateMetricCount(new String[]{Type.BASE.getName(), Type.VENDOR.getName(), Type.APPLICATION.getName()}, 6); - validateMetricCount(new String[]{Type.BASE.getName()}, 1); - validateMetricCount(new String[]{Type.VENDOR.getName()}, 2); - validateMetricCount(new String[]{Type.APPLICATION.getName()}, 3); - validateMetricCount(new String[]{"base", "vendor", "application"}, 6); - validateMetricCount(new String[]{"base"}, 1); - validateMetricCount(new String[]{"vendor"}, 2); - validateMetricCount(new String[]{"application"}, 3); + validateMetricCount(new String[] {}, 6); + validateMetricCount(new String[] {Type.BASE.getName(), Type.VENDOR.getName(), Type.APPLICATION.getName()}, 6); + validateMetricCount(new String[] {Type.BASE.getName()}, 1); + validateMetricCount(new String[] {Type.VENDOR.getName()}, 2); + validateMetricCount(new String[] {Type.APPLICATION.getName()}, 3); + validateMetricCount(new String[] {"base", "vendor", "application"}, 6); + validateMetricCount(new String[] {"base"}, 1); + validateMetricCount(new String[] {"vendor"}, 2); + validateMetricCount(new String[] {"application"}, 3); } @Test - public void testDisableMetrics() { - mockPostMetricDataAndGetTestMetricCount(); - + void testDisableMetrics() { baseMetricRegistry.counter("baseDummyCounter1").inc(); vendorMetricRegistry.counter("vendorDummyCounter1").inc(); appMetricRegistry.counter("appDummyCounter1").inc(); @@ -357,14 +363,14 @@ public void testDisableMetrics() { assertThat(testMetricCount, is(equalTo(0))); } - private void mockPostMetricDataAndGetTestMetricCount() { + private void mockPostMetricDataAndGetTestMetricCount(CountDownLatch countDownLatch) { // mock monitoringClient.postMetricData() doAnswer(invocationOnMock -> { PostMetricDataRequest postMetricDataRequest = invocationOnMock.getArgument(0); PostMetricDataDetails postMetricDataDetails = postMetricDataRequest.getPostMetricDataDetails(); testMetricCount = postMetricDataDetails.getMetricData().size(); // Give signal that testMetricCount was retrieved - countDownLatch1.countDown(); + countDownLatch.countDown(); return PostMetricDataResponse.builder() .__httpStatusCode__(200) .build(); @@ -374,7 +380,6 @@ private void mockPostMetricDataAndGetTestMetricCount() { private WebServer createWebServer(Routing routing) { WebServer webServer = WebServer.builder() .host("localhost") - .port(8888) .addRouting(routing) .build(); webServer.start().await(10L, TimeUnit.SECONDS); @@ -425,21 +430,28 @@ private void validateMetricCount(String[] scopes, int expectedMetricCount) { private void validateMetricCount(OciMetricsSupport.Builder ociMetricsSupportBuilder, int expectedMetricCount) { testMetricCount = 0; - countDownLatch1 = new CountDownLatch(1); + CountDownLatch countDownLatch = new CountDownLatch(1); + mockPostMetricDataAndGetTestMetricCount(countDownLatch); Routing routing = createRouting(ociMetricsSupportBuilder); WebServer webServer = createWebServer(routing); try { // Wait for signal from metric update that testMetricCount has been retrieved - countDownLatch1.await(10, TimeUnit.SECONDS); - } catch(InterruptedException e) { + countDownLatchWait(countDownLatch); + } catch (InterruptedException e) { fail("Error while waiting for testMetricCount: " + e.getMessage()); } webServer.shutdown().await(10, java.util.concurrent.TimeUnit.SECONDS); assertThat(testMetricCount, is(equalTo(expectedMetricCount))); } - private static void delay(long millis) { + private void countDownLatchWait(CountDownLatch countDownLatch) throws InterruptedException { + if (!countDownLatch.await(10, TimeUnit.SECONDS)) { + fail("CountDownLatch timed out"); + } + } + + private void delay(long millis) { try { Thread.sleep(millis); } catch (InterruptedException ie) {