Пример #1
0
        public void OneThreadScenario()
        {
            const int iterations   = 100;
            const int initialCount = 5;

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

            pool.Release(item);
            Assert.AreEqual(initialCount, pool.TotalCount);
            Assert.AreEqual(initialCount, pool.CurrentCount);
            Assert.Throws <ArgumentException>(() => pool.Release(new ThirdPartyPool(1, 1).TakeSlot()));
            Assert.Throws <InvalidOperationException>(() => pool.Release(item));

            for (var i = 0; i < iterations; i++)
            {
                using (var slot = pool.TakeSlot())
                {
                    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);
        }
Пример #2
0
        public void Creation()
        {
            var pool = new ThirdPartyPool(0, 3);

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

            pool = new ThirdPartyPool(100, 100);
            Assert.AreEqual(100, pool.TotalCount);
            Assert.AreEqual(100, pool.CurrentCount);
        }
Пример #3
0
        public void ReduceTotalCount()
        {
            var pool = new ThirdPartyPool(100, 100);

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

            var item = pool.TakeSlot();

            Assert.IsFalse(pool.TryReduceTotal(0));
            Assert.AreEqual(1, pool.TotalCount);
        }
Пример #4
0
        private static void ManyThreadsWithPool(int iterCount, int taskCount)
        {
            var tasks = new Task[taskCount];
            var pool = new ThirdPartyPool(1024, 0, 10);

            for (var t = 0; t < taskCount; t++)
            {
                tasks[t] = Task.Factory.StartNew(
                    () =>
                        {
                            for (var i = 0; i < iterCount; i++)
                            {
                                using (var slot = pool.TakeSlot())
                                {
                                }
                            }
                        });
            }
            Task.WaitAll(tasks);
        }
Пример #5
0
 private static void OneThreadWithPool(int iterCount)
 {
     var pool = new ThirdPartyPool(1024, 0, 10);
     for (var i = 0; i < iterCount; i++)
     {
         using (var slot = pool.TakeSlot())
         {
         }
     }
 }