Add SqlCommand Begin and EndExecuteXmlReader functions (dotnet/corefx#26252)
authorJoel Braun <Joelbraun@outlook.com>
Wed, 24 Jan 2018 20:27:41 +0000 (12:27 -0800)
committerSaurabh Singh <saurabh500@users.noreply.github.com>
Wed, 24 Jan 2018 20:27:41 +0000 (12:27 -0800)
* Add SqlCommand Begin and EndExecuteXmlReader functions

* Updated tests to remove try/catch and use Assert.Throws

Commit migrated from https://github.com/dotnet/corefx/commit/767696781f2a1803c5bb003a27bd0bc885817f6f

src/libraries/System.Data.SqlClient/ref/System.Data.SqlClient.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlCommand.cs
src/libraries/System.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs [new file with mode: 0644]
src/libraries/System.Data.SqlClient/tests/ManualTests/System.Data.SqlClient.ManualTesting.Tests.csproj

index f84f25c..0bd9e6e 100644 (file)
@@ -416,6 +416,9 @@ namespace System.Data.SqlClient
         public System.Xml.XmlReader ExecuteXmlReader() { throw null; }
         public System.Threading.Tasks.Task<System.Xml.XmlReader> ExecuteXmlReaderAsync() { throw null; }
         public System.Threading.Tasks.Task<System.Xml.XmlReader> ExecuteXmlReaderAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
+        public System.IAsyncResult BeginExecuteXmlReader() { throw null; }
+        public System.IAsyncResult BeginExecuteXmlReader(System.AsyncCallback callback, object stateObject) { throw null; }
+        public System.Xml.XmlReader EndExecuteXmlReader(System.IAsyncResult asyncResult) { throw null; }
         public override void Prepare() { }
         public System.Data.Sql.SqlNotificationRequest Notification { get { throw null; } set { } }
         public void ResetCommandTimeout() { }
index 38e0b56..170c612 100644 (file)
@@ -1223,7 +1223,12 @@ namespace System.Data.SqlClient
         }
 
 
-        private IAsyncResult BeginExecuteXmlReader(AsyncCallback callback, object stateObject)
+        public IAsyncResult BeginExecuteXmlReader()
+        {
+            return BeginExecuteXmlReader(null, null);
+        }
+
+        public IAsyncResult BeginExecuteXmlReader(AsyncCallback callback, object stateObject)
         {
             // Reset _pendingCancel upon entry into any Execute - used to synchronize state
             // between entry into Execute* API and the thread obtaining the stateObject.
@@ -1302,7 +1307,7 @@ namespace System.Data.SqlClient
         }
 
 
-        private XmlReader EndExecuteXmlReader(IAsyncResult asyncResult)
+        public XmlReader EndExecuteXmlReader(IAsyncResult asyncResult)
         {
             Exception asyncException = ((Task)asyncResult).Exception;
             if (asyncException != null)
diff --git a/src/libraries/System.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs b/src/libraries/System.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/XmlReaderAsyncTest.cs
new file mode 100644 (file)
index 0000000..14cd523
--- /dev/null
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml;
+using Xunit;
+
+namespace System.Data.SqlClient.ManualTesting.Tests
+{
+    public static class XmlReaderAsyncTest
+    {
+        private static string commandText =
+            "SELECT * from dbo.Customers FOR XML AUTO, XMLDATA;";
+
+        [CheckConnStrSetupFact]
+        public static void ExecuteTest()
+        {
+            using (SqlConnection connection = new SqlConnection(DataTestUtility.TcpConnStr))
+            {
+                SqlCommand command = new SqlCommand(commandText, connection);
+                connection.Open();
+
+                IAsyncResult result = command.BeginExecuteXmlReader();
+                while (!result.IsCompleted)
+                {
+                    System.Threading.Thread.Sleep(100);
+                }
+
+                XmlReader reader = command.EndExecuteXmlReader(result);
+
+                reader.ReadToDescendant("dbo.Customers");
+                Assert.Equal("ALFKI", reader["CustomerID"]);
+            }
+        }
+
+        [CheckConnStrSetupFact]
+        public static void ExceptionTest()
+        {
+            using (SqlConnection connection = new SqlConnection(DataTestUtility.TcpConnStr))
+            {
+                SqlCommand command = new SqlCommand(commandText, connection);
+                connection.Open();
+
+                //Try to execute a synchronous query on same command
+                IAsyncResult result = command.BeginExecuteXmlReader();
+
+                Assert.Throws<InvalidOperationException>( delegate { command.ExecuteXmlReader(); });
+
+                while (!result.IsCompleted)
+                {
+                    System.Threading.Thread.Sleep(100);
+                }
+
+                XmlReader reader = command.EndExecuteXmlReader(result);
+
+                reader.ReadToDescendant("dbo.Customers");
+                Assert.Equal("ALFKI", reader["CustomerID"]);
+            }
+        }
+    }
+}
index 7daecb1..6ca023e 100644 (file)
@@ -13,6 +13,7 @@
     <Compile Include="DataCommon\CheckConnStrSetupFactAttribute.cs" />
     <Compile Include="SQL\AdapterTest\AdapterTest.cs" />
     <Compile Include="SQL\AsyncTest\BeginExecAsyncTest.cs" />
+    <Compile Include="SQL\AsyncTest\XmlReaderAsyncTest.cs" />
     <Compile Include="SQL\Common\AsyncDebugScope.cs" />
     <Compile Include="SQL\Common\ConnectionPoolWrapper.cs" />
     <Compile Include="SQL\Common\InternalConnectionWrapper.cs" />