Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
+ address comments
  • Loading branch information
Davoud Eshtehari committed Apr 14, 2021
1 parent 98ea1a7 commit 1de2d74
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3563,7 +3563,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)

return true;
}
catch (System.OutOfMemoryException e)
catch (OutOfMemoryException e)
{
_isClosed = true;
SqlConnection con = _connection;
Expand All @@ -3573,7 +3573,7 @@ private bool TryReadInternal(bool setTimeout, out bool more)
}
throw;
}
catch (System.StackOverflowException e)
catch (StackOverflowException e)
{
_isClosed = true;
SqlConnection con = _connection;
Expand All @@ -3583,6 +3583,9 @@ private bool TryReadInternal(bool setTimeout, out bool more)
}
throw;
}
/* Even though ThreadAbortException exists in .NET Core,
* since Abort is not supported, the common language runtime won't ever throw ThreadAbortException.
* just to keep a common codebase!*/
catch (System.Threading.ThreadAbortException e)
{
_isClosed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,11 @@ bool isDelegateControlRequest
stateObj = _parser.GetSession(this);
mustPutSession = true;
}
else if (internalTransaction.OpenResultsCount != 0)
if (internalTransaction.OpenResultsCount != 0)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlInternalConnectionTds.ExecuteTransactionYukon|DATA|CATCH> {0}, Connection is going to doom cause of having OpenResultsCount {1} > 0.",
ObjectID,
internalTransaction.OpenResultsCount);
throw SQL.CannotCompleteDelegatedTransactionWithOpenResults(this, _parser.MARSOn);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,11 @@ internal void ExecuteTransactionYukon(
stateObj = _parser.GetSession(this);
mustPutSession = true;
}
else if (internalTransaction.OpenResultsCount != 0)
if (internalTransaction.OpenResultsCount != 0)
{
SqlClientEventSource.Log.TryTraceEvent("<sc.SqlInternalConnectionTds.ExecuteTransactionYukon|DATA|CATCH> {0}, Connection is going to doom cause of having OpenResultsCount {1} > 0.",
ObjectID,
internalTransaction.OpenResultsCount);
throw SQL.CannotCompleteDelegatedTransactionWithOpenResults(this, _parser.MARSOn);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,87 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class TransactionTest
{
public static TheoryData<string> ConnectionStrings =>
new TheoryData<string>
{
new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString)
{
Pooling = false,
MultipleActiveResultSets = false
}.ConnectionString
, new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString)
{
MultipleActiveResultSets = false,
Pooling = true,
MaxPoolSize = 1
}.ConnectionString
, new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString)
{
MultipleActiveResultSets = true
}.ConnectionString
};

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[MemberData(nameof(ConnectionStrings))]
public static void ReadNextQueryAfterTxAborted(string connString)
{
using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
{
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand("SELECT 1", sqlConnection);
sqlConnection.Open();
var reader = cmd.ExecuteReader();
scope.Dispose();
}

using (SqlConnection sqlConnection = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand("SELECT 2", sqlConnection))
{
sqlConnection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
bool result = reader.Read();
Assert.True(result);
if (result)
{
Assert.Equal(2, reader.GetValue(0));
}
}
}

using (SqlConnection sqlConnection = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand("SELECT 3", sqlConnection))
{
sqlConnection.Open();
using (SqlDataReader reader = cmd.ExecuteReaderAsync().Result)
{
bool result = reader.ReadAsync().Result;
Assert.True(result);
if (result)
{
Assert.Equal(3, reader.GetValue(0));
}
}
}

using (SqlConnection sqlConnection = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand("SELECT TOP(1) 4 Clm0 FROM sysobjects FOR XML AUTO", sqlConnection))
{
sqlConnection.Open();
using (System.Xml.XmlReader reader = cmd.ExecuteXmlReader())
{
bool result = reader.Read();
Assert.True(result);
if (result)
{
Assert.Equal("4", reader[0]);
}
}
}
}
}

// Synapse: Enforced unique constraints are not supported. To create an unenforced unique constraint you must include the NOT ENFORCED syntax as part of your statement.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void TestMain()
Expand Down

0 comments on commit 1de2d74

Please sign in to comment.