@@ -149,7 +149,8 @@ public void EventCounter_StasisCounters_Functional()
149
149
[ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) ) ]
150
150
public void EventCounter_ReclaimedConnectionsCounter_Functional ( )
151
151
{
152
- SqlConnection . ClearAllPools ( ) ;
152
+ // clean pools and pool groups
153
+ ClearConnectionPools ( ) ;
153
154
var stringBuilder = new SqlConnectionStringBuilder ( DataTestUtility . TCPConnectionString ) { Pooling = true , MaxPoolSize = 1 } ;
154
155
155
156
long rc = SqlClientEventSourceProps . ReclaimedConnections ;
@@ -158,6 +159,7 @@ public void EventCounter_ReclaimedConnectionsCounter_Functional()
158
159
// Specifying the generation number makes it to run faster by avoiding a full GC process
159
160
GC . Collect ( gcNumber ) ;
160
161
GC . WaitForPendingFinalizers ( ) ;
162
+ System . Threading . Thread . Sleep ( 200 ) ; // give the pooler some time to reclaim the connection and avoid the conflict.
161
163
162
164
using ( SqlConnection conn = new SqlConnection ( stringBuilder . ToString ( ) ) )
163
165
{
@@ -220,26 +222,32 @@ private static InternalConnectionWrapper CreateEmancipatedConnection(string conn
220
222
private void ClearConnectionPools ( )
221
223
{
222
224
//ClearAllPoos kills all the existing pooled connection thus deactivating all the active pools
223
- var liveConnectionPools = SqlClientEventSourceProps . ActiveConnectionPools +
225
+ long liveConnectionPools = SqlClientEventSourceProps . ActiveConnectionPools +
224
226
SqlClientEventSourceProps . InactiveConnectionPools ;
225
227
SqlConnection . ClearAllPools ( ) ;
226
228
Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPools , 0 , liveConnectionPools ) ;
227
229
Assert . Equal ( 0 , SqlClientEventSourceProps . ActiveConnectionPools ) ;
228
230
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.
230
234
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 ) ;
232
237
233
238
//the 2nd call deactivates the dangling connection pool groups
234
- var liveConnectionPoolGroups = SqlClientEventSourceProps . ActiveConnectionPoolGroups +
239
+ long liveConnectionPoolGroups = SqlClientEventSourceProps . ActiveConnectionPoolGroups +
235
240
SqlClientEventSourceProps . InactiveConnectionPoolGroups ;
241
+ long acpg = SqlClientEventSourceProps . ActiveConnectionPoolGroups ;
236
242
PruneConnectionPoolGroups ( ) ;
237
243
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 ) ;
239
246
247
+ long icpg = SqlClientEventSourceProps . InactiveConnectionPoolGroups ;
240
248
//the 3rd call cleans the dangling connection pool groups
241
249
PruneConnectionPoolGroups ( ) ;
242
- Assert . Equal ( 0 , SqlClientEventSourceProps . InactiveConnectionPoolGroups ) ;
250
+ Assert . InRange ( SqlClientEventSourceProps . InactiveConnectionPoolGroups , 0 , icpg ) ;
243
251
}
244
252
245
253
private static void PruneConnectionPoolGroups ( )
0 commit comments