diff --git a/Sdk/Speckle.Connectors.Common.Tests/CancellationManagerTests.cs b/Sdk/Speckle.Connectors.Common.Tests/CancellationManagerTests.cs index cd9de3a95..06aa34bd1 100644 --- a/Sdk/Speckle.Connectors.Common.Tests/CancellationManagerTests.cs +++ b/Sdk/Speckle.Connectors.Common.Tests/CancellationManagerTests.cs @@ -24,9 +24,9 @@ public class CancellationManagerTests item.Dispose(); manager.IsExist(id).Should().BeFalse(); - Assert.Throws(() => item.Token.IsCancellationRequested.Should().BeTrue()); + item.Token.IsCancellationRequested.Should().BeTrue(); - Assert.Throws(() => manager.IsCancellationRequested(id).Should().BeTrue()); + manager.IsCancellationRequested(id).Should().BeTrue(); } [Test] @@ -49,9 +49,9 @@ public class CancellationManagerTests item1.Dispose(); manager.IsExist(id1).Should().BeFalse(); - Assert.Throws(() => item1.Token.IsCancellationRequested.Should().BeTrue()); + item1.Token.IsCancellationRequested.Should().BeTrue(); - Assert.Throws(() => manager.IsCancellationRequested(id1).Should().BeTrue()); + manager.IsCancellationRequested(id1).Should().BeTrue(); manager.NumberOfOperations.Should().Be(1); manager.IsExist(id2).Should().BeTrue(); @@ -62,9 +62,9 @@ public class CancellationManagerTests item2.Dispose(); manager.IsExist(id2).Should().BeFalse(); - Assert.Throws(() => item2.Token.IsCancellationRequested.Should().BeTrue()); + item2.Token.IsCancellationRequested.Should().BeTrue(); - Assert.Throws(() => manager.IsCancellationRequested(id2).Should().BeTrue()); + manager.IsCancellationRequested(id2).Should().BeTrue(); } [Test] @@ -92,5 +92,18 @@ public class CancellationManagerTests manager.IsExist(id1).Should().BeFalse(); manager.IsExist(id2).Should().BeFalse(); + + item1.Token.IsCancellationRequested.Should().BeTrue(); + item2.Token.IsCancellationRequested.Should().BeTrue(); + } + + [Test] + public void Cancel_No_Existing() + { + var manager = new CancellationManager(); + manager.NumberOfOperations.Should().Be(0); + var x = Guid.NewGuid().ToString(); + manager.IsCancellationRequested(x).Should().BeTrue(); + manager.IsExist(x).Should().BeFalse(); } } diff --git a/Sdk/Speckle.Connectors.Common/Cancellation/CancellationManager.cs b/Sdk/Speckle.Connectors.Common/Cancellation/CancellationManager.cs index e46ede195..79f1d0fbc 100644 --- a/Sdk/Speckle.Connectors.Common/Cancellation/CancellationManager.cs +++ b/Sdk/Speckle.Connectors.Common/Cancellation/CancellationManager.cs @@ -27,18 +27,10 @@ public class CancellationManager : ICancellationManager public int NumberOfOperations => _operationsInProgress.Count; - /// - /// Get token with registered id. - /// - /// Id of the operation. - /// CancellationToken that belongs to operation. - public CancellationToken GetToken(string id) => _operationsInProgress[id].Token; + //if we can't find it then it must be cancelled + private CancellationToken GetToken(string id) => + _operationsInProgress.TryGetValue(id, out var source) ? source.Token : new CancellationToken(true); - /// - /// Whether given id registered or not. - /// - /// Id to check registration. - /// Whether given id registered or not. public bool IsExist(string id) => _operationsInProgress.ContainsKey(id); public void CancelAllOperations() @@ -91,5 +83,5 @@ public class CancellationManager : ICancellationManager /// /// Id to check cancellation requested already or not. /// - public bool IsCancellationRequested(string id) => _operationsInProgress[id].IsCancellationRequested; + public bool IsCancellationRequested(string id) => GetToken(id).IsCancellationRequested; } diff --git a/Sdk/Speckle.Connectors.Common/Operations/ReceiveOperation.cs b/Sdk/Speckle.Connectors.Common/Operations/ReceiveOperation.cs index 9d2fd00ae..fe7a3cd03 100644 --- a/Sdk/Speckle.Connectors.Common/Operations/ReceiveOperation.cs +++ b/Sdk/Speckle.Connectors.Common/Operations/ReceiveOperation.cs @@ -26,6 +26,7 @@ public sealed class ReceiveOperation( ) { using var execute = activityFactory.Start("Receive Operation"); + cancellationToken.ThrowIfCancellationRequested(); execute?.SetTag("receiveInfo", receiveInfo); // 2 - Check account exist Account account = accountService.GetAccountWithServerUrlFallback(receiveInfo.AccountId, receiveInfo.ServerUrl); @@ -34,6 +35,7 @@ public sealed class ReceiveOperation( var version = await apiClient.Version.Get(receiveInfo.SelectedVersionId, receiveInfo.ProjectId, cancellationToken); + cancellationToken.ThrowIfCancellationRequested(); var commitObject = await threadContext.RunOnWorkerAsync( () => ReceiveData(account, version, receiveInfo, onOperationProgressed, cancellationToken) ); @@ -47,6 +49,7 @@ public sealed class ReceiveOperation( ) .ConfigureAwait(false); + cancellationToken.ThrowIfCancellationRequested(); await apiClient.Version.Received( new(version.id, receiveInfo.ProjectId, receiveInfo.SourceApplication), cancellationToken diff --git a/Sdk/Speckle.Connectors.Common/Operations/SendOperation.cs b/Sdk/Speckle.Connectors.Common/Operations/SendOperation.cs index 8751efc24..840e29da7 100644 --- a/Sdk/Speckle.Connectors.Common/Operations/SendOperation.cs +++ b/Sdk/Speckle.Connectors.Common/Operations/SendOperation.cs @@ -31,8 +31,10 @@ public sealed class SendOperation( CancellationToken ct = default ) { + ct.ThrowIfCancellationRequested(); var buildResult = await rootObjectBuilder.Build(objects, sendInfo, onOperationProgressed, ct); + ct.ThrowIfCancellationRequested(); // POC: Jonathon asks on behalf of willow twin - let's explore how this can work // buildResult.RootObject["@report"] = new Report { ConversionResults = buildResult.ConversionResults }; @@ -42,6 +44,7 @@ public sealed class SendOperation( var (rootObjId, convertedReferences) = await threadContext.RunOnWorkerAsync( () => Send(buildResult.RootObject, sendInfo, onOperationProgressed, ct) ); + ct.ThrowIfCancellationRequested(); return new(rootObjId, convertedReferences, buildResult.ConversionResults); }