public void CancellationFiresCanceled() { var runner = new CommandRunner(); var cancelException = new CommandCanceledException(); CommandCanceledException firedReason = null; int fireCount = 0; runner.TaskCanceled += (sender, e) => { ++fireCount; firedReason = e.CancelReason; }; var mockCommand = GetCommandMock(); mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>())) .Returns((TypedData) null) .Callback(() => { throw cancelException; }); var task = runner.Run(mockCommand.Object); task.WaitHandle.WaitOne(); Assert.AreEqual(1, fireCount); AssertCanceledExceptionsSame(cancelException, firedReason); }
public async Task ExecuteCancelableCommand_ThrowsWrappedException() { using (CancellationTokenSource cts = new()) { CancellationCommand command = new(); cts.Cancel(); CommandCanceledException exception = await Assert.ThrowsAsync <CommandCanceledException>(() => CommandExecutor.InvokeAsync(command, cts.Token)); OperationCanceledException inner = Assert.IsType <OperationCanceledException>(exception.InnerException); Assert.Equal(cts.Token, inner.CancellationToken); } using (CancellationTokenSource cts = new()) { CancelCommand command = new(); cts.Cancel(); Task <CommandResult> task = CommandExecutor.InvokeAsync(command, cts.Token); CommandCanceledException exception = await Assert.ThrowsAsync <CommandCanceledException>(() => task); TaskCanceledException inner = Assert.IsType <TaskCanceledException>(exception.InnerException); Assert.Equal(cts.Token, inner.CancellationToken); Assert.NotSame(task, inner.Task); Assert.Equal(TaskStatus.Faulted, task.Status); Assert.Same(exception, task.Exception.InnerExceptions.Single()); } }
public void CancellationSetsCancelReason() { var runner = new CommandRunner(); var cancelException = new CommandCanceledException(); var mockCommand = GetCommandMock(); mockCommand.Setup((command) => command.Process(null, It.Is<IMutableProgressTracker>((mpt) => mpt != null), It.IsAny<CancellationToken>())) .Returns((TypedData) null) .Callback(() => { throw cancelException; }); var task = runner.Run(mockCommand.Object); task.WaitHandle.WaitOne(); AssertCanceledExceptionsSame(cancelException, task.CancelReason); }
private static void AssertCanceledExceptionsSame(CommandCanceledException expected, Exception actual) { if (expected == null && actual == null) { return; } Assert.IsNotNull(expected); Assert.IsNotNull(actual); while (actual.InnerException != null) { actual = actual.InnerException; } Assert.AreSame(expected, actual); }
public CommandTaskCancellationEventArgs(ICommandTask task, CommandCanceledException cancelReason) : base(task) { this.cancelReason = cancelReason; }
private void OnCanceled(CommandCanceledException cancelException) { var handler = Canceled; if (handler != null) { handler(this, new CommandTaskCancellationEventArgs(this, cancelException)); } }
private void HandleCancellation(Exception e) { State = TaskState.Canceled; var reason = CommandCanceledException.Wrap(e, this.command); lock (this.syncRoot) { this.cancelReason = reason; } this.progressTracker.Status = reason.Message; OnCanceled(reason); }