Skip to content

Commit 0f33883

Browse files
committed
Fix flaky live metrics smoke test
1 parent 8dbbe92 commit 0f33883

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

smoke-tests/apps/LiveMetrics/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LiveMetricsTest.java

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
1818
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.awaitility.Awaitility.await;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2022

2123
import com.azure.json.JsonProviders;
2224
import com.azure.json.JsonReader;
@@ -28,9 +30,7 @@
2830
import com.azure.monitor.opentelemetry.autoconfigure.implementation.quickpulse.swagger.models.Trace;
2931
import java.io.IOException;
3032
import java.time.Duration;
31-
import java.util.ArrayList;
3233
import java.util.List;
33-
import org.awaitility.Awaitility;
3434
import org.junit.jupiter.api.Test;
3535
import org.junit.jupiter.api.extension.RegisterExtension;
3636

@@ -41,42 +41,39 @@ abstract class LiveMetricsTest {
4141

4242
@Test
4343
@TargetUri("/test")
44-
void testTelemetryDataFlow() throws java.lang.Exception {
45-
Awaitility.await()
44+
void testTelemetryDataFlow() {
45+
await()
4646
.atMost(Duration.ofSeconds(60))
4747
.until(() -> testing.mockedIngestion.getCountForType("RequestData") == 1);
4848

49-
PostBodyVerifier postBodyVerifier = new PostBodyVerifier();
50-
5149
assertThat(testing.mockedIngestion.isPingReceived()).isTrue();
5250

53-
List<String> postBodies = testing.mockedIngestion.getPostBodies();
54-
assertThat(postBodies).hasSizeGreaterThan(0); // should post at least once
55-
56-
for (String postBody : postBodies) {
57-
postBodyVerifier.searchPostBody(postBody);
58-
}
59-
60-
assertThat(postBodyVerifier.hasExceptionDoc()).isTrue();
61-
assertThat(postBodyVerifier.hasTraceDoc()).isTrue();
62-
assertThat(postBodyVerifier.hasDependency()).isTrue();
63-
assertThat(postBodyVerifier.hasRequest()).isTrue();
51+
await()
52+
.untilAsserted(
53+
() -> {
54+
PostBodyVerifier verifier = new PostBodyVerifier();
55+
for (String postBody : testing.mockedIngestion.getPostBodies()) {
56+
verifier.searchPostBody(postBody);
57+
}
58+
59+
assertTrue(verifier.hasExceptionDoc());
60+
assertTrue(verifier.hasTraceDoc());
61+
assertTrue(verifier.hasDependency());
62+
assertTrue(verifier.hasRequest());
63+
});
6464
}
6565

66-
class PostBodyVerifier {
67-
boolean foundExceptionDoc = false;
68-
boolean foundTraceDoc = false;
69-
boolean foundDependency = false;
70-
boolean foundRequest = false;
66+
static class PostBodyVerifier {
67+
boolean foundExceptionDoc;
68+
boolean foundTraceDoc;
69+
boolean foundDependency;
70+
boolean foundRequest;
7171

72-
public void searchPostBody(String postBody) {
72+
public void searchPostBody(String postBody) throws IOException {
7373
// Each post body is a list with a singular MonitoringDataPoint
74-
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
75-
try {
76-
JsonReader reader = JsonProviders.createReader(postBody);
74+
List<MonitoringDataPoint> dataPoints;
75+
try (JsonReader reader = JsonProviders.createReader(postBody)) {
7776
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
78-
} catch (IOException e) {
79-
throw new IllegalStateException("Failed to parse post request body", e);
8077
}
8178

8279
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
@@ -143,7 +140,10 @@ private boolean hasDependency(List<MetricPoint> metrics) {
143140
String name = metric.getName();
144141
double value = metric.getValue();
145142
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec")) {
146-
return value == 1;
143+
// TODO wait for the live metrics from health check to be emitted
144+
// before calling MockedQuickPulseServlet.resetData()
145+
// then we can assert that the value is exactly == 1
146+
return value >= 1;
147147
}
148148
}
149149
return false;
@@ -154,7 +154,10 @@ private boolean hasRequest(List<MetricPoint> metrics) {
154154
String name = metric.getName();
155155
double value = metric.getValue();
156156
if (name.equals("\\ApplicationInsights\\Requests/Sec")) {
157-
return value == 1;
157+
// TODO wait for the live metrics from health check to be emitted
158+
// before calling MockedQuickPulseServlet.resetData()
159+
// then we can assert that the value is exactly == 1
160+
return value >= 1;
158161
}
159162
}
160163
return false;

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/SmokeTestExtension.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ private void clearOutAnyInitLogs() throws Exception {
284284
String contextRootUrl = getBaseUrl() + "/";
285285
HttpHelper.getResponseCodeEnsuringSampled(contextRootUrl);
286286
waitForHealthCheckTelemetry(contextRootUrl);
287+
// wait for live metrics to start sending data (needed for LiveMetricsTest)
288+
await().untilAsserted(() -> assertThat(mockedIngestion.getPostBodies()).isNotEmpty());
289+
287290
System.out.println("Clearing any RequestData from health check.");
288291
mockedIngestion.resetData();
289292
}

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/fakeingestion/MockedAppInsightsIngestionServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public void stopServer() throws Exception {
5555
}
5656

5757
public void resetData() {
58-
this.servlet.resetData();
58+
servlet.resetData();
59+
quickPulseServlet.resetData();
5960
}
6061

6162
public boolean hasData() {

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/fakeingestion/MockedQuickPulseServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ public List<String> getPostBodies() {
7878
public void setRequestLoggingEnabled(boolean enabled) {
7979
loggingEnabled = enabled;
8080
}
81+
82+
public void resetData() {
83+
synchronized (lock) {
84+
postBodies.clear();
85+
}
86+
}
8187
}

0 commit comments

Comments
 (0)