Esempio n. 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();
        }
Esempio n. 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();
        }
Esempio n. 3
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);
        }
Esempio n. 4
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();
        }
Esempio n. 5
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();
                }
            });
        }
Esempio n. 6
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 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();
        }
Esempio n. 8
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();
        }
        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);
        }
Esempio n. 10
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 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 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();
        }
        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();
        }
Esempio n. 14
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();
        }
Esempio n. 16
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);
        }
Esempio n. 18
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();
            });
        }
Esempio n. 19
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();
        }
Esempio n. 20
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();
            }
        }
Esempio n. 22
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);
        }
        public void WaitAllWithTimeoutFailure()
        {
            STP smartThreadPool = new STP();

            IWorkItemResult[] wirs = new IWorkItemResult[5];

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

            bool timeout = !STP.WaitAll(wirs, 10, true);
            bool success = timeout;

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
Esempio n. 24
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();
            });
        }
Esempio n. 25
0
        public void CancelInQueueWorkItem()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                STPStartInfo stpStartInfo = new STPStartInfo {
                    StartSuspended = true
                };

                STP stp             = new STP(stpStartInfo);
                IWorkItemResult wir = stp.QueueWorkItem(arg => null);

                wir.Cancel();

                Assert.IsTrue(wir.IsCanceled);

                try { wir.GetResult(); }
                finally { stp.Shutdown(); }
            });
        }
        public void CancelInQueueWorkItem()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                STPStartInfo stpStartInfo   = new STPStartInfo();
                stpStartInfo.StartSuspended = true;

                bool hasRun = false;

                STP stp             = new STP(stpStartInfo);
                IWorkItemResult wir = stp.QueueWorkItem(
                    new WorkItemInfo()
                {
                    Timeout = 500
                },
                    state =>
                {
                    hasRun = true;
                    return(null);
                });

                Assert.IsFalse(wir.IsCanceled);

                Thread.Sleep(2000);

                Assert.IsTrue(wir.IsCanceled);

                stp.Start();
                stp.WaitForIdle();

                Assert.IsFalse(hasRun);

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }
Esempio n. 27
0
        public void TestChoice()
        {
            STP stp = new STP();

            int index = stp.Choice(
                () => Thread.Sleep(1000),
                () => Thread.Sleep(1500),
                () => Thread.Sleep(500));

            Assert.AreEqual(2, index);

            index = stp.Choice(
                () => Thread.Sleep(300),
                () => Thread.Sleep(100),
                () => Thread.Sleep(200));

            Assert.AreEqual(1, index);

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

            ManualResetEvent isRunning = new ManualResetEvent(false);

            for (int i = 0; i < 4; ++i)
            {
                smartThreadPool.QueueWorkItem(delegate { isRunning.WaitOne(); });
            }

            bool success = !smartThreadPool.WaitForIdle(1000);

            isRunning.Set();

            success = success && smartThreadPool.WaitForIdle(1000);

            smartThreadPool.Shutdown();

            Assert.IsTrue(success);
        }
        public void TimeoutCompletedWorkItem()
        {
            STP             stp = new STP();
            IWorkItemResult wir =
                stp.QueueWorkItem(
                    new WorkItemInfo()
            {
                Timeout = 500
            },
                    state => 1);

            stp.WaitForIdle();

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

            Thread.Sleep(1000);

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

            stp.Shutdown();
        }
Esempio n. 30
0
        public void CancelCanceledWorkItem()
        {
            Assert.ThrowsException <WorkItemCancelException>(() =>
            {
                STPStartInfo stpStartInfo = new STPStartInfo {
                    StartSuspended = true
                };

                STP stp             = new STP(stpStartInfo);
                IWorkItemResult wir = stp.QueueWorkItem(state => null);

                int counter = 0;

                wir.Cancel();

                try
                {
                    wir.GetResult();
                }
                catch (WorkItemCancelException ce)
                {
                    ce.GetHashCode();
                    ++counter;
                }

                Assert.AreEqual(counter, 1);

                wir.Cancel();

                try
                {
                    wir.GetResult();
                }
                finally
                {
                    stp.Shutdown();
                }
            });
        }