Esempio n. 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());
            }
        }
Esempio n. 2
0
        public void TestRescanWorks()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

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

                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
                testInst.RescanContainer();

                Assert.AreEqual(0, testInst.Count);
                Assert.AreEqual(0, testInst.AvailableCount);
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 3
0
        public void TestRemoveOnTake()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

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

                testInst.ProcessAllElements(o => o.MarkElementDestroyed());

                PoolElementWrapper <int> item;
                bool takeResult = testInst.TryTake(out item, 0, new CancellationToken());
                Assert.IsFalse(takeResult);

                Assert.AreEqual(0, testInst.Count);
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 4
0
        public void TestTakeByPriority()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

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

                List <PoolElementWrapper <int> > takenElems = new List <PoolElementWrapper <int> >();
                PoolElementWrapper <int>         item;

                for (int i = 0; i < 100; i++)
                {
                    bool takeRes = testInst.TryTake(out item, 0, new CancellationToken());
                    takenElems.Add(item);

                    Assert.IsTrue(takeRes);
                    Assert.IsNotNull(item);
                    Assert.IsTrue(item.IsBusy);
                }


                for (int i = 0; i < takenElems.Count - 1; i++)
                {
                    Assert.IsTrue(takenElems[i].Element > takenElems[i + 1].Element);
                }


                for (int i = 0; i < takenElems.Count; i++)
                {
                    testInst.Release(takenElems[i]);
                }
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Pool clean-up (core method)
        /// </summary>
        /// <param name="waitForRelease">Should it wait for full completion</param>
        private void DisposePool(bool waitForRelease)
        {
            if (!_disposeCancellation.IsCancellationRequested)
            {
#if DEBUG
                _elementsContainer.ProcessAllElements(o => o.SetPoolDisposed());
#endif

                _disposeCancellation.Cancel();

                try { }
                finally
                {
                    int count = _elementsContainer.Count;
                    while (TakeDestroyAndRemoveElement())
                    {
                        TurboContract.Assert(--count >= 0, conditionString: "--count >= 0");
                    }

                    if (_elementsContainer.Count == 0)
                    {
                        _stoppedEvent.Set();
                    }
                }

                Profiling.Profiler.ObjectPoolDisposed(this.Name, false);
            }
            else
            {
                if (_elementsContainer.Count == 0)
                {
                    _stoppedEvent.Set();
                }
            }

            if (waitForRelease)
            {
                this.WaitUntilStop();
            }
        }
Esempio n. 6
0
        public void ComplexTest()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

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

                TestContext.WriteLine("ComplexTest started");

                RunComplexTest(testInst, Environment.ProcessorCount, 100000, 4);

                TestContext.WriteLine("ComplexTest phase 1 finished");

                for (int i = testInst.Count; i < Environment.ProcessorCount; i++)
                {
                    testInst.Add(i, new PoolOperations(), true);
                }

                RunComplexTest(testInst, Environment.ProcessorCount, 1000000, 4);

                TestContext.WriteLine("ComplexTest phase 2 finished");

                for (int i = testInst.Count; i < 2 * Environment.ProcessorCount; i++)
                {
                    testInst.Add(i, new PoolOperations(), true);
                }

                RunComplexTest(testInst, Environment.ProcessorCount, 1000000, 4);

                TestContext.WriteLine("ComplexTest phase 3 finished");
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 7
0
        public void TestAddElementAndNotMakeAvailable()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

            try
            {
                Assert.AreEqual(0, testInst.Count);
                Assert.AreEqual(0, testInst.AvailableCount);

                var wrapper = testInst.Add(100, new PoolOperations(), false);
                Assert.IsNotNull(wrapper);
                Assert.IsTrue(wrapper.IsBusy);
                Assert.AreEqual(100, wrapper.Element);

                Assert.AreEqual(1, testInst.Count);
                Assert.AreEqual(0, testInst.AvailableCount);
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 8
0
        public void TestTakeUntilEmpty()
        {
            PrioritizedElementsContainer <int> testInst = new PrioritizedElementsContainer <int>(new PoolElementComparer());

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

                List <PoolElementWrapper <int> > takenElems = new List <PoolElementWrapper <int> >();
                PoolElementWrapper <int>         item;

                for (int i = 0; i < 10; i++)
                {
                    bool takeRes = testInst.TryTake(out item, 0, new CancellationToken());
                    takenElems.Add(item);

                    Assert.IsTrue(takeRes);
                    Assert.IsNotNull(item);
                    Assert.IsTrue(item.IsBusy);
                }

                Assert.AreEqual(0, testInst.AvailableCount);

                bool takeResO = testInst.TryTake(out item, 0, new CancellationToken());
                Assert.IsFalse(takeResO);

                for (int i = 0; i < takenElems.Count; i++)
                {
                    testInst.Release(takenElems[i]);
                }
            }
            finally
            {
                testInst.ProcessAllElements(o => o.MarkElementDestroyed());
            }
        }
Esempio n. 9
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());
            }
        }