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 bug in ExecuteXmlReaderAsync implementation #782

Merged
merged 1 commit into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1779,7 +1779,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
try
{
success = true;
return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader)));
return CompleteXmlReader(InternalEndExecuteReader(asyncResult, false, nameof(EndExecuteXmlReader)), true);
}
catch (Exception e)
{
Expand Down
Expand Up @@ -2265,7 +2265,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
int? sqlExceptionNumber = null;
try
{
XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false));
XmlReader result = CompleteXmlReader(InternalEndExecuteReader(asyncResult, ADP.EndExecuteXmlReader, isInternal: false), true);
success = true;
return result;
}
Expand Down Expand Up @@ -2299,7 +2299,7 @@ private XmlReader EndExecuteXmlReaderInternal(IAsyncResult asyncResult)
}
}

private XmlReader CompleteXmlReader(SqlDataReader ds)
private XmlReader CompleteXmlReader(SqlDataReader ds, bool async = false)
{
XmlReader xr = null;

Expand All @@ -2313,7 +2313,7 @@ private XmlReader CompleteXmlReader(SqlDataReader ds)
try
{
SqlStream sqlBuf = new SqlStream(ds, true /*addByteOrderMark*/, (md[0].SqlDbType == SqlDbType.Xml) ? false : true /*process all rows*/);
xr = sqlBuf.ToXmlReader();
xr = sqlBuf.ToXmlReader(async);
}
catch (Exception e)
{
Expand Down
Expand Up @@ -4,22 +4,23 @@

using System;
using System.Xml;
using System.Xml.Linq;
using Xunit;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class XmlReaderAsyncTest
{
private static string commandText =
private const string CommandText =
"SELECT * from dbo.Customers FOR XML AUTO, XMLDATA;";

// Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void ExecuteTest()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand command = new SqlCommand(CommandText, connection))
{
SqlCommand command = new SqlCommand(commandText, connection);
connection.Open();

IAsyncResult result = command.BeginExecuteXmlReader();
Expand All @@ -28,10 +29,13 @@ public static void ExecuteTest()
System.Threading.Thread.Sleep(100);
}

XmlReader reader = command.EndExecuteXmlReader(result);

reader.ReadToDescendant("dbo.Customers");
Assert.Equal("ALFKI", reader["CustomerID"]);
using (XmlReader xmlReader = command.EndExecuteXmlReader(result))
{
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
Assert.True(xmlReader.Settings.Async);
xmlReader.ReadToDescendant("dbo.Customers");
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
}
}
}

Expand All @@ -40,8 +44,8 @@ public static void ExecuteTest()
public static void ExceptionTest()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand command = new SqlCommand(CommandText, connection))
{
SqlCommand command = new SqlCommand(commandText, connection);
connection.Open();

//Try to execute a synchronous query on same command
Expand All @@ -55,10 +59,39 @@ public static void ExceptionTest()
System.Threading.Thread.Sleep(100);
}

XmlReader reader = command.EndExecuteXmlReader(result);
using (XmlReader xmlReader = command.EndExecuteXmlReader(result))
{
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
Assert.True(xmlReader.Settings.Async);
xmlReader.ReadToDescendant("dbo.Customers");
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
}
}
}

// Synapse: Parse error at line: 1, column: 29: Incorrect syntax near 'FOR'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static async void MoveToContentAsyncTest()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (SqlCommand command = new SqlCommand(CommandText, connection))
{
connection.Open();

reader.ReadToDescendant("dbo.Customers");
Assert.Equal("ALFKI", reader["CustomerID"]);
using (XmlReader xmlReader = await command.ExecuteXmlReaderAsync().ConfigureAwait(false))
{
try
{
// Issue #781: Test failed here as xmlReader.Settings.Async was set to false
Assert.True(xmlReader.Settings.Async);
xmlReader.ReadToDescendant("dbo.Customers");
Assert.Equal("ALFKI", xmlReader["CustomerID"]);
}
catch (Exception ex)
{
Assert.False(true, "Exception occurred: " + ex.Message);
}
}
}
}
}
Expand Down