Skip to content

Commit 8b7cd8c

Browse files
committed
pr comments
1 parent a3e29d8 commit 8b7cd8c

File tree

2 files changed

+95
-76
lines changed

2 files changed

+95
-76
lines changed

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

Lines changed: 93 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -39,115 +39,135 @@ abstract class LiveMetricsTest {
3939

4040
@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();
4141

42-
private boolean foundExceptionDoc = false;
43-
private boolean foundTraceDoc = false;
44-
private boolean foundDependency = false;
45-
private boolean foundRequest = false;
46-
4742
@Test
4843
@TargetUri("/test")
4944
void testTelemetryDataFlow() throws java.lang.Exception {
5045
Awaitility.await()
5146
.atMost(Duration.ofSeconds(60))
5247
.until(() -> testing.mockedIngestion.getCountForType("RequestData") == 1);
5348

49+
PostBodyVerifier postBodyVerifier = new PostBodyVerifier();
50+
5451
assertThat(testing.mockedIngestion.isPingReceived()).isTrue();
5552

5653
List<String> postBodies = testing.mockedIngestion.getPostBodies();
5754
assertThat(postBodies).hasSizeGreaterThan(0); // should post at least once
5855

5956
for (String postBody : postBodies) {
60-
searchPostBody(postBody);
57+
postBodyVerifier.searchPostBody(postBody);
6158
}
6259

63-
assertThat(foundExceptionDoc).isTrue();
64-
assertThat(foundTraceDoc).isTrue();
65-
assertThat(foundDependency).isTrue();
66-
assertThat(foundRequest).isTrue();
60+
assertThat(postBodyVerifier.hasExceptionDoc()).isTrue();
61+
assertThat(postBodyVerifier.hasTraceDoc()).isTrue();
62+
assertThat(postBodyVerifier.hasDependency()).isTrue();
63+
assertThat(postBodyVerifier.hasRequest()).isTrue();
6764
}
6865

69-
private void searchPostBody(String postBody) {
70-
// Each post body is a list with a singular MonitoringDataPoint
71-
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
72-
try {
73-
JsonReader reader = JsonProviders.createReader(postBody);
74-
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
75-
} catch (IOException e) {
76-
throw new IllegalStateException("Failed to parse post request body", e);
66+
class PostBodyVerifier {
67+
boolean foundExceptionDoc = false;
68+
boolean foundTraceDoc = false;
69+
boolean foundDependency = false;
70+
boolean foundRequest = false;
71+
72+
public void searchPostBody(String postBody) {
73+
// Each post body is a list with a singular MonitoringDataPoint
74+
List<MonitoringDataPoint> dataPoints = new ArrayList<>();
75+
try {
76+
JsonReader reader = JsonProviders.createReader(postBody);
77+
dataPoints = reader.readArray(MonitoringDataPoint::fromJson);
78+
} catch (IOException e) {
79+
throw new IllegalStateException("Failed to parse post request body", e);
80+
}
81+
82+
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
83+
// post
84+
assertThat(dataPoints).hasSize(1);
85+
MonitoringDataPoint dataPoint = dataPoints.get(0);
86+
87+
List<DocumentIngress> docs = dataPoint.getDocuments();
88+
List<MetricPoint> metrics = dataPoint.getMetrics();
89+
90+
confirmDocsAreFiltered(docs);
91+
confirmPerfCountersNonZero(metrics);
92+
foundExceptionDoc = foundExceptionDoc || hasException(docs);
93+
foundTraceDoc = foundTraceDoc || hasTrace(docs);
94+
foundDependency = foundDependency || hasDependency(metrics);
95+
foundRequest = foundRequest || hasRequest(metrics);
7796
}
7897

79-
// Because the mock ping/posts should succeed, we should only have one MonitoringDataPoint per
80-
// post
81-
assertThat(dataPoints).hasSize(1);
82-
MonitoringDataPoint dataPoint = dataPoints.get(0);
98+
public boolean hasExceptionDoc() {
99+
return foundExceptionDoc;
100+
}
83101

84-
List<DocumentIngress> docs = dataPoint.getDocuments();
85-
List<MetricPoint> metrics = dataPoint.getMetrics();
102+
public boolean hasTraceDoc() {
103+
return foundTraceDoc;
104+
}
86105

87-
confirmDocsAreFiltered(docs);
88-
confirmPerfCountersNonZero(metrics);
89-
foundExceptionDoc = foundExceptionDoc || hasException(docs);
90-
foundTraceDoc = foundTraceDoc || hasTrace(docs);
91-
foundDependency = foundDependency || hasDependency(metrics);
92-
foundRequest = foundRequest || hasRequest(metrics);
93-
}
106+
public boolean hasDependency() {
107+
return foundDependency;
108+
}
94109

95-
private void confirmDocsAreFiltered(List<DocumentIngress> docs) {
96-
for (DocumentIngress doc : docs) {
97-
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
98-
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
110+
public boolean hasRequest() {
111+
return foundRequest;
99112
}
100-
}
101113

102-
private boolean hasException(List<DocumentIngress> docs) {
103-
for (DocumentIngress doc : docs) {
104-
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
105-
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
106-
return true;
114+
private void confirmDocsAreFiltered(List<DocumentIngress> docs) {
115+
for (DocumentIngress doc : docs) {
116+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REMOTE_DEPENDENCY);
117+
assertThat(doc.getDocumentType()).isNotEqualTo(DocumentType.REQUEST);
107118
}
108119
}
109-
return false;
110-
}
111120

112-
private boolean hasTrace(List<DocumentIngress> docs) {
113-
for (DocumentIngress doc : docs) {
114-
if (doc.getDocumentType().equals(DocumentType.TRACE)
115-
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
116-
return true;
121+
private boolean hasException(List<DocumentIngress> docs) {
122+
for (DocumentIngress doc : docs) {
123+
if (doc.getDocumentType().equals(DocumentType.EXCEPTION)
124+
&& ((Exception) doc).getExceptionMessage().equals("Fake Exception")) {
125+
return true;
126+
}
117127
}
128+
return false;
118129
}
119-
return false;
120-
}
121130

122-
private boolean hasDependency(List<MetricPoint> metrics) {
123-
for (MetricPoint metric : metrics) {
124-
String name = metric.getName();
125-
double value = metric.getValue();
126-
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec")) {
127-
return value == 1;
131+
private boolean hasTrace(List<DocumentIngress> docs) {
132+
for (DocumentIngress doc : docs) {
133+
if (doc.getDocumentType().equals(DocumentType.TRACE)
134+
&& ((Trace) doc).getMessage().equals("This message should generate a trace")) {
135+
return true;
136+
}
128137
}
138+
return false;
129139
}
130-
return false;
131-
}
132140

133-
private boolean hasRequest(List<MetricPoint> metrics) {
134-
for (MetricPoint metric : metrics) {
135-
String name = metric.getName();
136-
double value = metric.getValue();
137-
if (name.equals("\\ApplicationInsights\\Requests/Sec")) {
138-
return value == 1;
141+
private boolean hasDependency(List<MetricPoint> metrics) {
142+
for (MetricPoint metric : metrics) {
143+
String name = metric.getName();
144+
double value = metric.getValue();
145+
if (name.equals("\\ApplicationInsights\\Dependency Calls/Sec")) {
146+
return value == 1;
147+
}
139148
}
149+
return false;
150+
}
151+
152+
private boolean hasRequest(List<MetricPoint> metrics) {
153+
for (MetricPoint metric : metrics) {
154+
String name = metric.getName();
155+
double value = metric.getValue();
156+
if (name.equals("\\ApplicationInsights\\Requests/Sec")) {
157+
return value == 1;
158+
}
159+
}
160+
return false;
140161
}
141-
return false;
142-
}
143162

144-
private void confirmPerfCountersNonZero(List<MetricPoint> metrics) {
145-
for (MetricPoint metric : metrics) {
146-
String name = metric.getName();
147-
double value = metric.getValue();
148-
if (name.equals("\\Process\\Physical Bytes")
149-
|| name.equals("\\% Process\\Processor Time Normalized")) {
150-
assertThat(value).isNotEqualTo(0);
163+
private void confirmPerfCountersNonZero(List<MetricPoint> metrics) {
164+
for (MetricPoint metric : metrics) {
165+
String name = metric.getName();
166+
double value = metric.getValue();
167+
if (name.equals("\\Process\\Physical Bytes")
168+
|| name.equals("\\% Process\\Processor Time Normalized")) {
169+
assertThat(value).isNotEqualTo(0);
170+
}
151171
}
152172
}
153173
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.google.common.io.CharStreams;
77
import java.io.IOException;
88
import java.io.StringWriter;
9-
import java.rmi.ServerError;
109
import java.util.ArrayList;
1110
import java.util.List;
1211
import java.util.concurrent.atomic.AtomicBoolean;
@@ -61,8 +60,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
6160
resp.setHeader("x-ms-qps-subscribed", "true");
6261
resp.setHeader("x-ms-qps-configuration-etag", "fake::etag");
6362
} else {
64-
throw new ServerError(
65-
"Unexpected path: " + path + " please fix the test/mock server setup", new Error());
63+
throw new IllegalStateException(
64+
"Unexpected path: " + path + " please fix the test/mock server setup");
6665
}
6766
}
6867

0 commit comments

Comments
 (0)