コード例 #1
0
        public async Task TaskRunnerBase_Throws_PartitionKillerException()
        {
            var knownKillerExceptions = new List <Exception>()
            {
                new OutOfMemoryException()
            };

            foreach (var exception in knownKillerExceptions)
            {
                var sut = new TaskRunnerActual((ct =>
                {
                    return(new List <TaskRunnerInformationBase>()
                    {
                        GetTaskRunnerInformation <object>(() => { throw(exception); }, null, null, null, ct)
                    });
                }));

                var task = Task.Run(() => sut.OuterRun(CancellationToken.None));

                var e = await Assert.ThrowsAsync <AggregateException>(async() =>
                {
                    await task.ConfigureAwait(false);
                });

                Assert.True(e.InnerException.GetType() == exception.GetType(),
                            $"Expected exception to be {exception.GetType()}, was {task.Exception.InnerException.GetType()} instead");
            }
        }
コード例 #2
0
        public async Task TaskRunnerBase_Cancels_AfterCancellationRequested()
        {
            var counter = 0;

            var sut = new TaskRunnerActual((ct =>
            {
                return(new List <TaskRunnerInformationBase>()
                {
                    GetTaskRunnerInformation <object>(async() => { counter++; await Task.Delay(Timeout.Infinite); }, null, null, null, ct)
                });
            }));

            var source = new CancellationTokenSource();
            var task   = Task.Run(() => sut.OuterRun(source.Token));

            await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

            source.Cancel();

            Assert.True(counter == 1, $"Expected thread to only run once, ran {counter} times instead");
            Assert.True(sut.InitialCancellationToken.IsCancellationRequested == true, $"Expected cancellation token passed into SUT to be cancelled");
            Assert.True(sut.LinkedCancellationToken.IsCancellationRequested == true, $"Expected linked cancellation token created in SUT to be cancelled");

            await task.AwaitCompleteOrCancel();
        }