|
53 | 53 | import java.util.Map.Entry; |
54 | 54 | import java.util.Properties; |
55 | 55 | import java.util.Random; |
| 56 | +import java.util.concurrent.ExecutionException; |
56 | 57 | import java.util.concurrent.TimeUnit; |
57 | 58 | import java.util.concurrent.atomic.AtomicReference; |
58 | 59 |
|
@@ -363,27 +364,38 @@ protected static void waitForApplicationToStart() throws Exception { |
363 | 364 | String.format("%s on %s", getAppContext(), containerInfo.getImageName()), |
364 | 365 | HEALTH_CHECK_RETRIES); |
365 | 366 | System.out.println("Test app health check complete."); |
366 | | - if (requestCaptureEnabled) { |
367 | | - Stopwatch sw = Stopwatch.createStarted(); |
368 | | - mockedIngestion.waitForItem(new Predicate<Envelope>() { |
369 | | - @Override |
370 | | - public boolean apply(Envelope input) { |
371 | | - if (!"RequestData".equals(input.getData().getBaseType())) { |
372 | | - return false; |
373 | | - } |
374 | | - RequestData data = (RequestData) ((Data) input.getData()).getBaseData(); |
375 | | - return contextRootUrl.equals(data.getUrl()) && "200".equals(data.getResponseCode()); |
376 | | - } |
377 | | - }, TELEMETRY_RECEIVE_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
378 | | - System.out.printf("Received request telemetry after %.3f seconds...%n", |
379 | | - sw.elapsed(TimeUnit.MILLISECONDS) / 1000.0); |
380 | | - System.out.println("Clearing any RequestData from health check."); |
381 | | - } |
| 367 | + waitForHealthCheckTelemetryIfNeeded(contextRootUrl); |
382 | 368 | } catch (Exception e) { |
383 | 369 | docker.printContainerLogs(containerInfo.getContainerId()); |
384 | 370 | throw e; |
| 371 | + } finally { |
| 372 | + mockedIngestion.resetData(); |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + private static void waitForHealthCheckTelemetryIfNeeded(final String contextRootUrl) throws InterruptedException, ExecutionException { |
| 377 | + if (!requestCaptureEnabled) { |
| 378 | + return; |
| 379 | + } |
| 380 | + |
| 381 | + Stopwatch receivedTelemetryTimer = Stopwatch.createStarted(); |
| 382 | + final int requestTelemetryFromHealthCheckTimeout = TELEMETRY_RECEIVE_TIMEOUT_SECONDS; |
| 383 | + try { |
| 384 | + mockedIngestion.waitForItem(new Predicate<Envelope>() { |
| 385 | + @Override |
| 386 | + public boolean apply(Envelope input) { |
| 387 | + if (!"RequestData".equals(input.getData().getBaseType())) { |
| 388 | + return false; |
| 389 | + } |
| 390 | + RequestData data = (RequestData) ((Data) input.getData()).getBaseData(); |
| 391 | + return contextRootUrl.equals(data.getUrl()) && "200".equals(data.getResponseCode()); |
| 392 | + } |
| 393 | + }, requestTelemetryFromHealthCheckTimeout, TimeUnit.SECONDS); |
| 394 | + System.out.printf("Received request telemetry after %.3f seconds...%n", receivedTelemetryTimer.elapsed(TimeUnit.MILLISECONDS) / 1000.0); |
| 395 | + System.out.println("Clearing any RequestData from health check."); |
| 396 | + } catch (java.util.concurrent.TimeoutException e) { |
| 397 | + throw new TimeoutException("request telemetry from application health check", requestTelemetryFromHealthCheckTimeout, TimeUnit.SECONDS, e); |
385 | 398 | } |
386 | | - mockedIngestion.resetData(); |
387 | 399 | } |
388 | 400 |
|
389 | 401 | protected void callTargetUriAndWaitForTelemetry() throws Exception { |
@@ -710,8 +722,7 @@ protected static void waitForUrl(String url, long timeout, TimeUnit timeoutUnit, |
710 | 722 | assertFalse(String.format("Empty response from '%s'. Health check urls should return something non-empty", url), rval.isEmpty()); |
711 | 723 | } |
712 | 724 |
|
713 | | - protected static void waitForUrlWithRetries(String url, long timeout, TimeUnit timeoutUnit, String appName, int numberOfRetries) |
714 | | - throws IOException { |
| 725 | + protected static void waitForUrlWithRetries(String url, long timeout, TimeUnit timeoutUnit, String appName, int numberOfRetries) throws IOException { |
715 | 726 | Preconditions.checkArgument(numberOfRetries >= 0, "numberOfRetries must be non-negative"); |
716 | 727 | int triedCount = 0; |
717 | 728 | boolean success = false; |
|
0 commit comments