Пример #1
0
        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);
        }
Пример #2
0
        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());
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
 public CommandTaskCancellationEventArgs(ICommandTask task, CommandCanceledException cancelReason)
     : base(task)
 {
     this.cancelReason = cancelReason;
 }
Пример #6
0
        private void OnCanceled(CommandCanceledException cancelException)
        {
            var handler = Canceled;

            if (handler != null) {
                handler(this, new CommandTaskCancellationEventArgs(this, cancelException));
            }
        }
Пример #7
0
        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);
        }