Skip to content

Commit

Permalink
Fix | Fix issue with SqlCommand OnStatementCompleted event not being …
Browse files Browse the repository at this point in the history
…triggered (dotnet#216)
  • Loading branch information
cheenamalhotra committed Sep 26, 2019
1 parent a9279ac commit 3ed8f60
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,11 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
cmd.InternalRecordsAffected = count;
}
}
// Skip the bogus DONE counts sent by the server
if (stateObj._receivedColMetaData || (curCmd != TdsEnums.SELECT))
{
cmd.OnStatementCompleted(count);
}
}

stateObj._receivedColMetaData = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@
<Compile Include="DDBasics\DDMARSTest\DDMARSTest.cs" />
<Compile Include="ProviderAgnostic\MultipleResultsTest\MultipleResultsTest.cs" />
<Compile Include="ProviderAgnostic\ReaderTest\ReaderTest.cs" />
<Compile Include="SQL\AsyncTest\AsyncTest.cs" />
<Compile Include="SQL\CommandCancelTest\CommandCancelTest.cs" />
<Compile Include="SQL\AsyncTest\AsyncTest.cs" />
<Compile Include="SQL\SqlCommand\SqlCommandCompletedTest.cs" />
<Compile Include="SQL\SqlCommand\SqlCommandCancelTest.cs" />
<Compile Include="SQL\ConnectionPoolTest\ConnectionPoolTest.cs" />
<Compile Include="SQL\ConnectivityTests\ConnectivityTest.cs" />
<Compile Include="SQL\DataBaseSchemaTest\ConnectionSchemaTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class CommandCancelTest
public static class SqlCommandCancelTest
{
// Shrink the packet size - this should make timeouts more likely
private static readonly string s_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { PacketSize = 512 }).ConnectionString;
Expand Down Expand Up @@ -139,7 +139,7 @@ private static void MultiThreadedCancel(string constr, bool async)

Task.WaitAll(tasks, 15 * 1000);

CommandCancelTest.VerifyConnection(command);
SqlCommandCancelTest.VerifyConnection(command);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Data;
using Xunit;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class SqlCommandCompletedTest
{
private static readonly string s_connStr = (new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr) { PacketSize = 512 }).ConnectionString;
private static int completedHandlerExecuted = 0;

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void VerifyStatmentCompletedCalled()
{
string tableName = DataTestUtility.GetUniqueNameForSqlServer("stmt");

using (var conn = new SqlConnection(s_connStr))
using (var cmd = conn.CreateCommand())
{
try
{
cmd.StatementCompleted += StatementCompletedHandler;
conn.Open();

cmd.CommandText = $"CREATE TABLE {tableName} (c1 int)";
var res = cmd.ExecuteScalar();

cmd.CommandText = $"INSERT {tableName} VALUES(1)"; //DML (+1)
res = cmd.ExecuteScalar();

cmd.CommandText = $"Update {tableName} set c1=2"; //DML (+1)
res = cmd.ExecuteScalar();

cmd.CommandText = $"SELECT * from {tableName}"; //DQL (+1)
res = cmd.ExecuteScalar();

cmd.CommandText = $"DELETE FROM {tableName}"; //DML (+1)
res = cmd.ExecuteScalar();
}
finally
{
cmd.CommandText = $"DROP TABLE {tableName}";
var res = cmd.ExecuteScalar();
}
}
// DDL and DQL queries that return DoneRowCount are accounted here.
Assert.True(completedHandlerExecuted == 4);
}

private static void StatementCompletedHandler(object sender, StatementCompletedEventArgs args)
{
// Increment on event pass through
completedHandlerExecuted++;
}
}
}

0 comments on commit 3ed8f60

Please sign in to comment.