Ejemplo n.º 1
0
        public void TestWithCancellation()
        {
            var cts = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromMilliseconds(25));

            Assert.ThrowsAny <OperationCanceledException>(
                () =>
            {
                try
                {
                    var provider = new MakoProvider();
                    var items    = provider
                                   .GetNews(cts.Token)
                                   .Result
                                   .ToList();
                }
                catch (AggregateException e)
                {
                    List <OperationCanceledException> canceledExceptions = e.InnerExceptions
                                                                           .Cast <OperationCanceledException>()
                                                                           .ToList();
                    if (canceledExceptions.Any())
                    {
                        throw canceledExceptions.First();
                    }
                }
            });
        }