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); }
/// <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(object[] counts) { foreach (int count in counts.Cast <int>()) { 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) => { Console.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 }); }