Skip to content

Commit

Permalink
Fix EventCountersAndMetricsValues and VerifyCountersFireWithCorrectVa…
Browse files Browse the repository at this point in the history
…lues flaky tests (dotnet#59411)
  • Loading branch information
JamesNK authored Dec 12, 2024
1 parent 835792e commit ad2b386
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public async Task EventCountersAndMetricsValues()
// Arrange
var hostingEventSource = new HostingEventSource(Guid.NewGuid().ToString());

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

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

// Act/Assert 1
Logger.LogInformation("Act/Assert 1");
Logger.LogInformation(nameof(HostingApplication.CreateContext));

var context1 = hostingApplication1.CreateContext(features1);
var context2 = hostingApplication2.CreateContext(features2);

await totalRequestValues.WaitForSumValueAsync(2);
await rpsValues.WaitForValueAsync(2);
await currentRequestValues.WaitForValueAsync(2);
await failedRequestValues.WaitForValueAsync(0);

Logger.LogInformation(nameof(HostingApplication.DisposeContext));

hostingApplication1.DisposeContext(context1, null);
hostingApplication2.DisposeContext(context2, null);

await totalRequestValues.WaitForSumValueAsync(2);
await rpsValues.WaitForValueAsync(0);
await currentRequestValues.WaitForValueAsync(0);
await failedRequestValues.WaitForValueAsync(0);

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

// Act/Assert 2
Logger.LogInformation("Act/Assert 2");
Logger.LogInformation(nameof(HostingApplication.CreateContext));

context1 = hostingApplication1.CreateContext(features1);
context2 = hostingApplication2.CreateContext(features2);

await totalRequestValues.WaitForSumValueAsync(4);
await rpsValues.WaitForValueAsync(2);
await currentRequestValues.WaitForValueAsync(2);
await failedRequestValues.WaitForValueAsync(0);

context1.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
context2.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;

Logger.LogInformation(nameof(HostingApplication.DisposeContext));

hostingApplication1.DisposeContext(context1, null);
hostingApplication2.DisposeContext(context2, null);

await totalRequestValues.WaitForSumValueAsync(4);
await rpsValues.WaitForValueAsync(0);
await currentRequestValues.WaitForValueAsync(0);
await failedRequestValues.WaitForValueAsync(2);

Expand Down
7 changes: 1 addition & 6 deletions src/Hosting/Hosting/test/Internal/HostingEventSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ public async Task VerifyCountersFireWithCorrectValues()
// Arrange
var hostingEventSource = GetHostingEventSource();

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

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

await totalRequestValues.WaitForSumValueAsync(1);
await rpsValues.WaitForValueAsync(1);
await currentRequestValues.WaitForValueAsync(1);
await failedRequestValues.WaitForValueAsync(0);

Logger.LogInformation(nameof(HostingEventSource.RequestStop));
hostingEventSource.RequestStop();

await totalRequestValues.WaitForSumValueAsync(1);
await rpsValues.WaitForValueAsync(0);
await currentRequestValues.WaitForValueAsync(0);
await failedRequestValues.WaitForValueAsync(0);

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

await totalRequestValues.WaitForSumValueAsync(2);
await rpsValues.WaitForValueAsync(1);
await currentRequestValues.WaitForValueAsync(1);
await failedRequestValues.WaitForValueAsync(0);

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

await totalRequestValues.WaitForSumValueAsync(2);
await rpsValues.WaitForValueAsync(0);
await currentRequestValues.WaitForValueAsync(0);
await failedRequestValues.WaitForValueAsync(1);
}
Expand Down

0 comments on commit ad2b386

Please sign in to comment.