Exemple #1
0
        public static bool CancelAfterWait()
        {
            TestHarness.TestLog("* BarrierCancellationTests.CancelAfterWait()");
            bool passed = true;

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            const int numberParticipants = 3;
            Barrier   barrier            = new Barrier(numberParticipants);

            ThreadPool.QueueUserWorkItem(
                (args) =>
            {
                Thread.Sleep(1000);
                cancellationTokenSource.Cancel();
            }
                );

            //Now wait.. the wait should abort and an exception should be thrown
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => barrier.SignalAndWait(cancellationToken),
                cancellationToken, "An OCE(null) should have been thrown that references the cancellationToken.");

            //Test that backout occured.
            passed &= TestHarnessAssert.AreEqual(numberParticipants, barrier.ParticipantsRemaining,
                                                 "All participants should remain as the current one should have backout out its signal");

            // the token should not have any listeners.
            // currently we don't expose this.. but it was verified manually

            return(passed);
        }
        public static bool CancelAfterWait()
        {
            TestHarness.TestLog("* CountdownEventCancellationTests.CancelAfterWait()");
            bool passed = true;

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken       cancellationToken       = cancellationTokenSource.Token;

            CountdownEvent countdownEvent = new CountdownEvent(2);;   // countdownEvent that will block all waiters

            ThreadPool.QueueUserWorkItem(
                (args) =>
            {
                Thread.Sleep(1000);
                cancellationTokenSource.Cancel();
            }
                );

            //Now wait.. the wait should abort and an exception should be thrown
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => countdownEvent.Wait(cancellationToken),
                cancellationToken, "An OCE(null) should have been thrown that references the cancellationToken.");

            // the token should not have any listeners.
            // currently we don't expose this.. but it was verified manually

            return(passed);
        }
Exemple #3
0
        public static bool ExternalCancel_GetConsumingEnumerable()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.ExternalCancel_GetConsumingEnumerable()");
            bool passed = true;

            BlockingCollection <int> bc = new BlockingCollection <int>();
            CancellationTokenSource  cs = new CancellationTokenSource();

            ThreadPool.QueueUserWorkItem(
                (obj) =>
            {
                Thread.Sleep(100);
                cs.Cancel();
            });

            IEnumerable <int> enumerable = bc.GetConsumingEnumerable(cs.Token);

            passed &= TestHarnessAssert.IsFalse(cs.IsCancellationRequested, "At this point the cancel should not have occurred.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => enumerable.GetEnumerator().MoveNext(),
                cs.Token,
                "The operation should wake up via token cancellation.");

            return(passed);
        }
Exemple #4
0
        public static bool ExternalCancel_TryAddToAny()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.ExternalCancel_AddToAny()");
            bool passed = true;

            BlockingCollection <int> bc1 = new BlockingCollection <int>(1);
            BlockingCollection <int> bc2 = new BlockingCollection <int>(1);

            bc1.Add(1); //fill the bc.
            bc2.Add(1); //fill the bc.

            CancellationTokenSource cs = new CancellationTokenSource();

            ThreadPool.QueueUserWorkItem(
                (obj) =>
            {
                Thread.Sleep(100);
                cs.Cancel();
            });

            passed &= TestHarnessAssert.IsFalse(cs.IsCancellationRequested, "At this point the cancel should not have occurred.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => BlockingCollection <int> .TryAddToAny(new[] { bc1, bc2 }, 1, 10000, cs.Token),
                cs.Token,
                "The operation should wake up via token cancellation.");

            return(passed);
        }
Exemple #5
0
        public static bool ExternalCancel_TryTake()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.ExternalCancel_TryTake()");
            bool passed = true;

            BlockingCollection <int> bc = new BlockingCollection <int>(); //empty collection.

            CancellationTokenSource cs = new CancellationTokenSource();

            ThreadPool.QueueUserWorkItem(
                (obj) =>
            {
                Thread.Sleep(100);
                cs.Cancel();
            });

            int item;

            passed &= TestHarnessAssert.IsFalse(cs.IsCancellationRequested, "At this point the cancel should not have occurred.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(
                () => bc.TryTake(out item, 100000, cs.Token),
                cs.Token,
                "The operation should wake up via token cancellation.");

            return(passed);
        }
Exemple #6
0
        public static bool CancelBeforeWait()
        {
            TestHarness.TestLog("* ManualResetEventCancellationTests.CancelBeforeWait()");
            bool passed = true;

            ManualResetEventSlim    mres = new ManualResetEventSlim();
            CancellationTokenSource cs   = new CancellationTokenSource();

            cs.Cancel();
            CancellationToken ct = cs.Token;

            const int millisec = 100;
            TimeSpan  timeSpan = new TimeSpan(100);

            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => mres.Wait(ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => mres.Wait(millisec, ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => mres.Wait(timeSpan, ct), ct, "An OCE should have been thrown.");
            mres.Dispose();

            return(passed);
        }
Exemple #7
0
        public static bool CancelBeforeWait()
        {
            TestHarness.TestLog("* BarrierCancellationTests.CancelBeforeWait()");
            bool passed = true;

            Barrier barrier = new Barrier(3);

            CancellationTokenSource cs = new CancellationTokenSource();

            cs.Cancel();
            CancellationToken ct = cs.Token;

            const int millisec = 100;
            TimeSpan  timeSpan = new TimeSpan(100);

            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(millisec, ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(timeSpan, ct), ct, "An OCE should have been thrown.");

            barrier.Dispose();
            return(passed);
        }
Exemple #8
0
        public static bool CancelBeforeWait()
        {
            TestHarness.TestLog("* SemaphoreSlimCancellationTests.CancelBeforeWait()");
            bool passed = true;

            SemaphoreSlim semaphoreSlim = new SemaphoreSlim(2);

            CancellationTokenSource cs = new CancellationTokenSource();

            cs.Cancel();
            CancellationToken ct = cs.Token;

            const int millisec = 100;
            TimeSpan  timeSpan = new TimeSpan(100);

            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => semaphoreSlim.Wait(ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => semaphoreSlim.Wait(millisec, ct), ct, "An OCE should have been thrown.");
            passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => semaphoreSlim.Wait(timeSpan, ct), ct, "An OCE should have been thrown.");
            semaphoreSlim.Dispose();

            return(passed);
        }