From b2695418676b27a16a64f54e5da469d340b1566a Mon Sep 17 00:00:00 2001 From: samatstarion Date: Fri, 15 May 2026 16:43:40 +0200 Subject: [PATCH] [Fix] honor CancellationToken in QueryBase64PayloadsAsync; fixes #67 --- ...cElementWithAttributesExtensionTestFixture.cs | 14 ++++++++++++++ .../SpecElementWithAttributesExtensions.cs | 16 ++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ReqIFSharp.Extensions.Tests/ReqIFExtensions/SpecElementWithAttributesExtensionTestFixture.cs b/ReqIFSharp.Extensions.Tests/ReqIFExtensions/SpecElementWithAttributesExtensionTestFixture.cs index b254a8c..677134f 100644 --- a/ReqIFSharp.Extensions.Tests/ReqIFExtensions/SpecElementWithAttributesExtensionTestFixture.cs +++ b/ReqIFSharp.Extensions.Tests/ReqIFExtensions/SpecElementWithAttributesExtensionTestFixture.cs @@ -20,6 +20,7 @@ namespace ReqIFSharp.Extensions.Tests.ReqIFExtensions { + using System; using System.IO; using System.Linq; using System.Threading; @@ -80,5 +81,18 @@ public async Task Verify_that_QueryBase64Payloads_returns_expected_results() Assert.That(base64Payload.Item2, Does.StartWith("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfgAAACfCAIAAACazFx+AAAAAXNSR0IArs4c6QAA")); } + + [Test] + public void Verify_that_QueryBase64PayloadsAsync_honors_cancellation() + { + var specObject = this.reqIf.CoreContent.SpecObjects.Single(x => x.Identifier == "_3.4.2.2.2_BrLeft_2_BrRight_._BrLeft_f_BrRight_1"); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + + Assert.That( + async () => await specObject.QueryBase64PayloadsAsync(this.reqIFLoaderService, cts.Token), + Throws.InstanceOf()); + } } } diff --git a/ReqIFSharp.Extensions/ReqIFExtensions/SpecElementWithAttributesExtensions.cs b/ReqIFSharp.Extensions/ReqIFExtensions/SpecElementWithAttributesExtensions.cs index 268b634..14f327a 100644 --- a/ReqIFSharp.Extensions/ReqIFExtensions/SpecElementWithAttributesExtensions.cs +++ b/ReqIFSharp.Extensions/ReqIFExtensions/SpecElementWithAttributesExtensions.cs @@ -68,10 +68,13 @@ public static IEnumerable QueryExternalObjects(this SpecElementW /// /// The that is used to query the payload from the associated reqifz file-stream /// + /// + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// /// /// an that contains base64 encoded strings /// - public static Task>> QueryBase64PayloadsAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService) + public static Task>> QueryBase64PayloadsAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService, CancellationToken token = default) { if (specElementWithAttributes == null) { @@ -83,7 +86,7 @@ public static Task>> QueryBase64Payloa throw new ArgumentNullException(nameof(reqIfLoaderService)); } - return QueryBase64PayloadsInternalAsync(specElementWithAttributes, reqIfLoaderService); + return QueryBase64PayloadsInternalAsync(specElementWithAttributes, reqIfLoaderService, token); } /// @@ -95,20 +98,21 @@ public static Task>> QueryBase64Payloa /// /// The that is used to query the payload from the associated reqifz file-stream /// + /// + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// /// /// an that contains base64 encoded strings /// - private static async Task>> QueryBase64PayloadsInternalAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService) + private static async Task>> QueryBase64PayloadsInternalAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService, CancellationToken token) { var result = new List>(); - using var cts = new CancellationTokenSource(); - foreach (var specObjectValue in specElementWithAttributes.Values.OfType()) { foreach (var externalObject in specObjectValue.ExternalObjects) { - var payload = await reqIfLoaderService.QueryDataAsync(externalObject, cts.Token); + var payload = await reqIfLoaderService.QueryDataAsync(externalObject, token); result.Add(new Tuple(externalObject, payload)); } }