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

Fix stored procedure issues introduced in v1.1.2 #544

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
Expand Up @@ -2946,11 +2946,11 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
}
}

// _attentionSent set by 'SendAttention'
// HasReceivedAttention set above
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._attentionSent && !stateObj.HasPendingData && stateObj.HasOpenResult)
if (!stateObj.HasReceivedAttention && !stateObj.HasPendingData && stateObj.HasOpenResult)
{
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3337,11 +3337,11 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
}
}

// _attentionSent set by 'SendAttention'
// _attentionReceived set above
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._attentionSent && !stateObj._pendingData && stateObj._hasOpenResult)
if (!stateObj._attentionReceived && !stateObj._pendingData && stateObj._hasOpenResult)
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
{
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,106 @@ public static void AsyncCancelDoesNotWaitNP()
AsyncCancelDoesNotWait(np_connStr).Wait();
}

[CheckConnStrSetupFact]
public static void TCPAttentionPacketTestTransaction()
{
CancelFollowedByTransaction(tcp_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void NPAttentionPacketTestTransaction()
{
CancelFollowedByTransaction(np_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void TCPAttentionPacketTestAlerts()
{
CancelFollowedByAlert(tcp_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void NPAttentionPacketTestAlerts()
{
CancelFollowedByAlert(np_connStr);
}

private static void CancelFollowedByTransaction(string constr)
{
using (SqlConnection connection = new SqlConnection(constr))
{
connection.Open();
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = @"SELECT @@VERSION";
using (var r = cmd.ExecuteReader())
{
cmd.Cancel();
}
}
using (SqlTransaction transaction = connection.BeginTransaction())
{ }
}
}

private static void CancelFollowedByAlert(string constr)
{
var alertName = "myAlert" + Guid.NewGuid().ToString();
// Since Alert conditions are randomly generated,
// we will retry on unexpected error messages to avoid collision in pipelines.
var n = new Random().Next(1, 100);
bool retry = true;
int retryAttempt = 0;
while (retry && retryAttempt < 3)
{
try
{
using (var conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT @@VERSION";
using (var reader = cmd.ExecuteReader())
{
cmd.Cancel(); // Sends Attention
}
}
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = $@"EXEC msdb.dbo.sp_add_alert @name=N'{alertName}',
@performance_condition = N'SQLServer:General Statistics|User Connections||>|{n}'";
cmd.ExecuteNonQuery();
cmd.CommandText = @"USE [msdb]";
cmd.ExecuteNonQuery();
cmd.CommandText = $@"/****** Object: Alert [{alertName}] Script Date: {DateTime.Now} ******/
IF EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'{alertName}')
EXEC msdb.dbo.sp_delete_alert @name=N'{alertName}'";
cmd.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
if (retryAttempt >= 3 || e.Message.Contains("The transaction operation cannot be performed"))
{
Assert.False(true, $"Retry Attempt: {retryAttempt} | Unexpected Exception occurred: {e.Message}");
}
else
{
retry = true;
retryAttempt++;
Console.WriteLine($"CancelFollowedByAlert Test retry attempt : {retryAttempt}");
Thread.Sleep(500);
continue;
}
}
retry = false;
}
}

private static void MultiThreadedCancel(string constr, bool async)
{
using (SqlConnection con = new SqlConnection(constr))
Expand Down