Exemple #1
0
        public void TakeDestroyReleaseWorkAsRemove()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    testInst.Add(i, new PoolOperations(), true);
                }

                for (int i = 0; i < 10; i++)
                {
                    var item = testInst.Take();
                    item.MarkElementDestroyed();
                    testInst.Release(item);


                    Assert.IsTrue(item.IsRemoved);

                    Assert.IsTrue(item.IsRemoved);
                    Assert.AreEqual(10 - i - 1, testInst.AvailableCount);
                    Assert.AreEqual(10 - i - 1, testInst.Count);
                }
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
        private static TimeSpan TestObjectPoolWithSyncPrior(PrioritizedElementsContainer <PoolElem> pool, int threadCount, int opCount, int pauseSpin)
        {
            Thread[] threads  = new Thread[threadCount];
            Barrier  startBar = new Barrier(threadCount + 1);

            int opCountPerThread = opCount / threadCount;

            Action thAct = () =>
            {
                startBar.SignalAndWait();

                int execOp = 0;
                while (execOp++ < opCountPerThread)
                {
                    PoolElementWrapper <PoolElem> el = null;
                    try
                    {
                        el = pool.Take();
                        //Thread.Sleep(pauseSpin);
                        SpinWaitHelper.SpinWait(pauseSpin);
                    }
                    finally
                    {
                        pool.Release(el);
                    }
                }
            };


            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(thAct));
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }

            startBar.SignalAndWait();
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            sw.Stop();

            Console.WriteLine("PrioritizedElementsContainer. Elapsed = " + sw.ElapsedMilliseconds.ToString() + "ms");

            return(sw.Elapsed);
        }
Exemple #3
0
        public void TestSimpleTakeRelease()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    testInst.Add(i, new PoolOperations(), true);
                }

                Assert.AreEqual(10, testInst.Count);
                Assert.AreEqual(10, testInst.AvailableCount);

                var item = testInst.Take();
                Assert.IsNotNull(item);
                Assert.IsTrue(item.Element >= 0 && item.Element < 10);
                Assert.IsTrue(item.IsBusy);
                Assert.IsFalse(item.IsElementDestroyed);


                Assert.AreEqual(10, testInst.Count);
                Assert.AreEqual(9, testInst.AvailableCount);


                testInst.Release(item);
                Assert.IsFalse(item.IsBusy);

                Assert.AreEqual(10, testInst.Count);
                Assert.AreEqual(10, testInst.AvailableCount);
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Exemple #4
0
        private void RunComplexTest(PrioritizedElementsContainer <int> testInst, int threadCount, int opCount, int pauseSpin)
        {
            Assert.AreEqual(testInst.AvailableCount, testInst.Count);

            Thread[] threads  = new Thread[threadCount];
            Barrier  startBar = new Barrier(threadCount + 1);

            int opCountPerThread = opCount / threadCount;

            Action thAct = () =>
            {
                startBar.SignalAndWait();
                TestContext.WriteLine("Inside thread. Signal and wait passed");

                try
                {
                    int execOp = 0;
                    while (execOp++ < opCountPerThread)
                    {
                        PoolElementWrapper <int> item = null;
                        try
                        {
                            item = testInst.Take();
                            //Thread.Sleep(pauseSpin);
                            SpinWaitHelper.SpinWait(pauseSpin);
                        }
                        finally
                        {
                            testInst.Release(item);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestContext.WriteLine("Unhandled exception: " + ex.ToString());
                    throw;
                }
            };


            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(new ThreadStart(thAct));
            }

            TestContext.WriteLine("Threads created");

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Start();
            }

            TestContext.WriteLine("Threads started");

            startBar.SignalAndWait();

            TestContext.WriteLine("Threads before join");

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            TestContext.WriteLine("All threads stopped");

            Assert.AreEqual(testInst.AvailableCount, testInst.Count);
        }