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

Unit tests for Microsoft.Data.SqlClient.SqlClientEventSource #2

Merged
merged 14 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
Expand Up @@ -25,7 +25,7 @@ internal abstract partial class DbConnectionFactory
private static int _objectTypeCount; // EventSource counter
internal int ObjectID { get; } = Interlocked.Increment(ref _objectTypeCount);

// s_pendingOpenNonPooled is an array of tasks used to throttle creation of non-pooled connections to
// s_pendingOpenNonPooled is an array of tasks used to throttle creation of non-pooled connections to
// a maximum of Environment.ProcessorCount at a time.
private static uint s_pendingOpenNonPooledNext = 0;
private static Task<DbConnectionInternal>[] s_pendingOpenNonPooled = new Task<DbConnectionInternal>[Environment.ProcessorCount];
Expand Down Expand Up @@ -307,7 +307,7 @@ private void PruneConnectionPoolGroups(object state)
{
// when debugging this method, expect multiple threads at the same time
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}", ObjectID);

// First, walk the pool release list and attempt to clear each
// pool, when the pool is finally empty, we dispose of it. If the
// pool isn't empty, it's because there are active connections or
Expand Down Expand Up @@ -377,8 +377,8 @@ private void PruneConnectionPoolGroups(object state)
// move idle entries from last prune pass to a queue for pending release
// otherwise process entry which may move it from active to idle
if (entry.Value.Prune())
{ // may add entries to _poolsToRelease
SqlClientEventSource.Log.ExitActiveConnectionPoolGroup();
{
// may add entries to _poolsToRelease
QueuePoolGroupForRelease(entry.Value);
}
else
Expand Down Expand Up @@ -412,6 +412,7 @@ internal void QueuePoolForRelease(DbConnectionPool pool, bool clearing)
_poolsToRelease.Add(pool);
}
SqlClientEventSource.Log.EnterInactiveConnectionPool();
SqlClientEventSource.Log.ExitActiveConnectionPool();
}

internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
Expand All @@ -424,6 +425,7 @@ internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
_poolGroupsToRelease.Add(poolGroup);
}
SqlClientEventSource.Log.EnterInactiveConnectionPoolGroup();
SqlClientEventSource.Log.ExitActiveConnectionPoolGroup();
}

virtual protected DbConnectionInternal CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ internal int Clear()
if (pool != null)
{
DbConnectionFactory connectionFactory = pool.ConnectionFactory;
SqlClientEventSource.Log.ExitActiveConnectionPool();
connectionFactory.QueuePoolForRelease(pool, true);
}
}
Expand Down Expand Up @@ -196,8 +195,8 @@ internal DbConnectionPool GetConnectionPool(DbConnectionFactory connectionFactor
// else pool entry has been disabled so don't create new pools
Debug.Assert(PoolGroupStateDisabled == _state, "state should be disabled");

// don't need to call connectionFactory.QueuePoolForRelease(newPool) because
// pool callbacks were delayed and no risk of connections being created
// don't need to call connectionFactory.QueuePoolForRelease(newPool) because
// pool callbacks were delayed and no risk of connections being created
newPool.Shutdown();
}
}
Expand Down Expand Up @@ -264,9 +263,7 @@ internal bool Prune()
// pool into a list of pools to be released when they
// are completely empty.
DbConnectionFactory connectionFactory = pool.ConnectionFactory;

connectionFactory.QueuePoolForRelease(pool, false);
SqlClientEventSource.Log.ExitActiveConnectionPool();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void OpenConnection_WithAsyncTrue()
{
// Passes on NetFx
var asyncConnectionString = DataTestUtility.TCPConnectionString + ";async=true";
SqlConnection connection = new SqlConnection(asyncConnectionString);
using (SqlConnection connection = new SqlConnection(asyncConnectionString)){}
}

#region <<ExecuteCommand_WithNewConnection>>
Expand Down Expand Up @@ -60,16 +60,17 @@ private static bool DoesProcessExecutedAsync(IReadOnlyList<string> executedProce

private static async Task ExecuteCommandWithNewConnectionAsync(string processName, string cmdText, ICollection<string> executedProcessList)
{
var conn = new SqlConnection(DataTestUtility.TCPConnectionString);

await conn.OpenAsync();
var cmd = new SqlCommand(cmdText, conn);

using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
using (var conn = new SqlConnection(DataTestUtility.TCPConnectionString))
{
while (await reader.ReadAsync())
await conn.OpenAsync();
var cmd = new SqlCommand(cmdText, conn);

using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
executedProcessList.Add(processName);
while (await reader.ReadAsync())
{
executedProcessList.Add(processName);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
{
// Collect all traces for better code coverage
//// Collect all traces for better code coverage
EnableEvents(eventSource, EventLevel.Informational, EventKeywords.All);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public void SimpleFillTest()
public void PrepUnprepTest()
{
// share the connection
using (SqlCommand cmd = new SqlCommand("select * from shippers", new SqlConnection(DataTestUtility.TCPConnectionString)))
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand cmd = new SqlCommand("select * from shippers", connection))
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
{
cmd.Connection.Open();
Expand Down Expand Up @@ -183,7 +184,8 @@ public void SqlVariantTest()
ExecuteNonQueryCommand("CREATE TABLE " + tableName + " (c0_bigint bigint, c1_variant sql_variant)");

// good test for null values and unicode strings
using (SqlCommand cmd = new SqlCommand(null, new SqlConnection(DataTestUtility.TCPConnectionString)))
using (SqlConnection conn = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand cmd = new SqlCommand(null, conn))
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
{
cmd.Connection.Open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,41 @@ private static void RunDataTestForSingleConnString(string tcpConnectionString)
/// <param name="connectionString"></param>
private static void BasicConnectionPoolingTest(string connectionString)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
InternalConnectionWrapper internalConnection = new InternalConnectionWrapper(connection);
ConnectionPoolWrapper connectionPool = new ConnectionPoolWrapper(connection);
connection.Close();
InternalConnectionWrapper internalConnection;
ConnectionPoolWrapper connectionPool;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
internalConnection = new InternalConnectionWrapper(connection);
connectionPool = new ConnectionPoolWrapper(connection);
connection.Close();
}

SqlConnection connection2 = new SqlConnection(connectionString);
connection2.Open();
Assert.True(internalConnection.IsInternalConnectionOf(connection2), "New connection does not use same internal connection");
Assert.True(connectionPool.ContainsConnection(connection2), "New connection is in a different pool");
connection2.Close();
using (SqlConnection connection2 = new SqlConnection(connectionString))
{
connection2.Open();
Assert.True(internalConnection.IsInternalConnectionOf(connection2), "New connection does not use same internal connection");
Assert.True(connectionPool.ContainsConnection(connection2), "New connection is in a different pool");
connection2.Close();
}

SqlConnection connection3 = new SqlConnection(connectionString + ";App=SqlConnectionPoolUnitTest;");
connection3.Open();
Assert.False(internalConnection.IsInternalConnectionOf(connection3), "Connection with different connection string uses same internal connection");
Assert.False(connectionPool.ContainsConnection(connection3), "Connection with different connection string uses same connection pool");
connection3.Close();
using (SqlConnection connection3 = new SqlConnection(connectionString + ";App=SqlConnectionPoolUnitTest;"))
{
connection3.Open();
Assert.False(internalConnection.IsInternalConnectionOf(connection3), "Connection with different connection string uses same internal connection");
Assert.False(connectionPool.ContainsConnection(connection3), "Connection with different connection string uses same connection pool");
connection3.Close();
}

connectionPool.Cleanup();

SqlConnection connection4 = new SqlConnection(connectionString);
connection4.Open();
Assert.True(internalConnection.IsInternalConnectionOf(connection4), "New connection does not use same internal connection");
Assert.True(connectionPool.ContainsConnection(connection4), "New connection is in a different pool");
connection4.Close();
using (SqlConnection connection4 = new SqlConnection(connectionString))
{
connection4.Open();
Assert.True(internalConnection.IsInternalConnectionOf(connection4), "New connection does not use same internal connection");
Assert.True(connectionPool.ContainsConnection(connection4), "New connection is in a different pool");
connection4.Close();
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAADPasswordConnStrSetup), nameof(DataTestUtility.IsAADAuthorityURLSetup))]
Expand Down Expand Up @@ -154,18 +164,20 @@ private static void ClearAllPoolsTest(string connectionString)
SqlConnection.ClearAllPools();
Assert.True(0 == ConnectionPoolWrapper.AllConnectionPools().Length, "Pools exist after clearing all pools");

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
ConnectionPoolWrapper pool = new ConnectionPoolWrapper(connection);
connection.Close();
ConnectionPoolWrapper[] allPools = ConnectionPoolWrapper.AllConnectionPools();
DataTestUtility.AssertEqualsWithDescription(1, allPools.Length, "Incorrect number of pools exist.");
Assert.True(allPools[0].Equals(pool), "Saved pool is not in the list of all pools");
DataTestUtility.AssertEqualsWithDescription(1, pool.ConnectionCount, "Saved pool has incorrect number of connections");

SqlConnection.ClearAllPools();
Assert.True(0 == ConnectionPoolWrapper.AllConnectionPools().Length, "Pools exist after clearing all pools");
DataTestUtility.AssertEqualsWithDescription(0, pool.ConnectionCount, "Saved pool has incorrect number of connections.");
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
ConnectionPoolWrapper pool = new ConnectionPoolWrapper(connection);
connection.Close();
ConnectionPoolWrapper[] allPools = ConnectionPoolWrapper.AllConnectionPools();
DataTestUtility.AssertEqualsWithDescription(1, allPools.Length, "Incorrect number of pools exist.");
Assert.True(allPools[0].Equals(pool), "Saved pool is not in the list of all pools");
DataTestUtility.AssertEqualsWithDescription(1, pool.ConnectionCount, "Saved pool has incorrect number of connections");

SqlConnection.ClearAllPools();
Assert.True(0 == ConnectionPoolWrapper.AllConnectionPools().Length, "Pools exist after clearing all pools");
DataTestUtility.AssertEqualsWithDescription(0, pool.ConnectionCount, "Saved pool has incorrect number of connections.");
}
}

/// <summary>
Expand Down
Loading