예제 #1
0
        private void RunComplexTest(StaticPoolManager <int> testInst, int threadCount, int opCount, int pauseSpin, bool autoAddRemove)
        {
            Thread[] threads  = new Thread[threadCount];
            Barrier  startBar = new Barrier(threadCount + 1);

            int opCountPerThread = opCount / threadCount;

            Action thAct = () =>
            {
                Random localRand = new Random(Thread.CurrentThread.ManagedThreadId + Environment.TickCount);
                int    pasueDiff = pauseSpin / 4;

                startBar.SignalAndWait();

                int execOp = 0;
                while (execOp++ < opCountPerThread)
                {
                    if (autoAddRemove && (testInst.ElementCount == 0 || localRand.Next(5) == 0))
                    {
                        testInst.AddElement(execOp);
                    }

                    using (var el = testInst.Rent())
                    {
                        if (autoAddRemove && testInst.ElementCount > 1 && localRand.Next(5) == 0)
                        {
                            testInst.RemoveElement(el);
                        }

                        int spinCount = localRand.Next(pauseSpin - pasueDiff, pauseSpin + pasueDiff);
                        Thread.SpinWait(spinCount);
                    }
                }

                if (autoAddRemove)
                {
                    testInst.AddElement(execOp);
                }
            };


            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();

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

            Assert.AreEqual(testInst.ElementCount, testInst.FreeElementCount);
        }
예제 #2
0
        public void TestRemoveElement()
        {
            using (StaticPoolManager <int> testInst = new StaticPoolManager <int>())
            {
                Assert.AreEqual(0, testInst.ElementCount);
                Assert.AreEqual(0, testInst.FreeElementCount);

                for (int i = 0; i < 10; i++)
                {
                    testInst.AddElement(i);
                }

                Assert.AreEqual(10, testInst.ElementCount);
                Assert.AreEqual(10, testInst.FreeElementCount);

                for (int i = 0; i < 10; i++)
                {
                    using (var el = testInst.Rent())
                    {
                        Assert.IsTrue(el.IsValid);
                        testInst.RemoveElement(el);
                        Assert.IsFalse(el.IsValid);
                    }

                    Assert.AreEqual(10 - i - 1, testInst.ElementCount);
                    Assert.AreEqual(10 - i - 1, testInst.FreeElementCount);
                }
            }
        }
예제 #3
0
        public void TestDestroyCalledOnRemove()
        {
            int          destroyed  = 0;
            Action <int> destroyAct = a =>
            {
                Interlocked.Increment(ref destroyed);
            };

            using (StaticPoolManager <int> testInst = new StaticPoolManager <int>("a", destroyAct))
            {
                Assert.AreEqual(0, testInst.ElementCount);
                Assert.AreEqual(0, testInst.FreeElementCount);

                for (int i = 0; i < 10; i++)
                {
                    testInst.AddElement(i);
                }

                Assert.AreEqual(10, testInst.ElementCount);
                Assert.AreEqual(10, testInst.FreeElementCount);

                for (int i = 0; i < 10; i++)
                {
                    using (var el = testInst.Rent())
                    {
                        Assert.IsTrue(el.IsValid);
                        testInst.RemoveElement(el);
                        Assert.IsFalse(el.IsValid);
                    }

                    Assert.AreEqual(i + 1, destroyed);
                }

                Assert.AreEqual(10, destroyed);
                Assert.AreEqual(0, testInst.ElementCount);
            }
        }