コード例 #1
0
        public void GetPoolObjectNoGrowth_WithDispose()
        {
            var pool = new ObjectPool <TestPoolObject>(5, false);

            // Get 2 objects
            TestPoolObject obj1 = pool.GetObject();

            Assert.IsNotNull(obj1);
            TestPoolObject obj2 = pool.GetObject();

            Assert.IsNotNull(obj2);
            // => 3 objects remaining in the pool

            // Release one => get back into the pool
            obj1.Dispose();
            // => 4 objects remaining in the pool
            obj1.CheckResets(1);
            obj1.CheckTerminates(0);
            obj2.CheckResets(0);
            obj2.CheckTerminates(0);

            for (int i = 0; i < 4; ++i)
            {
                Assert.IsNotNull(pool.GetObject());
            }

            // No more object in the pool
            TestPoolObject obj = pool.GetObject();

            Assert.IsNull(obj);
        }
コード例 #2
0
        public void GetPoolObjectGrowth_WithDispose()
        {
            var pool = new ObjectPool <TestPoolObject>(5, true);

            // Get 2 objects
            TestPoolObject obj1 = pool.GetObject();

            Assert.IsNotNull(obj1);
            TestPoolObject obj2 = pool.GetObject();

            Assert.IsNotNull(obj2);
            // => 3 objects remaining in the pool

            // Release one => get back into the pool
            obj1.Dispose();
            // => 4 objects remaining in the pool
            obj1.CheckResets(1);
            obj1.CheckTerminates(0);
            obj2.CheckResets(0);
            obj2.CheckTerminates(0);

            TestPoolObject[] objects = Enumerable.Range(0, 4)
                                       .Select(_ =>
            {
                TestPoolObject obj = pool.GetObject();
                Assert.IsNotNull(obj);
                return(obj);
            })
                                       .ToArray();
            // => 0 object remaining in the pool

            // Object from pool growth
            TestPoolObject obj6 = pool.GetObject();

            Assert.IsNotNull(obj6);

            // Release 5 objects => get back into the pool
            objects[0].Dispose();
            objects[1].Dispose();
            objects[3].Dispose();
            obj6.Dispose();
            obj2.Dispose();

            foreach (TestPoolObject obj in new[] { objects[0], objects[1], objects[3], obj6, obj2 })
            {
                obj.CheckResets(1);
                obj.CheckTerminates(0);
            }
            objects[2].CheckResets(0);
            objects[2].CheckTerminates(0);
            // => 5 objects remaining in the pool

            // Release 6th object => it is terminated
            objects[2].Dispose();
            objects[2].CheckResets(0);
            objects[2].CheckTerminates(1);
        }
コード例 #3
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);
        }
コード例 #4
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());
        }
コード例 #5
0
        public void GetPoolObjectGrowth(int nbObjects)
        {
            var pool = new ObjectPool <TestPoolObject>(nbObjects, true);

            for (int i = 0; i < nbObjects; ++i)
            {
                TestPoolObject obj = pool.GetObject();
                Assert.IsNotNull(obj);
            }

            TestPoolObject objNPlus1 = pool.GetObject();

            Assert.IsNotNull(objNPlus1);
            TestPoolObject objNPlus2 = pool.GetObject();

            Assert.IsNotNull(objNPlus2);
        }