@@ -149,7 +149,8 @@ public void EventCounter_StasisCounters_Functional()
149149 [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) ) ]
150150 public void EventCounter_ReclaimedConnectionsCounter_Functional ( )
151151 {
152- SqlConnection . ClearAllPools ( ) ;
152+ // clean pools and pool groups
153+ ClearConnectionPools ( ) ;
153154 var stringBuilder = new SqlConnectionStringBuilder ( DataTestUtility . TCPConnectionString ) { Pooling = true , MaxPoolSize = 1 } ;
154155
155156 long rc = SqlClientEventSourceProps . ReclaimedConnections ;
@@ -158,6 +159,7 @@ public void EventCounter_ReclaimedConnectionsCounter_Functional()
158159 // Specifying the generation number makes it to run faster by avoiding a full GC process
159160 GC . Collect ( gcNumber ) ;
160161 GC . WaitForPendingFinalizers ( ) ;
162+ System . Threading . Thread . Sleep ( 200 ) ; // give the pooler some time to reclaim the connection and avoid the conflict.
161163
162164 using ( SqlConnection conn = new SqlConnection ( stringBuilder . ToString ( ) ) )
163165 {
@@ -220,26 +222,32 @@ private static InternalConnectionWrapper CreateEmancipatedConnection(string conn
220222 private void ClearConnectionPools ( )
221223 {
222224 //ClearAllPoos kills all the existing pooled connection thus deactivating all the active pools
223- var liveConnectionPools = SqlClientEventSourceProps . ActiveConnectionPools +
225+ long liveConnectionPools = SqlClientEventSourceProps . ActiveConnectionPools +
224226 SqlClientEventSourceProps . InactiveConnectionPools ;
225227 SqlConnection . ClearAllPools ( ) ;
226228 Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPools , 0 , liveConnectionPools ) ;
227229 Assert . Equal ( 0 , SqlClientEventSourceProps . ActiveConnectionPools ) ;
228230
229- //the 1st PruneConnectionPoolGroups call cleans the dangling inactive connection pools
231+ long icp = SqlClientEventSourceProps . InactiveConnectionPools ;
232+
233+ // The 1st PruneConnectionPoolGroups call cleans the dangling inactive connection pools.
230234 PruneConnectionPoolGroups ( ) ;
231- Assert . Equal ( 0 , SqlClientEventSourceProps . InactiveConnectionPools ) ;
235+ // If the pool isn't empty, it's because there are active connections or distributed transactions that need it.
236+ Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPools , 0 , icp ) ;
232237
233238 //the 2nd call deactivates the dangling connection pool groups
234- var liveConnectionPoolGroups = SqlClientEventSourceProps . ActiveConnectionPoolGroups +
239+ long liveConnectionPoolGroups = SqlClientEventSourceProps . ActiveConnectionPoolGroups +
235240 SqlClientEventSourceProps . InactiveConnectionPoolGroups ;
241+ long acpg = SqlClientEventSourceProps . ActiveConnectionPoolGroups ;
236242 PruneConnectionPoolGroups ( ) ;
237243 Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPoolGroups , 0 , liveConnectionPoolGroups ) ;
238- Assert . Equal ( 0 , SqlClientEventSourceProps . ActiveConnectionPoolGroups ) ;
244+ // If the pool entry isn't empty, it's because there are active pools that need it.
245+ Assert . InRange ( SqlClientEventSourceProps . ActiveConnectionPoolGroups , 0 , acpg ) ;
239246
247+ long icpg = SqlClientEventSourceProps . InactiveConnectionPoolGroups ;
240248 //the 3rd call cleans the dangling connection pool groups
241249 PruneConnectionPoolGroups ( ) ;
242- Assert . Equal ( 0 , SqlClientEventSourceProps . InactiveConnectionPoolGroups ) ;
250+ Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPoolGroups , 0 , icpg ) ;
243251 }
244252
245253 private static void PruneConnectionPoolGroups ( )
0 commit comments