示例#1
0
        public void TestPipe()
        {
            SafeCounter sc  = new SafeCounter();
            STP         stp = new STP();

            stp.Pipe(
                sc,
                sc1 => { if (sc.Counter == 0)
                         {
                             sc1.Increment();
                         }
                },
                sc1 => { if (sc.Counter == 1)
                         {
                             sc1.Increment();
                         }
                },
                sc1 => { if (sc.Counter == 2)
                         {
                             sc1.Increment();
                         }
                }
                );

            Assert.AreEqual(3, sc.Counter);

            stp.Shutdown();
        }
示例#2
0
        public void DisposeCallerState()
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                DisposeOfStateObjects = true
            };

            STP smartThreadPool = new STP(stpStartInfo);

            CallerState nonDisposableCallerState = new NonDisposableCallerState();
            CallerState disposableCallerState    = new DisposableCallerState();

            IWorkItemResult wir1 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    nonDisposableCallerState);

            IWorkItemResult wir2 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    disposableCallerState);

            wir1.GetResult();
            Assert.AreEqual(1, nonDisposableCallerState.Value);

            wir2.GetResult();

            // Wait a little bit for the working thread to call dispose on the
            // work item's state.
            smartThreadPool.WaitForIdle();

            Assert.AreEqual(2, disposableCallerState.Value);

            smartThreadPool.Shutdown();
        }
        public void TimeoutInProgressWorkItemWithSample()
        {
            bool             timedout       = false;
            ManualResetEvent waitToStart    = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                new WorkItemInfo()
            {
                Timeout = 500
            },
                state =>
            {
                waitToStart.Set();
                Thread.Sleep(1000);
                timedout = STP.IsWorkItemCanceled;
                waitToComplete.Set();
                return(null);
            });

            waitToStart.WaitOne();

            waitToComplete.WaitOne();

            Assert.IsTrue(timedout);

            stp.Shutdown();
        }
示例#4
0
        public void CancelInProgressWorkItemSoftWithIgnoreSample()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                ManualResetEvent waitToStart    = new ManualResetEvent(false);
                ManualResetEvent waitToComplete = new ManualResetEvent(false);

                STP stp             = new STP();
                IWorkItemResult wir = stp.QueueWorkItem(
                    state =>
                {
                    waitToStart.Set();
                    Thread.Sleep(100);
                    waitToComplete.WaitOne();
                    return(null);
                }
                    );

                waitToStart.WaitOne();

                wir.Cancel(false);

                waitToComplete.Set();

                stp.WaitForIdle();

                // Throws WorkItemCancelException
                wir.GetResult();

                stp.Shutdown();
            });
        }
        public void ExceptionThrowing()
        {
            STP _smartThreadPool = new STP();

            DivArgs divArgs = new DivArgs
            {
                x = 10,
                y = 0
            };

            IWorkItemResult wir =
                _smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs);

            try
            {
                wir.GetResult();
            }
            catch (WorkItemResultException wire)
            {
                Assert.IsTrue(wire.InnerException is System.DivideByZeroException);
                return;
            }
            catch (System.Exception e)
            {
                e.GetHashCode();
                Assert.Fail();
            }
            Assert.Fail();
        }
        public void TimeoutInProgressWorkItemSoftWithAbortOnWorkItemCancel()
        {
            bool             abortFailed    = false;
            ManualResetEvent waitToStart    = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                new WorkItemInfo()
            {
                Timeout = 500
            },
                state =>
            {
                waitToStart.Set();
                Thread.Sleep(1000);
                STP.AbortOnWorkItemCancel();
                abortFailed = true;
                return(null);
            });

            waitToStart.WaitOne();

            stp.WaitForIdle();

            Assert.IsTrue(wir.IsCanceled);
            Assert.IsFalse(abortFailed);
            stp.Shutdown();
        }
        public void TestConcurrencyChange()
        {
            STP smartThreadPool = new STP(10 * 1000, 1, 0);

            bool success = false;

            for (int i = 0; i < 100; ++i)
            {
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    null);
            }

            smartThreadPool.Concurrency = 1;
            success = WaitForMaxThreadsValue(smartThreadPool, 1, 1 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Concurrency = 5;
            success = WaitForMaxThreadsValue(smartThreadPool, 5, 2 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Concurrency = 25;
            success = WaitForMaxThreadsValue(smartThreadPool, 25, 4 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Concurrency = 10;
            success = WaitForMaxThreadsValue(smartThreadPool, 10, 10 * 1000);
            Assert.IsTrue(success);

            smartThreadPool.Shutdown();
        }
示例#8
0
        //[ExpectedException(typeof(WorkItemCancelException))]
        public void CancelInProgressWorkItemSoft()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                ManualResetEvent waitToStart = new ManualResetEvent(false);

                STP stp             = new STP();
                IWorkItemResult wir = stp.QueueWorkItem(
                    state =>
                {
                    waitToStart.Set();
                    Thread.Sleep(100);
                    return(null);
                }
                    );

                waitToStart.WaitOne();

                wir.Cancel(false);

                Assert.IsTrue(wir.IsCanceled);

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }
示例#9
0
        public void DontDisposeCallerState()
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                DisposeOfStateObjects = false
            };

            STP smartThreadPool = new STP(stpStartInfo);

            CallerState nonDisposableCallerState = new NonDisposableCallerState();
            CallerState disposableCallerState    = new DisposableCallerState();

            IWorkItemResult wir1 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    nonDisposableCallerState);

            IWorkItemResult wir2 =
                smartThreadPool.QueueWorkItem(
                    new WorkItemCallback(this.DoSomeWork),
                    disposableCallerState);

            wir1.GetResult();
            bool success = (1 == nonDisposableCallerState.Value);

            wir2.GetResult();

            success = success && (1 == disposableCallerState.Value);

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
示例#10
0
        public void CancelInProgressWorkItemSoftWithAbortOnWorkItemCancel()
        {
            bool             abortFailed  = false;
            ManualResetEvent waitToStart  = new ManualResetEvent(false);
            ManualResetEvent waitToCancel = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state =>
            {
                waitToStart.Set();
                waitToCancel.WaitOne();
                STP.AbortOnWorkItemCancel();
                abortFailed = true;
                return(null);
            });

            waitToStart.WaitOne();

            wir.Cancel(false);

            waitToCancel.Set();

            stp.WaitForIdle();

            Assert.IsTrue(wir.IsCanceled);
            Assert.IsFalse(abortFailed);

            stp.Shutdown();
        }
示例#11
0
        public void TestJoin()
        {
            STP stp = new STP();

            SafeCounter sc = new SafeCounter();

            stp.Join(
                sc.Increment,
                sc.Increment,
                sc.Increment);

            Assert.AreEqual(3, sc.Counter);

            for (int j = 0; j < 10; j++)
            {
                sc.Reset();

                Action[] actions = new Action[1000];
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = sc.Increment;
                }

                stp.Join(actions);

                Assert.AreEqual(actions.Length, sc.Counter);
            }

            stp.Shutdown();
        }
示例#12
0
        public void CancelInProgressWorkItemSoftWithSample()
        {
            bool             cancelled      = false;
            ManualResetEvent waitToStart    = new ManualResetEvent(false);
            ManualResetEvent waitToComplete = new ManualResetEvent(false);

            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state =>
            {
                waitToStart.Set();
                waitToComplete.WaitOne();
                cancelled = STP.IsWorkItemCanceled;
                return(null);
            }
                );

            waitToStart.WaitOne();

            wir.Cancel(false);

            waitToComplete.Set();

            stp.WaitForIdle();

            Assert.IsTrue(cancelled);

            stp.Shutdown();
        }
        public void ExceptionReturning()
        {
            bool success = true;

            STP _smartThreadPool = new STP();

            DivArgs divArgs = new DivArgs
            {
                x = 10,
                y = 0
            };

            IWorkItemResult wir =
                _smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs);

            System.Exception e = null;
            try
            {
                wir.GetResult(out e);
            }
            catch (System.Exception ex)
            {
                ex.GetHashCode();
                success = false;
            }

            Assert.IsTrue(success);
            Assert.IsTrue(e is System.DivideByZeroException);
        }
        private object DoWaitForIdle(object state)
        {
            STP smartThreadPool = state as STP;

            smartThreadPool.WaitForIdle();
            return(null);
        }
        public void WaitAny()
        {
            STP smartThreadPool = new STP();

            bool success = false;

            IWorkItemResult[] wirs = new IWorkItemResult[5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] =
                    smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
            }

            int index = STP.WaitAny(wirs);

            if (wirs[index].IsCompleted)
            {
                int result = (int)wirs[index].GetResult();
                if (1 == result)
                {
                    success = true;
                }
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public void WaitAnyT()
        {
            STP smartThreadPool = new STP();

            bool success = false;

            IWorkItemResult <int>[] wirs = new IWorkItemResult <int> [5];

            for (int i = 0; i < wirs.Length; ++i)
            {
                wirs[i] = smartThreadPool.QueueWorkItem(new Func <int, int, int>(System.Math.Max), i, i - 1);
            }

            int index = STP.WaitAny(wirs);

            if (wirs[index].IsCompleted)
            {
                int result = wirs[index].GetResult();
                if (index == result)
                {
                    success = true;
                }
            }

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public void WaitAllWithEmptyArray()
        {
            IWorkItemResult[] wirs = new IWorkItemResult[0];

            bool success = STP.WaitAll(wirs);;

            Assert.IsTrue(success);
        }
示例#18
0
        public void GoodCallback()
        {
            STP stp = new STP();

            stp.QueueWorkItem(new WorkItemCallback(DoWork));

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void ExceptionType()
        {
            STP smartThreadPool = new STP();

            var workItemResult = smartThreadPool.QueueWorkItem(new Func <int>(ExceptionMethod));

            smartThreadPool.WaitForIdle();

            Assert.IsInstanceOfType(workItemResult.Exception, typeof(NotImplementedException));

            smartThreadPool.Shutdown();
        }
示例#20
0
        public void GoodPostExecute()
        {
            STP stp = new STP();

            stp.QueueWorkItem(
                new WorkItemCallback(DoWork),
                null,
                new PostExecuteWorkItemCallback(DoPostExecute));

            stp.WaitForIdle();

            stp.Shutdown();
        }
        public void WaitForIdleOnWrongThread()
        {
            STP smartThreadPool = new STP(10 * 1000, 25, 0);

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

            wir.GetResult(out Exception e);

            smartThreadPool.Shutdown();

            Assert.IsTrue(e is NotSupportedException);
        }
        private void CheckSinglePriority(ThreadPriority threadPriority)
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                ThreadPriority = threadPriority
            };

            STP stp = new STP(stpStartInfo);

            IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(GetThreadPriority));
            ThreadPriority  currentThreadPriority = (ThreadPriority)wir.GetResult();

            Assert.AreEqual(threadPriority, currentThreadPriority);
        }
        private bool WaitForMinThreadsValue(STP smartThreadPool, int minThreadsCount, int timeout)
        {
            DateTime end = DateTime.Now + new TimeSpan(0, 0, 0, 0, timeout);

            bool success = false;

            while (DateTime.Now <= end && !success)
            {
                success = (smartThreadPool.ActiveThreads == minThreadsCount);
                Thread.Sleep(10);
            }

            return(success);
        }
示例#24
0
        public void ChainedDelegatesCallback()
        {
            Assert.ThrowsException <NotSupportedException>(() =>
            {
                STP stp = new STP();

                WorkItemCallback workItemCallback = new WorkItemCallback(DoWork);
                workItemCallback += new WorkItemCallback(DoWork);

                stp.QueueWorkItem(workItemCallback);

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }
示例#25
0
        public void CancelCompletedWorkItem()
        {
            STP             stp = new STP();
            IWorkItemResult wir = stp.QueueWorkItem(
                state => 1
                );

            stp.WaitForIdle();

            Assert.AreEqual(wir.GetResult(), 1);

            wir.Cancel();

            Assert.AreEqual(wir.GetResult(), 1);

            stp.Shutdown();
        }
示例#26
0
        private static void CheckApartmentState(ApartmentState requestApartmentState)
        {
            STPStartInfo stpStartInfo = new STPStartInfo
            {
                ApartmentState = requestApartmentState
            };

            STP stp = new STP(stpStartInfo);

            IWorkItemResult <ApartmentState> wir = stp.QueueWorkItem(() => GetCurrentThreadApartmentState());

            ApartmentState resultApartmentState = wir.GetResult();

            stp.WaitForIdle();

            Assert.AreEqual(requestApartmentState, resultApartmentState);
        }
示例#27
0
        //[ExpectedException(typeof(WorkItemCancelException))]
        public void CancelCancelledWorkItemAbort()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                ManualResetEvent waitToStart = new ManualResetEvent(false);

                STP stp             = new STP();
                IWorkItemResult wir = stp.QueueWorkItem(
                    state =>
                {
                    waitToStart.Set();
                    while (true)
                    {
                        Thread.Sleep(1000);
                    }
#pragma warning disable CS0162 // Unreachable code detected
                    return(null);

#pragma warning restore CS0162 // Unreachable code detected
                }
                    );

                waitToStart.WaitOne();

                wir.Cancel(false);

                Assert.IsTrue(wir.IsCanceled);

                bool completed = stp.WaitForIdle(1000);

                Assert.IsFalse(completed);

                wir.Cancel(true);

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }
        public void FuncT()
        {
            STP stp = new STP();
            IWorkItemResult <int> wir =
                stp.QueueWorkItem(new Func <int, int>(Function), 1);

            int y = wir.GetResult();

            Assert.AreEqual(y, 2);

            try
            {
                wir.GetResult();
            }
            finally
            {
                stp.Shutdown();
            }
        }
示例#29
0
        public void TestThreadsEvents()
        {
            ClearResults();

            STP stp = new STP();

            stp.OnThreadInitialization += OnInitialization;
            stp.OnThreadTermination    += OnTermination;

            stp.QueueWorkItem(new WorkItemCallback(DoSomeWork), null);

            stp.WaitForIdle();
            stp.Shutdown();

            Thread.Sleep(500); // Wait for the STP to shutdown.
            Assert.IsTrue(_initSuccess);
            Assert.IsTrue(_workItemSuccess);
            Assert.IsTrue(_termSuccess);
        }
示例#30
0
        public void ChainedDelegatesPostExecute()
        {
            Assert.ThrowsException <NotSupportedException>(() =>
            {
                STP stp = new STP();

                PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
                postExecuteWorkItemCallback += DoPostExecute;

                stp.QueueWorkItem(
                    new WorkItemCallback(DoWork),
                    null,
                    postExecuteWorkItemCallback);

                stp.WaitForIdle();

                stp.Shutdown();
            });
        }