public void TestElementToDestroyDecrements()
        {
            UsedElementTracker testInst = new UsedElementTracker(10);

            testInst.UpdateMinFreeElementCount(100);
            Thread.Sleep(100);
            testInst.UpdateState();


            Assert.AreEqual(100, testInst.ElementToDestroy);
            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);

            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(testInst.RequestElementToDestroy());
                Assert.AreEqual(100 - i - 1, testInst.ElementToDestroy);
            }

            Assert.IsFalse(testInst.RequestElementToDestroy());
        }
Esempio n. 2
0
        /// <summary>
        /// Releases element back to the pool. Normally should be called from <see cref="RentedElementMonitor{TElem}"/>
        /// </summary>
        /// <param name="element">Element wrapper to be released</param>
        protected internal sealed override void ReleaseElement(PoolElementWrapper <TElem> element)
        {
            TurboContract.Requires(element != null, conditionString: "element != null");

            if (!element.IsBusy)
            {
                throw new InvalidOperationException("Trying to release same element several times in Pool: " + this.Name);
            }

            _usedElementTracker.UpdateState();

            bool doTrim  = _elementsContainer.Count > _minElementCount && _usedElementTracker.RequestElementToDestroy();
            bool isValid = this.IsValidElement(element.Element);

            if (_disposeCancellation.IsCancellationRequested || !isValid)
            {
                DestroyAndRemoveElement(element);
            }
            else
            {
                _elementsContainer.Release(element);

                if (_disposeCancellation.IsCancellationRequested || doTrim)
                {
                    TakeDestroyAndRemoveElement();
                }
            }

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

            if (!isValid)
            {
                Profiling.Profiler.ObjectPoolElementFaulted(this.Name, this.ElementCount);
            }

            Profiling.Profiler.ObjectPoolElementReleased(this.Name, this.RentedElementCount);
        }