55using System . Diagnostics . Tracing ;
66using Microsoft . AspNetCore . Internal ;
77using Microsoft . AspNetCore . InternalTesting ;
8+ using Microsoft . Extensions . Logging ;
89
910namespace Microsoft . AspNetCore . ConcurrencyLimiter . Tests ;
1011
@@ -55,7 +56,7 @@ public async Task TracksQueueLength()
5556
5657 using var timeoutTokenSource = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) ;
5758
58- var lengthValues = eventListener . GetCounterValues ( "queue-length" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
59+ var lengthValues = eventListener . GetCounterValues ( "queue-length" , timeoutTokenSource . Token ) ;
5960
6061 eventListener . EnableEvents ( eventSource , EventLevel . Informational , EventKeywords . None ,
6162 new Dictionary < string , string >
@@ -66,20 +67,20 @@ public async Task TracksQueueLength()
6667 // Act
6768 eventSource . RequestRejected ( ) ;
6869
69- Assert . True ( await UntilValueMatches ( lengthValues , 0 ) ) ;
70+ await WaitForCounterValue ( lengthValues , expectedValue : 0 , Logger ) ;
7071 using ( eventSource . QueueTimer ( ) )
7172 {
72- Assert . True ( await UntilValueMatches ( lengthValues , 1 ) ) ;
73+ await WaitForCounterValue ( lengthValues , expectedValue : 1 , Logger ) ;
7374
7475 using ( eventSource . QueueTimer ( ) )
7576 {
76- Assert . True ( await UntilValueMatches ( lengthValues , 2 ) ) ;
77+ await WaitForCounterValue ( lengthValues , expectedValue : 2 , Logger ) ;
7778 }
7879
79- Assert . True ( await UntilValueMatches ( lengthValues , 1 ) ) ;
80+ await WaitForCounterValue ( lengthValues , expectedValue : 1 , Logger ) ;
8081 }
8182
82- Assert . True ( await UntilValueMatches ( lengthValues , 0 ) ) ;
83+ await WaitForCounterValue ( lengthValues , expectedValue : 0 , Logger ) ;
8384 }
8485
8586 [ Fact ]
@@ -96,7 +97,7 @@ public async Task TracksDurationSpentInQueue()
9697
9798 using var timeoutTokenSource = new CancellationTokenSource ( TimeSpan . FromSeconds ( 5 ) ) ;
9899
99- var durationValues = eventListener . GetCounterValues ( "queue-duration" , timeoutTokenSource . Token ) . GetAsyncEnumerator ( ) ;
100+ var durationValues = eventListener . GetCounterValues ( "queue-duration" , timeoutTokenSource . Token ) ;
100101
101102 eventListener . EnableEvents ( eventSource , EventLevel . Informational , EventKeywords . None ,
102103 new Dictionary < string , string >
@@ -105,17 +106,17 @@ public async Task TracksDurationSpentInQueue()
105106 } ) ;
106107
107108 // Act
108- Assert . True ( await UntilValueMatches ( durationValues , 0 ) ) ;
109+ await WaitForCounterValue ( durationValues , expectedValue : 0 , Logger ) ;
109110
110111 using ( eventSource . QueueTimer ( ) )
111112 {
112- Assert . True ( await UntilValueMatches ( durationValues , 0 ) ) ;
113+ await WaitForCounterValue ( durationValues , expectedValue : 0 , Logger ) ;
113114 }
114115
115116 // check that something (anything!) has been written
116- while ( await durationValues . MoveNextAsync ( ) )
117+ while ( await durationValues . Values . MoveNextAsync ( ) )
117118 {
118- if ( durationValues . Current > 0 )
119+ if ( durationValues . Values . Current > 0 )
119120 {
120121 return ;
121122 }
@@ -124,17 +125,9 @@ public async Task TracksDurationSpentInQueue()
124125 throw new TimeoutException ( ) ;
125126 }
126127
127- private async Task < bool > UntilValueMatches ( IAsyncEnumerator < double > enumerator , int value )
128+ private static async Task WaitForCounterValue ( CounterValues values , double expectedValue , ILogger logger )
128129 {
129- while ( await enumerator . MoveNextAsync ( ) )
130- {
131- if ( enumerator . Current == value )
132- {
133- return true ;
134- }
135- }
136-
137- return false ;
130+ await values . Values . WaitForValueAsync ( expectedValue , values . CounterName , logger ) ;
138131 }
139132
140133 private static ConcurrencyLimiterEventSource GetConcurrencyLimiterEventSource ( )
0 commit comments