Пример #1
0
        //=============================


        private void RunSimpleProcessWorkTest(int minThreadCount, int maxThreadCount, int queueCapacity, int workCount = 100000)
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(minThreadCount, maxThreadCount, queueCapacity, "name"))
            {
                Assert.IsTrue(testInst.ThreadCount >= minThreadCount);
                Assert.IsTrue(testInst.ActiveThreadCount >= minThreadCount);
                Assert.AreEqual(queueCapacity, testInst.QueueCapacity);
                Assert.IsTrue(testInst.IsWork);

                int expectedWork = workCount;
                int executedWork = 0;

                for (int i = 0; i < expectedWork; i++)
                {
                    testInst.Run(() =>
                    {
                        Interlocked.Increment(ref executedWork);
                    });
                }

                testInst.Dispose(true, true, true);

                Assert.AreEqual(0, testInst.ThreadCount);
                Assert.IsFalse(testInst.IsWork);
                Assert.AreEqual(expectedWork, executedWork);

                testInst.CompleteAdding();
            }
        }
Пример #2
0
        public void TestLongProcessWork()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, 100, "name"))
            {
                Assert.AreEqual(100, testInst.QueueCapacity);

                int expectedWork = 100;
                int executedWork = 0;

                for (int i = 0; i < expectedWork; i++)
                {
                    testInst.Run(() =>
                    {
                        Thread.Sleep(250);
                        Interlocked.Increment(ref executedWork);
                    });
                }

                testInst.Dispose(true, true, true);

                Assert.AreEqual(0, testInst.ThreadCount);
                Assert.AreEqual(0, testInst.ActiveThreadCount);
                Assert.IsFalse(testInst.IsWork);
                Assert.AreEqual(expectedWork, executedWork);
            }
        }
Пример #3
0
        public void TestQueueCapacityBound()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, 10, "name"))
            {
                Assert.AreEqual(10, testInst.QueueCapacity);

                int tryRunWorkItem = 100 * testInst.QueueCapacity;
                int runWorkCount   = 0;
                int executedWork   = 0;

                for (int i = 0; i < tryRunWorkItem; i++)
                {
                    if (testInst.TryRun(() =>
                    {
                        Thread.Sleep(500);
                        Interlocked.Increment(ref executedWork);
                    }))
                    {
                        runWorkCount++;
                    }
                }

                testInst.Dispose(true, true, true);
                Assert.IsTrue(runWorkCount > 0);
                Assert.IsTrue(runWorkCount < tryRunWorkItem);
                Assert.AreEqual(runWorkCount, executedWork);
            }
        }
Пример #4
0
        public void TestAwaitThroughSyncContext()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, 2 * Environment.ProcessorCount, -1, "name", false, new DynamicThreadPoolOptions()
            {
                UseOwnTaskScheduler = true, UseOwnSyncContext = true
            }))
            {
                bool isNotFailed  = false;
                bool isThreadPool = false;

                testInst.Run(() =>
                {
                    var task = AwaitThroughSyncContext();
                    task.ContinueWith(t =>
                    {
                        Volatile.Write(ref isNotFailed, !t.IsFaulted);
                        Volatile.Write(ref isThreadPool, testInst.IsThreadPoolThread);
                    }, TaskContinuationOptions.ExecuteSynchronously).Wait();
                });

                testInst.Dispose(true, true, false);

                Assert.IsTrue(isNotFailed, "isNotFailed");
                Assert.IsTrue(isThreadPool, "isThreadPool");
            }
        }
Пример #5
0
        public void WaitUntilStopWorkCorrect()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, -1, "name"))
            {
                Assert.AreEqual(-1, testInst.QueueCapacity);

                int expectedWork = 100;
                int executedWork = 0;

                for (int i = 0; i < expectedWork; i++)
                {
                    testInst.Run(() =>
                    {
                        Thread.Sleep(200);
                        Interlocked.Increment(ref executedWork);
                    });
                }

                testInst.Dispose(false, true, false);
                Assert.IsTrue(executedWork < expectedWork);
                Assert.IsTrue(testInst.State == ThreadPoolState.StopRequested);

                testInst.WaitUntilStop();
                Assert.IsTrue(testInst.State == ThreadPoolState.Stopped);
                Assert.AreEqual(expectedWork, executedWork);
            }
        }
Пример #6
0
        protected override void Dispose(bool isUserCall)
        {
            if (isUserCall)
            {
                _tasks.ForEach(x => x.Dispose());
                _threadPool.Dispose(false, false, false);
            }

            base.Dispose(isUserCall);
        }
Пример #7
0
        protected override void Dispose(bool isUserCall)
        {
            if (isUserCall)
            {
                _token.Cancel();
                _event.Set();
                _threadPool.Dispose(false, false, false);
            }

            base.Dispose(isUserCall);
        }
Пример #8
0
        public void TestNotAddAfterDispose()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, -1, "name"))
            {
                Assert.IsTrue(testInst.TryRun(() => { }));

                testInst.Dispose();

                Assert.IsFalse(testInst.TryRun(() => { }));

                testInst.Run(() => { });
            }
        }
Пример #9
0
        public void TestTaskSchedulerSetted()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, -1, "name", false, new DynamicThreadPoolOptions()
            {
                UseOwnTaskScheduler = true, UseOwnSyncContext = true
            }))
            {
                AtomicBool isPropperSceduller = new AtomicBool(false);

                testInst.Run(() =>
                {
                    isPropperSceduller.Value = TaskScheduler.Current == testInst.TaskScheduler;
                });

                TimingAssert.IsTrue(10000, isPropperSceduller, "isPropperSceduller");
                testInst.Dispose(true, true, false);
            }
        }
Пример #10
0
        public void TestQueueCapacityBoundExtends()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, 10, "name"))
            {
                int expectedWork = 25 + Environment.ProcessorCount;
                int executedWork = 0;

                ManualResetEventSlim tracker = new ManualResetEventSlim();

                for (int i = 0; i < expectedWork; i++)
                {
                    testInst.Run(() =>
                    {
                        tracker.Wait();
                        Interlocked.Increment(ref executedWork);
                    });
                }

                tracker.Set();

                testInst.Dispose(true, true, true);
                Assert.AreEqual(expectedWork, executedWork);
            }
        }
Пример #11
0
        public void TestNoSyncContextAndTaskSchedullerWhenNotConfigurated()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, -1, "name", false, new DynamicThreadPoolOptions()
            {
                UseOwnTaskScheduler = false, UseOwnSyncContext = false
            }))
            {
                var defSyncContext    = SynchronizationContext.Current;
                var defTaskScheduller = TaskScheduler.Current;

                AtomicBool isDefaultSyncContext    = new AtomicBool(false);
                AtomicBool isDefaultTaskScheduller = new AtomicBool(false);

                testInst.Run(() =>
                {
                    isDefaultSyncContext.Value    = SynchronizationContext.Current == defSyncContext;
                    isDefaultTaskScheduller.Value = TaskScheduler.Current == defTaskScheduller;
                });

                TimingAssert.IsTrue(10000, isDefaultSyncContext, "isDefaultSyncContext");
                TimingAssert.IsTrue(10000, isDefaultTaskScheduller, "isDefaultTaskScheduller");
                testInst.Dispose(true, true, false);
            }
        }
Пример #12
0
        public void TestSyncContextSetted()
        {
            using (DynamicThreadPool testInst = new DynamicThreadPool(0, Environment.ProcessorCount, -1, "name", false, new DynamicThreadPoolOptions()
            {
                UseOwnTaskScheduler = true, UseOwnSyncContext = true
            }))
            {
                AtomicBool isPropperSyncContext = new AtomicBool(false);

                testInst.Run(() =>
                {
                    if (SynchronizationContext.Current != null)
                    {
                        SynchronizationContext.Current.Post((st) =>
                        {
                            isPropperSyncContext.Value = testInst.IsThreadPoolThread;
                        }, null);
                    }
                });

                TimingAssert.IsTrue(10000, isPropperSyncContext, "isPropperSyncContext");
                testInst.Dispose(true, true, false);
            }
        }