Exemple #1
0
        public void TestThreadPoolManyThreadsActionTNumberOfCalls()
        {
            threadPool = new ThreadPool(Environment.ProcessorCount, "testWorker");

            var wirs    = new List <IWorkItemState>();
            var objects = new List <ThreadingThreadpoolTestObject>();

            for (var j = 0; j < 5000; j++)
            {
                wirs.Clear();
                objects.Clear();
                for (var i = 0; i < 100; i++)
                {
                    var o = new ThreadingThreadpoolTestObject();
                    objects.Add(o);
                    var wir = threadPool.EnqueueWorkItem(o.Call);
                    wirs.Add(wir);
                }

                foreach (var workItemState in wirs)
                {
                    workItemState.Result();
                    workItemState.Dispose();
                }

                foreach (var threadingThreadpoolTestObject in objects)
                {
                    Assert.AreEqual(1, threadingThreadpoolTestObject.NumberOfCalls,
                                    "One of the objects has been called " + threadingThreadpoolTestObject.NumberOfCalls +
                                    " times. It was expected to be called once only and exactly once.");
                }
            }

            threadPool.ShutDown();
        }
        public void TestThreadPoolManyThreadsActionTNumberOfCalls()
        {
            threadPool = new ThreadPool(Environment.ProcessorCount, "testWorker");

            var wirs = new List<IWorkItemState>();
            var objects = new List<ThreadingThreadpoolTestObject>();

            for (var j = 0; j < 5000; j++)
            {
                wirs.Clear();
                objects.Clear();
                for (var i = 0; i < 100; i++)
                {
                    var o = new ThreadingThreadpoolTestObject();
                    objects.Add(o);
                    var wir = threadPool.EnqueueWorkItem(o.Call);
                    wirs.Add(wir);
                }

                foreach (var workItemState in wirs)
                {
                    workItemState.Result();
                    workItemState.Dispose();
                }

                foreach (var threadingThreadpoolTestObject in objects)
                {
                    Assert.AreEqual(1, threadingThreadpoolTestObject.NumberOfCalls,
                        "One of the objects has been called " + threadingThreadpoolTestObject.NumberOfCalls +
                        " times. It was expected to be called once only and exactly once.");
                }
            }

            threadPool.ShutDown();
        }