Esempio n. 1
0
        public void PaddingTest()
        {
            using (var group = new ThreadGroup(2, createBackgroundThreads: true, name: "PaddingTest")
            {
                DefaultConcurrencyPadding = 1
            }) {
                var queue    = group.GetQueueForType <HighPriorityBlockingWorkItem>();
                var barrier1 = new HighPriorityBlockingWorkItem {
                    Signal = new AutoResetEvent(false)
                };
                queue.Enqueue(barrier1);
                var barrier2 = new HighPriorityBlockingWorkItem {
                    Signal = new AutoResetEvent(false)
                };
                queue.Enqueue(barrier2);
                group.NotifyQueuesChanged();

                Thread.Sleep(300);
                Assert.AreEqual(1, queue.ItemsInFlight);

                barrier1.Signal.Set();
                Assert.IsFalse(queue.WaitUntilDrained(150));
                Assert.AreEqual(1, queue.ItemsInFlight);

                barrier2.Signal.Set();
                Assert.IsTrue(queue.WaitUntilDrained(150));
            }
        }
Esempio n. 2
0
        public void PriorityTest()
        {
            using (var group = new ThreadGroup(2, createBackgroundThreads: true, name: "PriorityTest")
            {
                DefaultConcurrencyPadding = 0
            }) {
                var queue    = group.GetQueueForType <HighPriorityBlockingWorkItem>();
                var barrier1 = new HighPriorityBlockingWorkItem {
                    Signal = new AutoResetEvent(false)
                };
                queue.Enqueue(barrier1);
                var barrier2 = new HighPriorityBlockingWorkItem {
                    Signal = new AutoResetEvent(false)
                };
                queue.Enqueue(barrier2);
                var barrier3 = new HighPriorityBlockingWorkItem {
                    Signal = new AutoResetEvent(false)
                };
                queue.Enqueue(barrier3);
                group.NotifyQueuesChanged();

                // HACK: Give the thread group time to start processing high priority items
                while (queue.ItemsInFlight < group.ThreadCount)
                {
                    Thread.Sleep(1);
                }

                var voidQueue = group.GetQueueForType <VoidWorkItem>();
                var testItem  = new VoidWorkItem();
                for (int i = 0; i < 10; i++)
                {
                    voidQueue.Enqueue(testItem);
                }
                group.NotifyQueuesChanged();

                var ok = voidQueue.WaitUntilDrained(50);
                Assert.IsFalse(ok, "wait for low priority queue to drain while threads are blocked on high priority");
                barrier1.Signal.Set();

                Assert.IsFalse(voidQueue.WaitUntilDrained(50), "ensure new high priority item is claimed by thread that finished previous one");
                barrier2.Signal.Set();
                barrier3.Signal.Set();

                ok = voidQueue.WaitUntilDrained(500);
                Assert.IsTrue(ok, "once high priority items are done the low priority ones should run");
            }
        }