예제 #1
0
        public void QueueWorkItem_WhenQueueMaxLengthZero_RejectsInsteadOfQueueing()
        {
            new Thread(() => Assert.ThrowsException <QueueRejectedException>(() =>
            {
                var info = new STPStartInfo
                {
                    MaxQueueLength   = 0,
                    MinWorkerThreads = 2,
                    MaxWorkerThreads = 2,
                };
                var pool = new STP(info);
                pool.Start();

                try
                {
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                }
                catch (QueueRejectedException e)
                {
                    throw new Exception("Caught QueueRejectedException too early: ", e);
                }

                pool.QueueWorkItem(SleepForOneSecond);
            })).Start();
        }
예제 #2
0
        public void QueueWorkItem_WhenMaxIsSet_ThrowsExceptionWhenHit()
        {
            Assert.ThrowsException <QueueRejectedException>(() =>
            {
                var info = new STPStartInfo
                {
                    MaxQueueLength   = 1,
                    MinWorkerThreads = 1,
                    MaxWorkerThreads = 1,
                };
                var pool = new STP(info);
                pool.Start();

                try
                {
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available, pool at max threads. Queued.
                }
                catch (QueueRejectedException e)
                {
                    throw new Exception("Caught QueueRejectedException too early: ", e);
                }

                // No waiters available, queue is at max (1). Throws.
                pool.QueueWorkItem(SleepForOneSecond);
            });
        }
        public void STPAndWIGStartSuspended()
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                StartSuspended = true
            };

            STP stp = new STP(stpStartInfo);

            WIGStartInfo wigStartInfo = new WIGStartInfo
            {
                StartSuspended = true
            };

            IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10, wigStartInfo);

            wig.QueueWorkItem(new WorkItemCallback(this.DoWork));

            Assert.IsFalse(wig.WaitForIdle(200));

            wig.Start();

            Assert.IsFalse(wig.WaitForIdle(200));

            stp.Start();

            Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle");
            Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle");
        }
예제 #4
0
        public void StpStartInfo_WithZeroMaxQueueLength_IsAllowed()
        {
            var info = new STPStartInfo
            {
                MaxQueueLength = 0,
            };
            var pool = new STP(info);

            pool.Start();
            Assert.IsTrue(0 == pool.STPStartInfo.MaxQueueLength);
        }
예제 #5
0
        public void QueueWorkItem_WhenMaxIsNull_Queues()
        {
            var info = new STPStartInfo
            {
                MaxQueueLength = null,
            };
            var pool = new STP(info);

            pool.Start();
            var workItem = pool.QueueWorkItem <object>(ReturnNull);

            // If rejected, an exception would have been thrown instead.
            Assert.IsTrue(workItem.GetResult() == null);
        }
예제 #6
0
        public void SetMaxQueueLength_FromNonZeroValueToZero_DisablesQueueing()
        {
            Assert.ThrowsException <QueueRejectedException>(() =>
            {
                var info = new STPStartInfo
                {
                    MinWorkerThreads = 1,
                    MaxWorkerThreads = 1,
                    MaxQueueLength   = 1,
                };

                var pool = new STP(info);
                pool.Start();

                try
                {
                    pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter.
                    pool.QueueWorkItem(SleepForOneSecond); // Queued.
                }
                catch (QueueRejectedException e)
                {
                    throw new Exception("Caught QueueRejectedException too early: ", e);
                }

                try
                {
                    pool.QueueWorkItem(SleepForOneSecond);
                }
                catch (QueueRejectedException)
                {
                    // Expected
                    Assert.IsTrue(true);
                }

                pool.MaxQueueLength = 0;
                Thread.Sleep(2100); // Let the work items complete.

                try
                {
                    pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter.
                }
                catch (QueueRejectedException e)
                {
                    throw new Exception("Caught QueueRejectedException too early: ", e);
                }

                pool.QueueWorkItem(SleepForOneSecond); // Rejected (max queue length is zero).
            });
        }
        public void StartSuspended()
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                StartSuspended = true
            };

            STP stp = new STP(stpStartInfo);

            stp.QueueWorkItem(new WorkItemCallback(this.DoWork));

            Assert.IsFalse(stp.WaitForIdle(200));

            stp.Start();

            Assert.IsTrue(stp.WaitForIdle(200));
        }
예제 #8
0
        private void Concurrency(
            int concurrencyPerWig,
            int wigsCount,
            int workItemsCount)
        {
            Console.WriteLine(
                "Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}",
                concurrencyPerWig,
                wigsCount,
                workItemsCount);

            _success           = true;
            _concurrencyPerWig = concurrencyPerWig;
            _randGen           = new Random(0);

            STPStartInfo stpStartInfo = new STPStartInfo
            {
                StartSuspended = true
            };

            STP stp = new STP(stpStartInfo);

            _concurrentOps = new int[wigsCount];

            IWorkItemsGroup[] wigs = new IWorkItemsGroup[wigsCount];

            for (int i = 0; i < wigs.Length; ++i)
            {
                wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig);
                for (int j = 0; j < workItemsCount; ++j)
                {
                    wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i);
                }

                wigs[i].Start();
            }

            stp.Start();

            stp.WaitForIdle();

            Assert.IsTrue(_success);

            stp.Shutdown();
        }
예제 #9
0
        public void QueueWorkItem_WhenBiggerMaxIsSet_ThrowsExceptionWhenHit()
        {
            new Thread(() => Assert.ThrowsException <QueueRejectedException>(() =>
            {
                var info = new STPStartInfo
                {
                    MaxQueueLength   = 5,
                    MinWorkerThreads = 5,
                    MaxWorkerThreads = 10,
                };
                var pool = new STP(info);
                pool.Start();

                try
                {
                    // Pool starts with 5 available waiters.

                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued.

                    pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued.
                    pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued.

                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued.
                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued.
                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued.
                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued.
                    pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued.
                }
                catch (QueueRejectedException e)
                {
                    throw new Exception("Caught QueueRejectedException too early: ", e);
                }

                // All threads are busy, and queue is at its max. Throws.
                pool.QueueWorkItem(SleepForOneSecond);
            })).Start();
        }
예제 #10
0
        public void SetMaxQueueLength_IncreasedFromZero_AllowsLargerQueue()
        {
            var info = new STPStartInfo
            {
                MinWorkerThreads = 1,
                MaxWorkerThreads = 1,
                MaxQueueLength   = 0,
            };

            var pool = new STP(info);

            pool.Start();

            try
            {
                pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter.
            }
            catch (QueueRejectedException e)
            {
                throw new Exception("Caught QueueRejectedException too early: ", e);
            }

            try
            {
                pool.QueueWorkItem(SleepForOneSecond);
            }
            catch (QueueRejectedException)
            {
                // Expected
                Assert.IsTrue(true);
            }

            pool.MaxQueueLength = 1;

            // Don't wait for worker item to complete, the queue should have immediately increased its allowance.

            var workItem = pool.QueueWorkItem <object>(ReturnNull);

            // If rejected, an exception would have been thrown instead.
            Assert.IsTrue(workItem.GetResult() == null);
        }