public void WorkItemWaitCanceling()
        {
            Assert.ThrowsException <WorkItemTimeoutException>(() =>
            {
                STP smartThreadPool = new STP();

                ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);

                // Queue a work item that will occupy the thread in the pool
                IWorkItemResult wir1 =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

                // Queue another work item that will wait for the first to complete
                IWorkItemResult wir2 =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);

                try
                {
                    wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
                }
                finally
                {
                    smartThreadPool.Shutdown();
                }
            });
        }
Esempio n. 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);
            });
        }
Esempio n. 3
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();
        }
Esempio n. 4
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).
            });
        }
 private void PauseSTP(STP stp)
 {
     _pauseSTP.Reset();
     stp.QueueWorkItem(
         new WorkItemCallback(this.DoPauseSTP),
         null);
 }
Esempio n. 6
0
        /// <summary>
        /// Example of how to use the post execute callback
        /// </summary>
        private bool DoTestDefaultPostExecute(CallToPostExecute callToPostExecute, bool answer)
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                CallToPostExecute           = callToPostExecute,
                PostExecuteWorkItemCallback = new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork)
            };

            STP smartThreadPool = new STP(stpStartInfo);

            bool success = false;

            PostExecuteResult postExecuteResult = new PostExecuteResult();

            IWorkItemResult wir =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    postExecuteResult);

            if (!wir.IsCompleted)
            {
                int result = (int)wir.GetResult();
                success = (1 == result);
                success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer);
            }

            smartThreadPool.Shutdown();

            return(success);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        /// <summary>
        /// Example of how to queue a work item and then cancel it while it is in the queue.
        /// </summary>
        private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer)
        {
            // Create a STP with only one thread.
            // It just to show how to use the work item canceling feature
            STP smartThreadPool = new STP(10 * 1000, 1);

            bool success = false;
            PostExecuteResult postExecuteResult = new PostExecuteResult();

            // Queue a work item that will occupy the thread in the pool
            smartThreadPool.QueueWorkItem(
                new WorkItemCallback(this.DoSomeWork),
                null);

            // Queue another work item that will wait for the first to complete
            IWorkItemResult wir =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    postExecuteResult,
                    new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork),
                    callToPostExecute);


            // Wait a while for the thread pool to start executing the first work item
            Thread.Sleep(100);

            // Cancel the second work item while it still in the queue
            if (wir.Cancel())
            {
                success = (postExecuteResult.wh.WaitOne(1000, true) == answer);
            }

            smartThreadPool.Shutdown();

            return(success);
        }
        private static void CheckIsBackground(bool isBackground)
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                AreThreadsBackground = isBackground
            };

            STP stp = new STP(stpStartInfo);

            IWorkItemResult <bool> wir = stp.QueueWorkItem(() => GetCurrentThreadIsBackground());

            bool resultIsBackground = wir.GetResult();

            stp.WaitForIdle();

            Assert.AreEqual(isBackground, resultIsBackground);
        }
        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));
        }
        public void Timeout()
        {
            Assert.ThrowsException <WorkItemTimeoutException>(() =>
            {
                STP smartThreadPool = new STP();

                IWorkItemResult wir =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

                try
                {
                    wir.GetResult(500, true);
                }
                finally
                {
                    smartThreadPool.Shutdown();
                }
            });
        }
        public void BlockingCall()
        {
            STP smartThreadPool = new STP();

            bool success = false;

            IWorkItemResult wir =
                smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);

            if (!wir.IsCompleted)
            {
                int result = (int)wir.GetResult();
                success = (1 == result);
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
Esempio n. 14
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();
        }
        public void ActionT0()
        {
            IWorkItemResult wir = _stp.QueueWorkItem(Action0);

            Assert.IsNull(wir.State);
        }