Exemple #1
0
        public void BoundedObjectPoolDoesNotExceedMaxSizeForMultiThreadedCallsWithBlockingOperations()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var loopResult = Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);

                //simulate a blocking operation
                if (i % 1000 == 0)
                {
                    Thread.Sleep(100);
                }

                pool.ReturnObjectToPool(item);
            });

            while (!loopResult.IsCompleted ||
                   pool.PoolCount > 10)
            {
                Thread.Sleep(10);
            }

            Assert.IsTrue(pool.PoolCount <= 10);
        }
Exemple #2
0
        public void BoundedObjectPoolResetsObjectStateWhenReturningObjectToPool()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var item = pool.GetObject();

            pool.ReturnObjectToPool(item);

            Assert.IsTrue(item.StateReset);
        }
Exemple #3
0
        public void BoundedObjectPoolReturnObjectToPoolDoesNotExceedMaxSize()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            var item = pool.GetObject();

            pool.ReturnObjectToPool(item);

            Assert.IsTrue(pool.PoolCount <= 10);
        }
Exemple #4
0
        public void BoundedObjectPoolReturnsMultipleObjectsToPoolDoesNotExceedMaxSize()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            //simulate a collection of pool items to be returned
            TestPoolObject[] items = new TestPoolObject[10];
            for (int i = 0; i < items.Length; i++)
            {
                items[i] = objGenerator();
            }
            items.ToList().ForEach(i => pool.ReturnObjectToPool(i));

            Assert.IsTrue(pool.PoolCount <= 10);
        }
Exemple #5
0
        public void BoundedObjectPoolGetsObjectFromPoolMultiThreaded()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);
                pool.ReturnObjectToPool(item);
            });

            Assert.IsTrue(values.All(i => i.HasValue));
        }
Exemple #6
0
        public void BoundedObjectPoolReleasesResourcesForObjectsNotReAddedToPool()
        {
            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            //simulate a collection of pool items to be returned
            TestPoolObject[] items = new TestPoolObject[10];
            for (int i = 0; i < items.Length; i++)
            {
                items[i] = objGenerator();
            }
            items.ToList().ForEach(i => pool.ReturnObjectToPool(i));

            var itemsDestroyed = items.Where(i => i.ResourcesRelated);

            Assert.AreEqual(5, itemsDestroyed.Count());
        }
Exemple #7
0
        public void BoundedObjectPoolGetsObjectFromPoolMultiThreadedWithBlockingOperations()
        {
            double?[] values = new double?[10000];

            Func <TestPoolObject> objGenerator = () => new TestPoolObject();
            var pool = new BoundedObjectPool <TestPoolObject>(objGenerator, 5, 10);

            Parallel.For(0, 10000, i =>
            {
                var item  = pool.GetObject();
                values[i] = item.GetValue(i);

                //simulate a blocking operation
                if (i % 1000 == 0)
                {
                    Thread.Sleep(100);
                }

                pool.ReturnObjectToPool(item);
            });

            Assert.IsTrue(values.All(i => i.HasValue));
        }