From c526a473317049c6fbdc271e08728fcddb22ea7f Mon Sep 17 00:00:00 2001 From: Turnerj Date: Thu, 23 Jul 2020 19:28:43 +0930 Subject: [PATCH] More test tweaks to pass CI --- .../Parser/XmlSitemapParser.cs | 2 ++ .../TextSitemapParserTests.cs | 9 ++++----- .../XmlSitemapParserTests.cs | 18 ++++++------------ 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs index 0ad5f51..01748aa 100644 --- a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs +++ b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs @@ -45,6 +45,7 @@ public async Task ParseSitemapAsync(TextReader reader, Cancellation foreach (var urlNode in topNode.Elements()) { + cancellationToken.ThrowIfCancellationRequested(); var sitemapEntry = ParseSitemapEntry(urlNode); urls.Add(sitemapEntry); } @@ -57,6 +58,7 @@ public async Task ParseSitemapAsync(TextReader reader, Cancellation foreach (var sitemapNode in topNode.Elements()) { + cancellationToken.ThrowIfCancellationRequested(); var indexedSitemap = ParseSitemapIndex(sitemapNode); indexedSitemaps.Add(indexedSitemap); } diff --git a/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs b/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs index e0cee31..2ff24eb 100644 --- a/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs +++ b/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs @@ -36,15 +36,14 @@ public async Task ParseTextSitemapAsync() } [TestMethod] - public async Task ParseTextSitemapAsyncCancelation() + public async Task ParseTextSitemapAsync_Cancellation() { - var cts = new CancellationTokenSource(0); using (var reader = LoadResource("text-sitemap.txt")) { var parser = new TextSitemapParser(); - SitemapFile sitemapFile = null; - await Assert.ThrowsExceptionAsync(async () => sitemapFile = await parser.ParseSitemapAsync(reader, cts.Token)); - Assert.AreEqual(null, sitemapFile); + await Assert.ThrowsExceptionAsync( + async () => await parser.ParseSitemapAsync(reader, new CancellationToken(true)) + ); } } } diff --git a/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs b/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs index cdd8500..c07d701 100644 --- a/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs +++ b/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs @@ -12,7 +12,7 @@ namespace TurnerSoftware.SitemapTools.Tests public class XmlSitemapParserTests : TestBase { [TestMethod] - public async Task ChangeFrequenciesAreSetCorrectlyAsync() + public async Task ChangeFrequenciesAreSetCorrectly() { foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) { @@ -104,26 +104,20 @@ public async Task ParseSitemapFileAsync() } [TestMethod] - public async Task ParseSitemapFileAsyncCancelation() + public async Task ParseSitemapFileAsync_Cancellation() { - var cts = new CancellationTokenSource(0); using (var reader = LoadResource("basic-sitemap.xml")) { var parser = new XmlSitemapParser(); - SitemapFile sitemapFile = null; try { - sitemapFile = await parser.ParseSitemapAsync(reader, cts.Token); + await parser.ParseSitemapAsync(reader, new CancellationToken(true)); } - catch (TaskCanceledException ex) + catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException) { - Assert.ThrowsException(() => throw ex); + return; } - catch (OperationCanceledException ex) - { - Assert.ThrowsException(() => throw ex); - } - Assert.AreEqual(null, sitemapFile); + Assert.Fail("Expected exception not thrown"); } } }