Exemple #1
0
        // throwing a fake OCE(ct) when the ct isn't canceled should produce an AggregateException.
        private static bool Bugfix640886_SetOperationsThrowAggregateOnCancelOrDispose_2()
        {
            bool passed = true;

            TestHarness.TestLog("* PlinqCancellationTests.Bugfix640886_SetOperationsThrowAggregateOnCancelOrDispose_2()");

            try
            {
                CancellationTokenSource cs = new CancellationTokenSource();
                var plinq = Enumerable.Range(0, 50)
                            .AsParallel().WithCancellation(cs.Token)
                            .WithDegreeOfParallelism(1)
#if PFX_LEGACY_3_5
                            .Union(Enumerable.Range(0, 10).AsParallel().Select <int, int>(x => { throw new OperationCanceledException(); }));
#else
                            .Union(Enumerable.Range(0, 10).AsParallel().Select <int, int>(x => { throw new OperationCanceledException(cs.Token); }));
#endif


                var walker = plinq.GetEnumerator();
                while (walker.MoveNext())
                {
                    Thread.Sleep(1);
                }
                walker.MoveNext();
                passed &= TestHarnessAssert.Fail("AggregateException was expected, but no exception occured.");
            }
Exemple #2
0
        // REPRO 1 -- cancellation
        private static bool Bugfix640886_SetOperationsThrowAggregateOnCancelOrDispose_1()
        {
            bool passed = true;

            TestHarness.TestLog("* PlinqCancellationTests.Bugfix640886_SetOperationsThrowAggregateOnCancelOrDispose_1()");

            var mre       = new ManualResetEvent(false);
            var plinq_src =
                Enumerable.Range(0, 5000000).Select(x =>
            {
                if (x == 0)
                {
                    mre.Set();
                }
                return(x);
            });

            Task t = null;

            try
            {
                CancellationTokenSource cs = new CancellationTokenSource();
                var plinq = plinq_src
                            .AsParallel().WithCancellation(cs.Token)
                            .WithDegreeOfParallelism(1)
                            .Union(Enumerable.Range(0, 10).AsParallel());

                var walker = plinq.GetEnumerator();

                t = Task.Factory.StartNew(() =>
                {
                    mre.WaitOne();
                    cs.Cancel();
                });
                while (walker.MoveNext())
                {
                    Thread.Sleep(1);
                    var item = walker.Current;
                }
                walker.MoveNext();
                passed &= TestHarnessAssert.Fail("OperationCanceledException was expected, but no exception occured.");
            }
            catch (OperationCanceledException)
            {
                //This is expected.
            }

            catch (Exception e)
            {
                passed &= TestHarnessAssert.Fail("OperationCanceledException was expected, but a different exception occured.");
                TestHarness.TestLog(e.ToString());
            }

            if (t != null)
            {
                t.Wait();
            }


            return(passed);
        }