Skip to content

Commit

Permalink
Fix bug in Async ExecuteXmlReader implementation
Browse files Browse the repository at this point in the history
Fixes #781
  • Loading branch information
cheenamalhotra committed Nov 3, 2020
1 parent ab9fbb7 commit 34d6810
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
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

0 comments on commit 34d6810

Please sign in to comment.