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 }); }
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)); }