Skip to content

Commit

Permalink
Fix intermittent timed out exception (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavoudEshtehari authored Jan 14, 2022
1 parent d9efa74 commit d74556b
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public void EventCounter_StasisCounters_Functional()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public void EventCounter_ReclaimedConnectionsCounter_Functional()
{
SqlConnection.ClearAllPools();
// clean pools and pool groups
ClearConnectionPools();
var stringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) { Pooling = true, MaxPoolSize = 1 };

long rc = SqlClientEventSourceProps.ReclaimedConnections;
Expand All @@ -158,6 +159,7 @@ public void EventCounter_ReclaimedConnectionsCounter_Functional()
// Specifying the generation number makes it to run faster by avoiding a full GC process
GC.Collect(gcNumber);
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(200); // give the pooler some time to reclaim the connection and avoid the conflict.

using (SqlConnection conn = new SqlConnection(stringBuilder.ToString()))
{
Expand Down Expand Up @@ -220,26 +222,32 @@ private static InternalConnectionWrapper CreateEmancipatedConnection(string conn
private void ClearConnectionPools()
{
//ClearAllPoos kills all the existing pooled connection thus deactivating all the active pools
var liveConnectionPools = SqlClientEventSourceProps.ActiveConnectionPools +
long liveConnectionPools = SqlClientEventSourceProps.ActiveConnectionPools +
SqlClientEventSourceProps.InactiveConnectionPools;
SqlConnection.ClearAllPools();
Assert.InRange(SqlClientEventSourceProps.InactiveConnectionPools, 0, liveConnectionPools);
Assert.Equal(0, SqlClientEventSourceProps.ActiveConnectionPools);

//the 1st PruneConnectionPoolGroups call cleans the dangling inactive connection pools
long icp = SqlClientEventSourceProps.InactiveConnectionPools;

// The 1st PruneConnectionPoolGroups call cleans the dangling inactive connection pools.
PruneConnectionPoolGroups();
Assert.Equal(0, SqlClientEventSourceProps.InactiveConnectionPools);
// If the pool isn't empty, it's because there are active connections or distributed transactions that need it.
Assert.InRange(SqlClientEventSourceProps.InactiveConnectionPools, 0, icp);

//the 2nd call deactivates the dangling connection pool groups
var liveConnectionPoolGroups = SqlClientEventSourceProps.ActiveConnectionPoolGroups +
long liveConnectionPoolGroups = SqlClientEventSourceProps.ActiveConnectionPoolGroups +
SqlClientEventSourceProps.InactiveConnectionPoolGroups;
long acpg = SqlClientEventSourceProps.ActiveConnectionPoolGroups;
PruneConnectionPoolGroups();
Assert.InRange(SqlClientEventSourceProps.InactiveConnectionPoolGroups, 0, liveConnectionPoolGroups);
Assert.Equal(0, SqlClientEventSourceProps.ActiveConnectionPoolGroups);
// If the pool entry isn't empty, it's because there are active pools that need it.
Assert.InRange(SqlClientEventSourceProps.ActiveConnectionPoolGroups, 0, acpg);

long icpg = SqlClientEventSourceProps.InactiveConnectionPoolGroups;
//the 3rd call cleans the dangling connection pool groups
PruneConnectionPoolGroups();
Assert.Equal(0, SqlClientEventSourceProps.InactiveConnectionPoolGroups);
Assert.InRange(SqlClientEventSourceProps.InactiveConnectionPoolGroups, 0, icpg);
}

private static void PruneConnectionPoolGroups()
Expand Down

0 comments on commit d74556b

Please sign in to comment.