예제 #1
0
        public void PoolFlushTimout()
        {
            var pool = new ThirdPartyPool(0, 2);

            pool.FlushTimeOut = 1;

            var insance1 = pool.TakeInstance();
            var insance2 = pool.TakeInstance();

            // вернули только один
            pool.Release(insance1);
            ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush());

            var pool2 = new ThirdPartyPool(0, 2);

            pool.FlushTimeOut = 1;

            var insance = pool.TakeInstance();

            pool.Release(insance1);

            Action <ThirdParty> longTimeDestroy = obj =>
            {
                Thread.Sleep(35000);
            };

            // слишком длительная очистка объекта
            ThrowsAssert.Throws <TimeoutException>(() => pool.TryFlush(longTimeDestroy));
        }
예제 #2
0
        public void PoolOneThreadScenario()
        {
            const int iterations   = 100;
            const int initialCount = 5;

            var pool = new ThirdPartyPool(initialCount, 50);
            var item = pool.TakeInstance();

            pool.Release(item);
            Assert.AreEqual(initialCount, pool.TotalCount);
            Assert.AreEqual(initialCount, pool.CurrentCount);

            ThrowsAssert.Throws <ArgumentException>(() => pool.Release(new ThirdPartyPool(1, 1).TakeInstance()));
            ThrowsAssert.Throws <InvalidOperationException>(() => pool.Release(item));

            for (var i = 0; i < iterations; i++)
            {
                using (var slot = pool.TakeInstance())
                {
                    Assert.IsFalse(slot.Object.Flag);
                    slot.Object.Flag = true;
                    Assert.AreEqual(initialCount, pool.TotalCount);
                    Assert.AreEqual(initialCount - 1, pool.CurrentCount);
                }
            }
            Assert.AreEqual(initialCount, pool.TotalCount);
            Assert.AreEqual(initialCount, pool.CurrentCount);
        }
예제 #3
0
        public void PoolFlushing()
        {
            var pool         = new ThirdPartyPool(0, 3);
            var insanceArray = new List <PoolInstance <ThirdParty> >();

            insanceArray.Add(pool.TakeInstance());
            insanceArray.Add(pool.TakeInstance());
            insanceArray.Add(pool.TakeInstance());
            Assert.AreEqual(3, pool.TotalCount);

            insanceArray.ForEach(insance => pool.Release(insance));

            pool.TryFlush();
            insanceArray.Clear();
            Assert.IsTrue(insanceArray.All(inst => inst.Object.Disposed));
            Assert.AreEqual(0, pool.TotalCount);

            // recycle pool and manual flush
            insanceArray.Add(pool.TakeInstance());
            insanceArray.Add(pool.TakeInstance());

            Assert.AreEqual(2, pool.TotalCount);
            insanceArray.ForEach(insance => pool.Release(insance));

            Action <ThirdParty> destroyer = obj =>
            {
                obj.ManualDisposed = true;
            };

            pool.TryFlush(destroyer);

            Assert.IsTrue(insanceArray.All(inst => inst.Object.ManualDisposed));
            Assert.AreEqual(0, pool.TotalCount);
        }
예제 #4
0
        public void PoolCreation()
        {
            var pool = new ThirdPartyPool(0, 3);

            pool.TakeInstance();
            pool.TakeInstance();
            pool.TakeInstance();
            Assert.AreEqual(3, pool.TotalCount);

            pool = new ThirdPartyPool(100, 100);
            Assert.AreEqual(100, pool.TotalCount);
            Assert.AreEqual(100, pool.CurrentCount);
        }
예제 #5
0
        public void PoolReduceTotalCount()
        {
            var pool = new ThirdPartyPool(100, 100);

            Assert.IsTrue(pool.TryReduceTotal(10));
            Assert.AreEqual(10, pool.TotalCount);

            var item = pool.TakeInstance();

            Assert.IsFalse(pool.TryReduceTotal(0));
            Assert.AreEqual(1, pool.TotalCount);
        }