public static bool PreCanceledToken_SimpleEnumerator() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.PreCanceledToken_SimpleEnumerator()"); OperationCanceledException caughtException = null; var cs = new CancellationTokenSource(); cs.Cancel(); int[] srcEnumerable = Enumerable.Range(0, 1000).ToArray(); ThrowOnFirstEnumerable <int> throwOnFirstEnumerable = new ThrowOnFirstEnumerable <int>(srcEnumerable); try { var query = throwOnFirstEnumerable .AsParallel() .WithCancellation(cs.Token); foreach (var item in query) { } } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "an OCE should be throw during query opening"); passed &= TestHarnessAssert.AreEqual(cs.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the cancellation token."); return(passed); }
// Plinq supresses OCE(externalCT) occuring in worker threads and then throws a single OCE(ct) // if a manual OCE(ct) is thrown but ct is not canceled, Plinq should not suppress it, else things // get confusing... // ONLY an OCE(ct) for ct.IsCancellationRequested=true is co-operative cancellation private static bool Bugfix632544_OnlySuppressOCEifCTCanceled() { bool passed = true; #if !PFX_LEGACY_3_5 TestHarness.TestLog("* PlinqCancellationTests.Bugfix632544_OnlySuppressOCEifCTCanceled()"); AggregateException caughtException = null; CancellationTokenSource cts = new CancellationTokenSource(); CancellationToken externalToken = cts.Token; try { Enumerable.Range(1, 10).AsParallel() .WithCancellation(externalToken) .Select( x => { if (x % 2 == 0) { throw new OperationCanceledException(externalToken); } return(x); } ) .ToArray(); } catch (AggregateException ae) { caughtException = ae; } passed &= TestHarnessAssert.IsNotNull(caughtException, "We expect this OCE(ct) to merely be aggregated."); #endif return(passed); }
private static bool MultiplesWithCancellationIsIllegal() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.MultiplesWithCancellationIsIllegal()"); InvalidOperationException caughtException = null; try { CancellationTokenSource cs = new CancellationTokenSource(); CancellationToken ct = cs.Token; var query = Enumerable.Range(1, 10).AsParallel().WithDegreeOfParallelism(2).WithDegreeOfParallelism(2); query.ToArray(); } catch (InvalidOperationException ex) { caughtException = ex; //Console.WriteLine("IOE caught. message = " + ex.Message); } passed &= TestHarnessAssert.IsNotNull(caughtException, "An exception should be thrown."); return(passed); }
private static bool CancellationTokenTest_NonSorting_SynchronousMergerEnumeratorDispose() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.CancellationTokenTest_NonSorting_SynchronousMergerEnumeratorDispose()"); int size = 10000; CancellationTokenSource tokenSource = new CancellationTokenSource(); Exception caughtException = null; var query = Enumerable.Range(1, size).AsParallel() .WithCancellation(tokenSource.Token) .Select( i => { Thread.Sleep(100); return(i); }).WithMergeOptions(ParallelMergeOptions.FullyBuffered); IEnumerator <int> enumerator = query.GetEnumerator(); ThreadPool.QueueUserWorkItem( (arg) => { Thread.Sleep(1000); enumerator.Dispose(); }); try { // This query should run for at least a few seconds due to the sleeps in the select-delegate for (int j = 0; j < 1000; j++) { enumerator.MoveNext(); } } catch (ObjectDisposedException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "An ObjectDisposedException should be thrown"); return(passed); }
private static bool CancellationTokenTest_Sorting_ToArray() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.CancellationTokenTest_Sorting_ToArray()"); int size = 10000; CancellationTokenSource tokenSource = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem( (arg) => { Thread.Sleep(500); tokenSource.Cancel(); }); OperationCanceledException caughtException = null; try { // This query should run for at least a few seconds due to the sleeps in the select-delegate var query = Enumerable.Range(1, size).AsParallel() .WithCancellation(tokenSource.Token) .Select( i => { Thread.Sleep(1); return(i); }); query.ToArray(); } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "An OCE should be thrown"); passed &= TestHarnessAssert.AreEqual(tokenSource.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the external token."); return(passed); }
private static bool CancellationTokenTest_NonSorting_ToArray_ExternalCancel() { bool passed = true; TestHarness.TestLog("* PlinqCancellationTests.CancellationTokenTest_NonSorting_ToArray_ExternalCancel()"); int size = 10000; CancellationTokenSource tokenSource = new CancellationTokenSource(); OperationCanceledException caughtException = null; ThreadPool.QueueUserWorkItem( (arg) => { Thread.Sleep(1000); tokenSource.Cancel(); }); try { int[] output = Enumerable.Range(1, size).AsParallel() .WithCancellation(tokenSource.Token) .Select( i => { Thread.Sleep(100); return(i); }).ToArray(); } catch (OperationCanceledException ex) { caughtException = ex; } passed &= TestHarnessAssert.IsNotNull(caughtException, "An ObjectDisposedException should be thrown"); passed &= TestHarnessAssert.AreEqual(tokenSource.Token, OCEHelper.ExtractCT(caughtException), "The OCE should reference the external cancellation token."); return(passed); }