Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #5

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Reflection;
using System.Transactions;
Expand Down Expand Up @@ -146,6 +146,69 @@ public void EventCounter_StasisCounters_Functional()
Assert.Equal(0, SqlClientEventSourceProps.StasisConnections);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public void EventCounter_ReclaimedConnectionsCounter_Functional()
{
SqlConnection.ClearAllPools();
var stringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) { Pooling = true, MaxPoolSize = 1};

long rc = SqlClientEventSourceProps.ReclaimedConnections;

InternalConnectionWrapper internalConnection = CreateEmancipatedConnection(stringBuilder.ToString());

GC.Collect();
GC.WaitForPendingFinalizers();

using (SqlConnection conn = new SqlConnection(stringBuilder.ToString()))
{
conn.Open();

// when calling open, the connection is reclaimed.
Assert.Equal(rc + 1, SqlClientEventSourceProps.ReclaimedConnections);
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public void EventCounter_ConnectionPoolGroupsCounter_Functional()
{
SqlConnection.ClearAllPools();

var stringBuilder = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) { Pooling = true};

long acpg = SqlClientEventSourceProps.ActiveConnectionPoolGroups;
long iacpg = SqlClientEventSourceProps.InactiveConnectionPoolGroups;

using (SqlConnection conn = new SqlConnection(stringBuilder.ToString())) {
conn.Open();

// when calling open, we have 1 more active connection pool group
Assert.Equal(acpg + 1, SqlClientEventSourceProps.ActiveConnectionPoolGroups);

conn.Close();
}

SqlConnection.ClearAllPools();

// poolGroup state is changed from Active to Idle
PruneConnectionPoolGroups();

// poolGroup state is changed from Idle to Disabled
PruneConnectionPoolGroups();
Assert.Equal(acpg, SqlClientEventSourceProps.ActiveConnectionPoolGroups);
Assert.Equal(iacpg + 1, SqlClientEventSourceProps.InactiveConnectionPoolGroups);

// Remove poolGroup from poolGroupsToRelease list
PruneConnectionPoolGroups();
Assert.Equal(iacpg, SqlClientEventSourceProps.ActiveConnectionPoolGroups);
}

private static InternalConnectionWrapper CreateEmancipatedConnection(string connectionString)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
return new InternalConnectionWrapper(connection);
}

private void ClearConnectionPools()
{
//ClearAllPoos kills all the existing pooled connection thus deactivating all the active pools
Expand Down Expand Up @@ -208,6 +271,7 @@ internal static class SqlClientEventSourceProps
private static readonly FieldInfo _activeConnectionsCounter;
private static readonly FieldInfo _freeConnectionsCounter;
private static readonly FieldInfo _stasisConnectionsCounter;
private static readonly FieldInfo _reclaimedConnectionsCounter;

static SqlClientEventSourceProps()
{
Expand Down Expand Up @@ -264,6 +328,9 @@ static SqlClientEventSourceProps()
_stasisConnectionsCounter =
sqlClientEventSourceType.GetField(nameof(_stasisConnectionsCounter), _bindingFlags);
Debug.Assert(_stasisConnectionsCounter != null);
_reclaimedConnectionsCounter =
sqlClientEventSourceType.GetField(nameof(_reclaimedConnectionsCounter), _bindingFlags);
Debug.Assert(_reclaimedConnectionsCounter != null);
}

public static long ActiveHardConnections => (long)_activeHardConnectionsCounter.GetValue(_log)!;
Expand Down Expand Up @@ -295,5 +362,7 @@ static SqlClientEventSourceProps()
public static long FreeConnections => (long)_freeConnectionsCounter.GetValue(_log)!;

public static long StasisConnections => (long)_stasisConnectionsCounter.GetValue(_log)!;

public static long ReclaimedConnections => (long)_reclaimedConnectionsCounter.GetValue(_log)!;
}
}