Ejemplo n.º 1
0
        /// <summary>
        /// <see cref="BalancingDynamicPoolManager{TElem}"/> constructor
        /// </summary>
        /// <param name="minElementCount">Minimum number of elements that should always be in pool</param>
        /// <param name="maxElementCount">Maximum number of elements in pool</param>
        /// <param name="name">Name for the current <see cref="DynamicPoolManager{TElem}"/> instance</param>
        /// <param name="trimPeriod">Period in milliseconds to estimate the load level and to reduce the number of elements in accordance with it (-1 - do not preform contraction)</param>
        /// <param name="getRetryTimeout">Interval in milliseconds between attempts to create a new element (used when a new element fails to create)</param>
        public BalancingDynamicPoolManager(int minElementCount, int maxElementCount, string name, int trimPeriod, int getRetryTimeout)
        {
            if (minElementCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minElementCount));
            }
            if (maxElementCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxElementCount));
            }
            if (maxElementCount < minElementCount)
            {
                throw new ArgumentException($"{nameof(maxElementCount)} cannot be less than minElementCount", nameof(maxElementCount));
            }
            if (getRetryTimeout <= 0)
            {
                throw new ArgumentException($"{nameof(getRetryTimeout)} should be positive", nameof(getRetryTimeout));
            }

            _name            = name ?? this.GetType().GetCSFullName();
            _minElementCount = minElementCount;
            _maxElementCount = maxElementCount;
            _getRetryTimeout = getRetryTimeout;
            _trimPeriod      = trimPeriod > 0 ? trimPeriod : int.MaxValue;

            _reservedCount = 0;

            _usedElementTracker  = new UsedElementTracker(_trimPeriod);
            _elementsContainer   = new PrioritizedElementsContainer <TElem>(new ElementComparer(this));
            _stoppedEvent        = new ManualResetEventSlim(false);
            _disposeCancellation = new CancellationTokenSource();

            Profiling.Profiler.ObjectPoolCreated(this.Name);
        }
        public void TestInitialPreset()
        {
            UsedElementTracker testInst = new UsedElementTracker(1000);

            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);
            Assert.AreEqual(0, testInst.ElementToDestroy);
        }
        public void TestResetWorks()
        {
            UsedElementTracker testInst = new UsedElementTracker(1000);

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

            testInst.UpdateMinFreeElementCount(100);
            Assert.AreEqual(100, testInst.MinFreeElementsCount);

            testInst.Reset();
            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);
        }
        public void TestItemFixationWorks()
        {
            UsedElementTracker testInst = new UsedElementTracker(10);

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

            testInst.UpdateMinFreeElementCount(100);
            Assert.AreEqual(100, testInst.MinFreeElementsCount);

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

            Assert.AreEqual(100, testInst.ElementToDestroy);
            Assert.AreEqual(int.MaxValue, testInst.MinFreeElementsCount);
        }
        public void TestMinFreeElementChanging()
        {
            UsedElementTracker testInst = new UsedElementTracker(1000);

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

            testInst.UpdateMinFreeElementCount(100);
            Assert.AreEqual(100, testInst.MinFreeElementsCount);

            testInst.UpdateMinFreeElementCount(200);
            Assert.AreEqual(100, testInst.MinFreeElementsCount);

            testInst.UpdateMinFreeElementCount(1);
            Assert.AreEqual(1, testInst.MinFreeElementsCount);
        }
        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());
        }