diff options
author | Petr Onderka <gsvick@gmail.com> | 2017-12-13 20:37:53 +0300 |
---|---|---|
committer | Paulo Janotti <pauloja@microsoft.com> | 2017-12-13 20:37:53 +0300 |
commit | 62d596476e4681110065439e2d442a773dd895a5 (patch) | |
tree | c0929cab0cebc4ce4b92170c0e8c938a329089e8 /src/System.Private.Xml | |
parent | a5cc3069882296b2f8586a56cf35fe053e1a9928 (diff) |
XmlReader.Read shouldn't throw AggregateException (#25681)
* XmlReader.Read shouldn't throw AggregateException
* Remove unnecessary uses of Task.Result
* Address feedback
* Test is skipped on .Net Framework
Diffstat (limited to 'src/System.Private.Xml')
3 files changed, 21 insertions, 16 deletions
diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs index b0a9d93ec5..d87ca813ea 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @@ -9,6 +9,7 @@ using System.Diagnostics; using System.Globalization; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace System.Xml { @@ -624,9 +625,8 @@ namespace System.Xml { //this will be hit when user create a XmlReader by setting Async, but the first call is Read() instead of ReadAsync(), //then we still should create an async stream here. And wait for the method finish. - System.Threading.Tasks.Task<object> t = _laterInitParam.inputUriResolver.GetEntityAsync(_laterInitParam.inputbaseUri, string.Empty, typeof(Stream)); - t.Wait(); - stream = (Stream)t.Result; + Task<object> t = _laterInitParam.inputUriResolver.GetEntityAsync(_laterInitParam.inputbaseUri, string.Empty, typeof(Stream)); + stream = (Stream)t.GetAwaiter().GetResult(); } else { diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs index 923c36b885..c27937f055 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs @@ -367,8 +367,8 @@ namespace System.Xml private async Task<int> ReadContentAsBase64_AsyncHelper(Task<bool> task, byte[] buffer, int index, int count) { - await task.ConfigureAwait(false); - if (!task.Result) + bool result = await task.ConfigureAwait(false); + if (!result) { return 0; } @@ -514,8 +514,8 @@ namespace System.Xml private async Task<int> ReadElementContentAsBase64Async_Helper(Task<bool> task, byte[] buffer, int index, int count) { - await task.ConfigureAwait(false); - if (!task.Result) + bool result = await task.ConfigureAwait(false); + if (!result) { return 0; } diff --git a/src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs b/src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs index f73ac94562..eec4e8db94 100644 --- a/src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs +++ b/src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs @@ -39,10 +39,10 @@ namespace System.Xml.Tests } } - [Fact] - public static void ReadAfterInitializationWithStreamOnAsyncReaderDoesNotThrow() + [Theory, InlineData(true), InlineData(false)] + public static void ReadAfterInitializationWithStreamOnAsyncReaderDoesNotThrow(bool async) { - using (XmlReader reader = XmlReader.Create(GetDummyXmlStream(), new XmlReaderSettings() { Async = true })) + using (XmlReader reader = XmlReader.Create(GetDummyXmlStream(), new XmlReaderSettings() { Async = async })) { reader.Read(); } @@ -57,10 +57,10 @@ namespace System.Xml.Tests } } - [Fact] - public static void ReadAfterInitializationWithTextReaderOnAsyncReaderDoesNotThrow() + [Theory, InlineData(true), InlineData(false)] + public static void ReadAfterInitializationWithTextReaderOnAsyncReaderDoesNotThrow(bool async) { - using (XmlReader reader = XmlReader.Create(GetDummyXmlTextReader(), new XmlReaderSettings() { Async = true })) + using (XmlReader reader = XmlReader.Create(GetDummyXmlTextReader(), new XmlReaderSettings() { Async = async })) { reader.Read(); } @@ -75,14 +75,19 @@ namespace System.Xml.Tests } } - [Fact] + [Fact, SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".Net Framework throws AggregateException")] public static void ReadAfterInitializationWithUriOnAsyncReaderTrows() { using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true })) { - AggregateException ae = Assert.Throws<System.AggregateException>(() => reader.Read()); - Assert.Equal(typeof(System.Net.WebException), ae.InnerException.GetType()); + Assert.Throws<System.Net.WebException>(() => reader.Read()); } } + + [Fact] + public static void InitializationWithUriOnNonAsyncReaderTrows() + { + Assert.Throws<System.Net.WebException>(() => XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = false })); + } } } |