19
19
20
20
namespace Microsoft . AspNetCore . Hosting . Tests ;
21
21
22
- public class HostingApplicationDiagnosticsTests
22
+ public class HostingApplicationDiagnosticsTests : LoggedTest
23
23
{
24
24
[ Fact ]
25
25
[ QuarantinedTest ( "https://github.com/dotnet/aspnetcore/issues/57259" ) ]
@@ -28,16 +28,17 @@ public async Task EventCountersAndMetricsValues()
28
28
// Arrange
29
29
var hostingEventSource = new HostingEventSource ( Guid . NewGuid ( ) . ToString ( ) ) ;
30
30
31
- var eventListener = new TestCounterListener ( new [ ]
32
- {
31
+ using var eventListener = new TestCounterListener ( LoggerFactory , hostingEventSource . Name ,
32
+ [
33
33
"requests-per-second" ,
34
34
"total-requests" ,
35
35
"current-requests" ,
36
36
"failed-requests"
37
- } ) ;
37
+ ] ) ;
38
38
39
39
var timeout = ! Debugger . IsAttached ? TimeSpan . FromSeconds ( 30 ) : Timeout . InfiniteTimeSpan ;
40
40
using CancellationTokenSource timeoutTokenSource = new CancellationTokenSource ( timeout ) ;
41
+ timeoutTokenSource . Token . Register ( ( ) => Logger . LogError ( "Timeout while waiting for counter value." ) ) ;
41
42
42
43
var rpsValues = eventListener . GetCounterValues ( "requests-per-second" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
43
44
var totalRequestValues = eventListener . GetCounterValues ( "total-requests" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
@@ -53,8 +54,9 @@ public async Task EventCountersAndMetricsValues()
53
54
var testMeterFactory1 = new TestMeterFactory ( ) ;
54
55
var testMeterFactory2 = new TestMeterFactory ( ) ;
55
56
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 ) ;
58
60
59
61
using var activeRequestsCollector1 = new MetricCollector < long > ( testMeterFactory1 , HostingMetrics . MeterName , "http.server.active_requests" ) ;
60
62
using var activeRequestsCollector2 = new MetricCollector < long > ( testMeterFactory2 , HostingMetrics . MeterName , "http.server.active_requests" ) ;
@@ -65,18 +67,18 @@ public async Task EventCountersAndMetricsValues()
65
67
var context1 = hostingApplication1 . CreateContext ( features1 ) ;
66
68
var context2 = hostingApplication2 . CreateContext ( features2 ) ;
67
69
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 ) ;
72
74
73
75
hostingApplication1 . DisposeContext ( context1 , null ) ;
74
76
hostingApplication2 . DisposeContext ( context2 , null ) ;
75
77
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 ) ;
80
82
81
83
Assert . Collection ( activeRequestsCollector1 . GetMeasurementSnapshot ( ) ,
82
84
m => Assert . Equal ( 1 , m . Value ) ,
@@ -93,21 +95,21 @@ public async Task EventCountersAndMetricsValues()
93
95
context1 = hostingApplication1 . CreateContext ( features1 ) ;
94
96
context2 = hostingApplication2 . CreateContext ( features2 ) ;
95
97
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 ) ;
100
102
101
103
context1 . HttpContext . Response . StatusCode = StatusCodes . Status500InternalServerError ;
102
104
context2 . HttpContext . Response . StatusCode = StatusCodes . Status500InternalServerError ;
103
105
104
106
hostingApplication1 . DisposeContext ( context1 , null ) ;
105
107
hostingApplication2 . DisposeContext ( context2 , null ) ;
106
108
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 ) ;
111
113
112
114
Assert . Collection ( activeRequestsCollector1 . GetMeasurementSnapshot ( ) ,
113
115
m => Assert . Equal ( 1 , m . Value ) ,
@@ -133,13 +135,13 @@ public void EventCountersEnabled()
133
135
// Arrange
134
136
var hostingEventSource = new HostingEventSource ( Guid . NewGuid ( ) . ToString ( ) ) ;
135
137
136
- var eventListener = new TestCounterListener ( new [ ]
137
- {
138
+ using var eventListener = new TestCounterListener ( LoggerFactory , hostingEventSource . Name ,
139
+ [
138
140
"requests-per-second" ,
139
141
"total-requests" ,
140
142
"current-requests" ,
141
143
"failed-requests"
142
- } ) ;
144
+ ] ) ;
143
145
144
146
eventListener . EnableEvents ( hostingEventSource , EventLevel . Informational , EventKeywords . None ,
145
147
new Dictionary < string , string >
0 commit comments