1919
2020namespace Microsoft . AspNetCore . Hosting . Tests ;
2121
22- public class HostingApplicationDiagnosticsTests
22+ public class HostingApplicationDiagnosticsTests : LoggedTest
2323{
2424 [ Fact ]
2525 [ QuarantinedTest ( "https://github.com/dotnet/aspnetcore/issues/57259" ) ]
@@ -28,16 +28,17 @@ public async Task EventCountersAndMetricsValues()
2828 // Arrange
2929 var hostingEventSource = new HostingEventSource ( Guid . NewGuid ( ) . ToString ( ) ) ;
3030
31- var eventListener = new TestCounterListener ( new [ ]
32- {
31+ using var eventListener = new TestCounterListener ( LoggerFactory , hostingEventSource . Name ,
32+ [
3333 "requests-per-second" ,
3434 "total-requests" ,
3535 "current-requests" ,
3636 "failed-requests"
37- } ) ;
37+ ] ) ;
3838
3939 var timeout = ! Debugger . IsAttached ? TimeSpan . FromSeconds ( 30 ) : Timeout . InfiniteTimeSpan ;
4040 using CancellationTokenSource timeoutTokenSource = new CancellationTokenSource ( timeout ) ;
41+ timeoutTokenSource . Token . Register ( ( ) => Logger . LogError ( "Timeout while waiting for counter value." ) ) ;
4142
4243 var rpsValues = eventListener . GetCounterValues ( "requests-per-second" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
4344 var totalRequestValues = eventListener . GetCounterValues ( "total-requests" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
@@ -53,8 +54,9 @@ public async Task EventCountersAndMetricsValues()
5354 var testMeterFactory1 = new TestMeterFactory ( ) ;
5455 var testMeterFactory2 = new TestMeterFactory ( ) ;
5556
56- var hostingApplication1 = CreateApplication ( out var features1 , eventSource : hostingEventSource , meterFactory : testMeterFactory1 ) ;
57- var hostingApplication2 = CreateApplication ( out var features2 , eventSource : hostingEventSource , meterFactory : testMeterFactory2 ) ;
57+ var logger = LoggerFactory . CreateLogger < HostingApplication > ( ) ;
58+ var hostingApplication1 = CreateApplication ( out var features1 , eventSource : hostingEventSource , meterFactory : testMeterFactory1 , logger : logger ) ;
59+ var hostingApplication2 = CreateApplication ( out var features2 , eventSource : hostingEventSource , meterFactory : testMeterFactory2 , logger : logger ) ;
5860
5961 using var activeRequestsCollector1 = new MetricCollector < long > ( testMeterFactory1 , HostingMetrics . MeterName , "http.server.active_requests" ) ;
6062 using var activeRequestsCollector2 = new MetricCollector < long > ( testMeterFactory2 , HostingMetrics . MeterName , "http.server.active_requests" ) ;
@@ -65,18 +67,18 @@ public async Task EventCountersAndMetricsValues()
6567 var context1 = hostingApplication1 . CreateContext ( features1 ) ;
6668 var context2 = hostingApplication2 . CreateContext ( features2 ) ;
6769
68- Assert . Equal ( 2 , await totalRequestValues . FirstOrDefault ( v => v == 2 ) ) ;
69- Assert . Equal ( 2 , await rpsValues . FirstOrDefault ( v => v == 2 ) ) ;
70- Assert . Equal ( 2 , await currentRequestValues . FirstOrDefault ( v => v == 2 ) ) ;
71- Assert . Equal ( 0 , await failedRequestValues . FirstOrDefault ( v => v == 0 ) ) ;
70+ await totalRequestValues . WaitForSumValueAsync ( 2 ) ;
71+ await rpsValues . WaitForValueAsync ( 2 ) ;
72+ await currentRequestValues . WaitForValueAsync ( 2 ) ;
73+ await failedRequestValues . WaitForValueAsync ( 0 ) ;
7274
7375 hostingApplication1 . DisposeContext ( context1 , null ) ;
7476 hostingApplication2 . DisposeContext ( context2 , null ) ;
7577
76- Assert . Equal ( 2 , await totalRequestValues . FirstOrDefault ( v => v == 2 ) ) ;
77- Assert . Equal ( 0 , await rpsValues . FirstOrDefault ( v => v == 0 ) ) ;
78- Assert . Equal ( 0 , await currentRequestValues . FirstOrDefault ( v => v == 0 ) ) ;
79- Assert . Equal ( 0 , await failedRequestValues . FirstOrDefault ( v => v == 0 ) ) ;
78+ await totalRequestValues . WaitForSumValueAsync ( 2 ) ;
79+ await rpsValues . WaitForValueAsync ( 0 ) ;
80+ await currentRequestValues . WaitForValueAsync ( 0 ) ;
81+ await failedRequestValues . WaitForValueAsync ( 0 ) ;
8082
8183 Assert . Collection ( activeRequestsCollector1 . GetMeasurementSnapshot ( ) ,
8284 m => Assert . Equal ( 1 , m . Value ) ,
@@ -93,21 +95,21 @@ public async Task EventCountersAndMetricsValues()
9395 context1 = hostingApplication1 . CreateContext ( features1 ) ;
9496 context2 = hostingApplication2 . CreateContext ( features2 ) ;
9597
96- Assert . Equal ( 4 , await totalRequestValues . FirstOrDefault ( v => v == 4 ) ) ;
97- Assert . Equal ( 2 , await rpsValues . FirstOrDefault ( v => v == 2 ) ) ;
98- Assert . Equal ( 2 , await currentRequestValues . FirstOrDefault ( v => v == 2 ) ) ;
99- Assert . Equal ( 0 , await failedRequestValues . FirstOrDefault ( v => v == 0 ) ) ;
98+ await totalRequestValues . WaitForSumValueAsync ( 4 ) ;
99+ await rpsValues . WaitForValueAsync ( 2 ) ;
100+ await currentRequestValues . WaitForValueAsync ( 2 ) ;
101+ await failedRequestValues . WaitForValueAsync ( 0 ) ;
100102
101103 context1 . HttpContext . Response . StatusCode = StatusCodes . Status500InternalServerError ;
102104 context2 . HttpContext . Response . StatusCode = StatusCodes . Status500InternalServerError ;
103105
104106 hostingApplication1 . DisposeContext ( context1 , null ) ;
105107 hostingApplication2 . DisposeContext ( context2 , null ) ;
106108
107- Assert . Equal ( 4 , await totalRequestValues . FirstOrDefault ( v => v == 4 ) ) ;
108- Assert . Equal ( 0 , await rpsValues . FirstOrDefault ( v => v == 0 ) ) ;
109- Assert . Equal ( 0 , await currentRequestValues . FirstOrDefault ( v => v == 0 ) ) ;
110- Assert . Equal ( 2 , await failedRequestValues . FirstOrDefault ( v => v == 2 ) ) ;
109+ await totalRequestValues . WaitForSumValueAsync ( 4 ) ;
110+ await rpsValues . WaitForValueAsync ( 0 ) ;
111+ await currentRequestValues . WaitForValueAsync ( 0 ) ;
112+ await failedRequestValues . WaitForValueAsync ( 2 ) ;
111113
112114 Assert . Collection ( activeRequestsCollector1 . GetMeasurementSnapshot ( ) ,
113115 m => Assert . Equal ( 1 , m . Value ) ,
@@ -133,13 +135,13 @@ public void EventCountersEnabled()
133135 // Arrange
134136 var hostingEventSource = new HostingEventSource ( Guid . NewGuid ( ) . ToString ( ) ) ;
135137
136- var eventListener = new TestCounterListener ( new [ ]
137- {
138+ using var eventListener = new TestCounterListener ( LoggerFactory , hostingEventSource . Name ,
139+ [
138140 "requests-per-second" ,
139141 "total-requests" ,
140142 "current-requests" ,
141143 "failed-requests"
142- } ) ;
144+ ] ) ;
143145
144146 eventListener . EnableEvents ( hostingEventSource , EventLevel . Informational , EventKeywords . None ,
145147 new Dictionary < string , string >
0 commit comments