Пример #1
0
        public void TestBorrow_BlockUntilAvailable_Exceeds_Timeout()
        {
            const int maximum = 1;

            using (var target =
                       new StandardObjectPool <PooledObject>(
                           () => new PooledObject(),
                           new ObjectPoolConfiguration()
            {
                BorrowTimeout = TimeSpan.FromTicks(1),
                EvitionInterval = null,
                ExhausionPolicy = ExhausionPolicy.BlockUntilAvailable,
                MaximumPooled = maximum,
                MinimumReserved = 1
            }
                           ))
            {
                var result = new List <PooledObject>();
                try
                {
                    for (int i = 0; i < maximum + 1; i++)
                    {
                        result.Add(target.Borrow());
                    }
                }
                finally
                {
                    result.DisposeAll();
                }
            }
        }
Пример #2
0
        public void TestBorrow_CanUpToMaximum()
        {
            const int maximum = 3;

            using (var target =
                       new StandardObjectPool <PooledObject>(
                           () => new PooledObject(),
                           new ObjectPoolConfiguration()
            {
                BorrowTimeout = TimeSpan.FromSeconds(3),
                EvitionInterval = null,
                ExhausionPolicy = ExhausionPolicy.ThrowException,
                MaximumPooled = maximum,
                MinimumReserved = 1
            }
                           ))
            {
                var result = new List <PooledObject>();
                try
                {
                    for (int i = 0; i < maximum; i++)
                    {
                        result.Add(target.Borrow());
                    }
                }
                finally
                {
                    result.DisposeAll();
                }
            }
        }
Пример #3
0
        public void TestEviction_Induced_ReducedToHalf()
        {
            const int maximum = 8;

            using (var target =
                       new StandardObjectPool <PooledObject>(
                           () => new PooledObject(),
                           new ObjectPoolConfiguration()
            {
                Name = "Pool",
                BorrowTimeout = null,
                EvitionInterval = TimeSpan.FromHours(1),
                ExhausionPolicy = ExhausionPolicy.ThrowException,
                MaximumPooled = maximum,
                MinimumReserved = 1
            }
                           ))
            {
                var items = new List <PooledObject>();
                try
                {
                    for (int i = 0; i < maximum; i++)
                    {
                        items.Add(target.Borrow());
                    }

                    foreach (var item in items)
                    {
                        target.Return(item);
                    }

                    Assert.That(target.LeasedCount, Is.EqualTo(maximum));
                    target.EvictExtraItems();
                    Assert.That(target.LeasedCount, Is.EqualTo(maximum / 2));
                    target.EvictExtraItems();
                    Assert.That(target.LeasedCount, Is.EqualTo(maximum / 4));
                    target.EvictExtraItems();
                    Assert.That(target.LeasedCount, Is.EqualTo(maximum / 8));
                    target.EvictExtraItems();
                    Assert.That(target.LeasedCount, Is.EqualTo(maximum / 8));
                }
                finally
                {
                    items.DisposeAll();
                }
            }
        }
Пример #4
0
        public void TestEviction_Timer_ReducedAtLeast()
        {
            const int maximum = 2;

            using (var target =
                       new StandardObjectPool <PooledObject>(
                           () => new PooledObject(),
                           new ObjectPoolConfiguration()
            {
                Name = "Pool",
                BorrowTimeout = null,
                EvitionInterval = TimeSpan.FromMilliseconds(500),
                ExhausionPolicy = ExhausionPolicy.ThrowException,
                MaximumPooled = maximum,
                MinimumReserved = 1
            }
                           ))
            {
                var items = new List <PooledObject>();
                try
                {
                    for (int i = 0; i < maximum; i++)
                    {
                        items.Add(target.Borrow());
                    }

                    foreach (var item in items)
                    {
                        target.Return(item);
                    }

                    Assert.That(target.LeasedCount, Is.EqualTo(maximum));

                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));

                    Assert.That(target.LeasedCount, Is.LessThan(maximum));
                }
                finally
                {
                    items.DisposeAll();
                }
            }
        }
Пример #5
0
        public void TestEviction_MinimumIsNotZero_PooledIsZero_Refilled()
        {
            const int minimum = 1;
            const int maximum = 2;

            using (var target =
                       new StandardObjectPool <PooledObject>(
                           () => new PooledObject(),
                           new ObjectPoolConfiguration()
            {
                BorrowTimeout = null,
                EvitionInterval = TimeSpan.FromMilliseconds(500),
                ExhausionPolicy = ExhausionPolicy.ThrowException,
                MaximumPooled = maximum,
                MinimumReserved = minimum
            }
                           ))
            {
                var items = new List <PooledObject>();
                try
                {
                    for (int i = 0; i < maximum; i++)
                    {
                        items.Add(target.Borrow());
                    }

                    Assert.That(target.PooledCount, Is.EqualTo(0));
                    target.EvictExtraItems();
                    Assert.That(target.PooledCount, Is.EqualTo(0));
                }
                finally
                {
                    items.DisposeAll();
                }
            }
        }