Beispiel #1
0
        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);
        }
Beispiel #2
0
        private static bool CancellationSequentialDistinct()
        {
            TestHarness.TestLog("* PlinqCancellationTests.CancellationSequentialDistinct()");
            IEnumerable <int>       src      = Enumerable.Repeat(0, int.MaxValue);
            CancellationTokenSource tokenSrc = new CancellationTokenSource();

            bool success = false;
            Task task    = Task.Factory.StartNew(
                () =>
            {
                try
                {
                    var q = src.AsParallel()
                            .WithCancellation(tokenSrc.Token)
                            .Distinct()
                            .TakeWhile(x => true);

                    foreach (var x in q)
                    {
                    }

                    TestHarness.TestLog("  > Failed: OperationCanceledException was not caught.");
                }
                catch (OperationCanceledException oce)
                {
                    if (OCEHelper.ExtractCT(oce) == tokenSrc.Token)
                    {
                        success = true;
                    }
                    else
                    {
                        TestHarness.TestLog("  > Failed: Wrong cancellation token.");
                    }
                }
            }
                );

            // We wait for 100 ms. If we canceled the token source immediately, the cancellation
            // would occur at the query opening time. The goal of this test is to test cancellation
            // at query execution time.
            Thread.Sleep(100);

            tokenSrc.Cancel();
            task.Wait();

            return(success);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
        }