示例#1
0
        public void CancellationWorks()
        {
            var timer = new Stopwatch();

            timer.Start();

            // Run SAPS.
            var sapsRunner = new SapsRunner(new Dictionary <string, IAllele>(), SapsRunnerTests.PathToExecutable, TimeSpan.FromMilliseconds(30000));
            var runner     = sapsRunner.Run(
                new InstanceSeedFile(SapsRunnerTests.PathToTestInstance, SapsRunnerTests.TestInstanceSeed),
                this._cancellationTokenSource.Token);

            // Cancel task and expect it to be cancelled.
            try
            {
                Thread.Sleep(100);
                this._cancellationTokenSource.Cancel();
                runner.Wait();
                Assert.True(false, "Expected a task cancelled exception.");
            }
            catch (AggregateException aggregateException)
            {
                timer.Stop();

                aggregateException.InnerExceptions.Count.ShouldBe(1);
                var innerException = aggregateException.InnerExceptions.Single();
                innerException.ShouldBeOfType <TaskCanceledException>();

                runner.IsCanceled.ShouldBeTrue();
                timer.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(1));
            }
            catch (Exception)
            {
                Assert.True(false, "Expected a task cancelled exception.");
            }
        }