public static void PreCanceledToken_SimpleEnumerator()
        {
            OperationCanceledException caughtException = null;
            var cs = new CancellationTokenSource();

            cs.Cancel();

            IEnumerable <int> throwOnFirstEnumerable = Enumerables <int> .ThrowOnEnumeration();

            try
            {
                var query = throwOnFirstEnumerable
                            .AsParallel()
                            .WithCancellation(cs.Token);

                foreach (var item in query)
                {
                }
            }
            catch (OperationCanceledException ex)
            {
                caughtException = ex;
            }

            Assert.NotNull(caughtException);
            Assert.Equal(cs.Token, caughtException.CancellationToken);
        }
Esempio n. 2
0
 /// <summary>
 ///For each count, return an Enumerable source that fails (throws an exception) on that count, with each buffering option.
 /// </summary>
 /// <param name="counts">The sizes of ranges to return.</param>
 /// <returns>Entries for test data.
 /// The first element is the Labeled{ParallelQuery{int}} range,
 /// the second element is the count, and the third is the ParallelMergeOption to use.</returns>
 public static IEnumerable <object[]> ThrowOnCount_AllMergeOptions_MemberData(int[] counts)
 {
     foreach (int count in counts)
     {
         var labeled = Labeled.Label("ThrowOnEnumeration " + count, Enumerables <int> .ThrowOnEnumeration(count).AsParallel().AsOrdered());
         foreach (ParallelMergeOptions option in Options)
         {
             yield return(new object[] { labeled, count, option });
         }
     }
 }
        public static void PreCanceledToken_ForAll()
        {
            OperationCanceledException caughtException = null;
            var cs = new CancellationTokenSource();

            cs.Cancel();

            IEnumerable <int> throwOnFirstEnumerable = Enumerables <int> .ThrowOnEnumeration();

            try
            {
                throwOnFirstEnumerable
                .AsParallel()
                .WithCancellation(cs.Token)
                .ForAll((x) => { Debug.WriteLine(x.ToString()); });
            }
            catch (OperationCanceledException ex)
            {
                caughtException = ex;
            }

            Assert.NotNull(caughtException);
            Assert.Equal(cs.Token, caughtException.CancellationToken);
        }
 // Return an enumerable which throws on first MoveNext.
 // Useful for testing promptness of cancellation.
 public static IEnumerable <object[]> ThrowOnFirstEnumeration()
 {
     yield return(new object[] { Labeled.Label("ThrowOnFirstEnumeration", Enumerables <int> .ThrowOnEnumeration().AsParallel()), 8 });
 }
Esempio n. 5
0
        public static void AlreadyCanceled(Action <ParallelQuery <int> > query)
        {
            ParallelQuery <int> s = Enumerables <int> .ThrowOnEnumeration(new ShouldNotBeInvokedException(), 2).AsParallel().WithCancellation(new CancellationToken(canceled: true));

            OperationCanceledException oce = Assert.Throws <OperationCanceledException>(() => query(s));
        }