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

Port connection pool tests from devdiv #1155

Merged
merged 17 commits into from
Sep 20, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,17 @@ public static void ExecuteTest()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
try
{
SqlCommand command = new SqlCommand(GenerateCommandText(), connection);
connection.Open();

IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
System.Threading.Thread.Sleep(100);
}
Assert.True(command.EndExecuteNonQuery(result) > 0, "FAILED: BeginExecuteNonQuery did not complete successfully.");
}
catch (SqlException ex)
{
Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
Assert.Null(ex);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Error: {0}", ex.Message);
Assert.Null(ex);
}
catch (Exception ex)

using SqlCommand command = new SqlCommand(GenerateCommandText(), connection);
connection.Open();

IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
// You might want to pass these errors
// back out to the caller.
Console.WriteLine("Error: {0}", ex.Message);
Assert.Null(ex);
System.Threading.Thread.Sleep(100);
}

Assert.True(command.EndExecuteNonQuery(result) > 0, "FAILED: BeginExecuteNonQuery did not complete successfully.");
}
}

Expand All @@ -74,24 +56,12 @@ public static void FailureTest()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
bool caughtException = false;
SqlCommand command = new SqlCommand(GenerateCommandText(), connection);
connection.Open();

//Try to execute a synchronous query on same command
IAsyncResult result = command.BeginExecuteNonQuery();
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Assert.True(ex is InvalidOperationException, "FAILED: Thrown exception for BeginExecuteNonQuery was not an InvalidOperationException");
caughtException = true;
}

Assert.True(caughtException, "FAILED: No exception thrown after trying second BeginExecuteNonQuery.");
caughtException = false;
InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => command.ExecuteNonQuery());

while (!result.IsCompleted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ public class InternalConnectionWrapper
private object _internalConnection = null;
private object _spid = null;

/// <summary>
/// Is this internal connection enlisted in a distributed transaction?
/// </summary>
public bool IsEnlistedInTransaction
{ get { return ConnectionHelper.IsEnlistedInTransaction(_internalConnection); } }

/// <summary>
/// Is this internal connection the root of a distributed transaction?
/// </summary>
public bool IsTransactionRoot
{ get { return ConnectionHelper.IsTransactionRoot(_internalConnection); } }

/// <summary>
/// True if this connection is the root of a transaction AND it is waiting for the transaction
/// to complete (i.e. it has been 'aged' or 'put into stasis'), otherwise false
/// </summary>
public bool IsTxRootWaitingForTxEnd
{ get { return ConnectionHelper.IsTxRootWaitingForTxEnd(_internalConnection); } }

/// <summary>
/// Gets the internal connection associated with the given SqlConnection
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ internal static class ConnectionHelper
private static PropertyInfo s_pendingSQLDNS_AddrIPv4 = s_SQLDNSInfo.GetProperty("AddrIPv4", BindingFlags.Instance | BindingFlags.Public);
private static PropertyInfo s_pendingSQLDNS_AddrIPv6 = s_SQLDNSInfo.GetProperty("AddrIPv6", BindingFlags.Instance | BindingFlags.Public);
private static PropertyInfo s_pendingSQLDNS_Port = s_SQLDNSInfo.GetProperty("Port", BindingFlags.Instance | BindingFlags.Public);
private static PropertyInfo dbConnectionInternalIsTransRoot = s_dbConnectionInternal.GetProperty("IsTransactionRoot", BindingFlags.Instance | BindingFlags.NonPublic);
private static PropertyInfo dbConnectionInternalEnlistedTrans = s_sqlInternalConnection.GetProperty("EnlistedTransaction", BindingFlags.Instance | BindingFlags.NonPublic);
private static PropertyInfo dbConnectionInternalIsTxRootWaitingForTxEnd = s_dbConnectionInternal.GetProperty("IsTxRootWaitingForTxEnd", BindingFlags.Instance | BindingFlags.NonPublic);

public static object GetConnectionPool(object internalConnection)
{
Expand Down Expand Up @@ -69,6 +72,24 @@ private static void VerifyObjectIsConnection(object connection)
throw new ArgumentException("Object provided was not a SqlConnection", nameof(connection));
}

public static bool IsEnlistedInTransaction(object internalConnection)
{
VerifyObjectIsInternalConnection(internalConnection);
return (dbConnectionInternalEnlistedTrans.GetValue(internalConnection, null) != null);
}

public static bool IsTransactionRoot(object internalConnection)
{
VerifyObjectIsInternalConnection(internalConnection);
return (bool)dbConnectionInternalIsTransRoot.GetValue(internalConnection, null);
}

public static bool IsTxRootWaitingForTxEnd(object internalConnection)
{
VerifyObjectIsInternalConnection(internalConnection);
return (bool)dbConnectionInternalIsTxRootWaitingForTxEnd.GetValue(internalConnection, null);
}

public static object GetParser(object internalConnection)
{
VerifyObjectIsInternalConnection(internalConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static void VerifyObjectIsTdsParser(object parser)
if (parser == null)
throw new ArgumentNullException("stateObject");
if (!s_tdsParser.IsInstanceOfType(parser))
throw new ArgumentException("Object provided was not a DbConnectionInternal", "internalConnection");
throw new ArgumentException("Object provided was not a TdsParser", nameof(parser));
}

internal static object GetStateObject(object parser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void VerifyObjectIsTdsParserStateObject(object stateObject)
if (stateObject == null)
throw new ArgumentNullException(nameof(stateObject));
if (!s_tdsParserStateObjectManaged.IsInstanceOfType(stateObject))
throw new ArgumentException("Object provided was not a DbConnectionInternal", "internalConnection");
throw new ArgumentException("Object provided was not a TdsParserStateObjectManaged", nameof(stateObject));
}

internal static object GetSessionHandle(object stateObject)
Expand Down
Loading