Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace ReqIFSharp.Extensions.Tests.ReqIFExtensions
{
using System;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -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<OperationCanceledException>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public static IEnumerable<ExternalObject> QueryExternalObjects(this SpecElementW
/// <param name="reqIfLoaderService">
/// The <see cref="IReqIFLoaderService"/> that is used to query the payload from the associated reqifz file-stream
/// </param>
/// <param name="token">
/// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
/// </param>
/// <returns>
/// an <see cref="IEnumerable{String}"/> that contains base64 encoded strings
/// </returns>
public static Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64PayloadsAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService)
public static Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64PayloadsAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService, CancellationToken token = default)
{
if (specElementWithAttributes == null)
{
Expand All @@ -83,7 +86,7 @@ public static Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64Payloa
throw new ArgumentNullException(nameof(reqIfLoaderService));
}

return QueryBase64PayloadsInternalAsync(specElementWithAttributes, reqIfLoaderService);
return QueryBase64PayloadsInternalAsync(specElementWithAttributes, reqIfLoaderService, token);
}

/// <summary>
Expand All @@ -95,20 +98,21 @@ public static Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64Payloa
/// <param name="reqIfLoaderService">
/// The <see cref="IReqIFLoaderService"/> that is used to query the payload from the associated reqifz file-stream
/// </param>
/// <param name="token">
/// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
/// </param>
/// <returns>
/// an <see cref="IEnumerable{String}"/> that contains base64 encoded strings
/// </returns>
private static async Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64PayloadsInternalAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService)
private static async Task<IEnumerable<Tuple<ExternalObject, string>>> QueryBase64PayloadsInternalAsync(this SpecElementWithAttributes specElementWithAttributes, IReqIFLoaderService reqIfLoaderService, CancellationToken token)
{
var result = new List<Tuple<ExternalObject, string>>();

using var cts = new CancellationTokenSource();

foreach (var specObjectValue in specElementWithAttributes.Values.OfType<AttributeValueXHTML>())
{
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, string>(externalObject, payload));
}
}
Expand Down
Loading