Пример #1
0
        //This tests that Take/TryTake wake up correctly if CompleteAdding() is called while the taker is waiting.
        public static bool InternalCancellation_WakingUpTake()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.InternalCancellation_WakingUpTake()");
            bool passed = true;

            BlockingCollection <int> coll1 = new BlockingCollection <int>();

            ThreadPool.QueueUserWorkItem(
                (obj) =>
            {
                Thread.Sleep(500);
                coll1.CompleteAdding();
            });

            //call Take.. it should wake up with an OCE. when CompleteAdding() is called.
            passed &= TestHarnessAssert.IsFalse(coll1.IsAddingCompleted, "(1) At this point CompleteAdding should not have occurred.");
            passed &= TestHarnessAssert.EnsureExceptionThrown(
                () => coll1.Take(), typeof(InvalidOperationException),
                "an IOE should be thrown if CompleteAdding occurs during blocking Take()");

            return(passed);
        }
Пример #2
0
        //This tests that TryAdd wake up correctly if CompleteAdding() is called while the taker is waiting.
        public static bool InternalCancellation_WakingUpTryAdd()
        {
            TestHarness.TestLog("* BlockingCollectionCancellationTests.InternalCancellation_WakingUpTryAdd()");
            bool passed = true;

            BlockingCollection <int> coll1 = new BlockingCollection <int>(1);

            coll1.Add(1); //fills the collection.

            ThreadPool.QueueUserWorkItem(
                (obj) =>
            {
                Thread.Sleep(500);
                coll1.CompleteAdding();
            });

            passed &= TestHarnessAssert.IsFalse(coll1.IsAddingCompleted, "At this point CompleteAdding should not have occurred.");
            passed &= TestHarnessAssert.EnsureExceptionThrown(
                () => coll1.TryAdd(1, 1000000),  //an indefinite wait to add.. 1000 seconds.
                typeof(InvalidOperationException),
                "an InvalidOpEx should be thrown if CompleteAdding occurs during blocking Add()");

            return(passed);
        }