Exemple #1
0
        public void TestFactoryAndReset () {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);

            // free it, and make sure it got reset
            pool.Free(e1);
            Assert.IsTrue(e1.value == 0);

            // now allocate again, this time since it's coming from the object pool, 
            // the factory code won't be called
            var e2 = pool.Allocate();
            Assert.IsTrue(e1 == e2);
            Assert.IsTrue(e2.value == 0); // because this is coming from the pool, not factory
        }
        public void TestFactoryAndReset()
        {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);

            // free it, and make sure it got reset
            pool.Free(e1);
            Assert.IsTrue(e1.value == 0);

            // now allocate again, this time since it's coming from the object pool,
            // the factory code won't be called
            var e2 = pool.Allocate();
            Assert.IsTrue(e1 == e2);
            Assert.IsTrue(e2.value == 0); // because this is coming from the pool, not factory
        }
Exemple #3
0
        public void TestPoolRelease () {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 0);

            // allocate and free an instance. this will grow the pool to size one
            pool.Free(pool.Allocate());
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 1);

            // now release the entire object pool. this will clear out the free list, but will not
            // affect the object already in circulation, since we're not tracking them.
            pool.Release();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == -1);
        }
        public void TestPoolRelease()
        {
            var pool = new ObjectPool<TestElement>();
            pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });

            // allocate a new instance using this custom factory, make sure the factory code got called
            var e1 = pool.Allocate();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 0);

            // allocate and free an instance. this will grow the pool to size one
            pool.Free(pool.Allocate());
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == 1);

            // now release the entire object pool. this will clear out the free list, but will not
            // affect the object already in circulation, since we're not tracking them.
            pool.Release();
            Assert.IsTrue(e1.value == 42);
            Assert.IsTrue(pool.UsedListSize == 1);
            Assert.IsTrue(pool.FreeListSize == -1);
        }