Skip to content

Commit ad2b386

Browse files
authored
Fix EventCountersAndMetricsValues and VerifyCountersFireWithCorrectValues flaky tests (#59411)
1 parent 835792e commit ad2b386

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public async Task EventCountersAndMetricsValues()
2828
// Arrange
2929
var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString());
3030

31+
// requests-per-second isn't tested because the value can't be reliably tested because of time
3132
using var eventListener = new TestCounterListener(LoggerFactory, hostingEventSource.Name,
3233
[
33-
"requests-per-second",
3434
"total-requests",
3535
"current-requests",
3636
"failed-requests"
@@ -40,7 +40,6 @@ public async Task EventCountersAndMetricsValues()
4040
using CancellationTokenSource timeoutTokenSource = new CancellationTokenSource(timeout);
4141
timeoutTokenSource.Token.Register(() => Logger.LogError("Timeout while waiting for counter value."));
4242

43-
var rpsValues = eventListener.GetCounterValues("requests-per-second", timeoutTokenSource.Token).GetAsyncEnumerator();
4443
var totalRequestValues = eventListener.GetCounterValues("total-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
4544
var currentRequestValues = eventListener.GetCounterValues("current-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
4645
var failedRequestValues = eventListener.GetCounterValues("failed-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
@@ -64,19 +63,22 @@ public async Task EventCountersAndMetricsValues()
6463
using var requestDurationCollector2 = new MetricCollector<double>(testMeterFactory2, HostingMetrics.MeterName, "http.server.request.duration");
6564

6665
// Act/Assert 1
66+
Logger.LogInformation("Act/Assert 1");
67+
Logger.LogInformation(nameof(HostingApplication.CreateContext));
68+
6769
var context1 = hostingApplication1.CreateContext(features1);
6870
var context2 = hostingApplication2.CreateContext(features2);
6971

7072
await totalRequestValues.WaitForSumValueAsync(2);
71-
await rpsValues.WaitForValueAsync(2);
7273
await currentRequestValues.WaitForValueAsync(2);
7374
await failedRequestValues.WaitForValueAsync(0);
7475

76+
Logger.LogInformation(nameof(HostingApplication.DisposeContext));
77+
7578
hostingApplication1.DisposeContext(context1, null);
7679
hostingApplication2.DisposeContext(context2, null);
7780

7881
await totalRequestValues.WaitForSumValueAsync(2);
79-
await rpsValues.WaitForValueAsync(0);
8082
await currentRequestValues.WaitForValueAsync(0);
8183
await failedRequestValues.WaitForValueAsync(0);
8284

@@ -92,22 +94,25 @@ public async Task EventCountersAndMetricsValues()
9294
m => Assert.True(m.Value > 0));
9395

9496
// Act/Assert 2
97+
Logger.LogInformation("Act/Assert 2");
98+
Logger.LogInformation(nameof(HostingApplication.CreateContext));
99+
95100
context1 = hostingApplication1.CreateContext(features1);
96101
context2 = hostingApplication2.CreateContext(features2);
97102

98103
await totalRequestValues.WaitForSumValueAsync(4);
99-
await rpsValues.WaitForValueAsync(2);
100104
await currentRequestValues.WaitForValueAsync(2);
101105
await failedRequestValues.WaitForValueAsync(0);
102106

103107
context1.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
104108
context2.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
105109

110+
Logger.LogInformation(nameof(HostingApplication.DisposeContext));
111+
106112
hostingApplication1.DisposeContext(context1, null);
107113
hostingApplication2.DisposeContext(context2, null);
108114

109115
await totalRequestValues.WaitForSumValueAsync(4);
110-
await rpsValues.WaitForValueAsync(0);
111116
await currentRequestValues.WaitForValueAsync(0);
112117
await failedRequestValues.WaitForValueAsync(2);
113118

src/Hosting/Hosting/test/Internal/HostingEventSourceTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ public async Task VerifyCountersFireWithCorrectValues()
182182
// Arrange
183183
var hostingEventSource = GetHostingEventSource();
184184

185+
// requests-per-second isn't tested because the value can't be reliably tested because of time
185186
using var eventListener = new TestCounterListener(LoggerFactory, hostingEventSource.Name,
186187
[
187-
"requests-per-second",
188188
"total-requests",
189189
"current-requests",
190190
"failed-requests"
@@ -193,7 +193,6 @@ public async Task VerifyCountersFireWithCorrectValues()
193193
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
194194
timeoutTokenSource.Token.Register(() => Logger.LogError("Timeout while waiting for counter value."));
195195

196-
var rpsValues = eventListener.GetCounterValues("requests-per-second", timeoutTokenSource.Token).GetAsyncEnumerator();
197196
var totalRequestValues = eventListener.GetCounterValues("total-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
198197
var currentRequestValues = eventListener.GetCounterValues("current-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
199198
var failedRequestValues = eventListener.GetCounterValues("failed-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
@@ -209,23 +208,20 @@ public async Task VerifyCountersFireWithCorrectValues()
209208
hostingEventSource.RequestStart("GET", "/");
210209

211210
await totalRequestValues.WaitForSumValueAsync(1);
212-
await rpsValues.WaitForValueAsync(1);
213211
await currentRequestValues.WaitForValueAsync(1);
214212
await failedRequestValues.WaitForValueAsync(0);
215213

216214
Logger.LogInformation(nameof(HostingEventSource.RequestStop));
217215
hostingEventSource.RequestStop();
218216

219217
await totalRequestValues.WaitForSumValueAsync(1);
220-
await rpsValues.WaitForValueAsync(0);
221218
await currentRequestValues.WaitForValueAsync(0);
222219
await failedRequestValues.WaitForValueAsync(0);
223220

224221
Logger.LogInformation(nameof(HostingEventSource.RequestStart));
225222
hostingEventSource.RequestStart("POST", "/");
226223

227224
await totalRequestValues.WaitForSumValueAsync(2);
228-
await rpsValues.WaitForValueAsync(1);
229225
await currentRequestValues.WaitForValueAsync(1);
230226
await failedRequestValues.WaitForValueAsync(0);
231227

@@ -235,7 +231,6 @@ public async Task VerifyCountersFireWithCorrectValues()
235231
hostingEventSource.RequestStop();
236232

237233
await totalRequestValues.WaitForSumValueAsync(2);
238-
await rpsValues.WaitForValueAsync(0);
239234
await currentRequestValues.WaitForValueAsync(0);
240235
await failedRequestValues.WaitForValueAsync(1);
241236
}

0 commit comments

Comments
 (0)