Example #1
0
        public void Cull_ReducesObjectCount()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            int count             = 9;
            int reduced           = count - 1;
            List <TestClass> list = new List <TestClass>();

            for (int i = 0; i < count; i++)
            {
                list.Add(PPocoPool.Get <TestClass>());
            }
            for (int i = 0; i < count; i++)
            {
                PPocoPool.Put(list[0]);
            }
            list.Clear();

            //  act
            PPocoPool.SetLimit <TestClass>(poolSize: reduced);
            int available = PPocoPool.GetAvailable <TestClass>();

            //  assert
            Assert.AreEqual(reduced, available, "Available objects does not match expected count");
        }
Example #2
0
        public void GetInUse_IndicatesUsedQuantity()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            int count = 3;

            TestClass[] gos = new TestClass[count];

            //  act
            for (int i = 0; i < count; i++)
            {
                gos[i] = PPocoPool.Get <TestClass>();
            }
            int resultForUsed = PPocoPool.GetInUse <TestClass>();

            for (int i = 0; i < count; i++)
            {
                PPocoPool.Put(gos[i]);
            }
            int resultForReclaimed = PPocoPool.GetInUse <TestClass>();

            //  assert
            Assert.AreEqual(count, resultForUsed, "Pool has wrong number of active objects");
            Assert.AreEqual(0, resultForReclaimed, "Pool has wrong number of active objects");
        }
Example #3
0
        public void GetAvailable_IndicatesUnusedQuantity()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            int count = 3;

            TestClass[] gos = new TestClass[count];

            //  act
            for (int i = 0; i < count; i++)
            {
                gos[i] = PPocoPool.Get <TestClass>();
            }
            int resultForEmpty = PPocoPool.GetAvailable <TestClass>();

            for (int i = 0; i < count; i++)
            {
                PPocoPool.Put(gos[i]);
            }
            int resultForReclaimed = PPocoPool.GetAvailable <TestClass>();

            //  assert
            Assert.AreEqual(0, resultForEmpty, "Pool not empty");
            Assert.AreEqual(count, resultForReclaimed, "Pool missing reclaimed objects");
        }
Example #4
0
        public void Put_ReturnsObjectToPool()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            TestClass instance  = PPocoPool.Get <TestClass>();
            int       available = PPocoPool.GetAvailable <TestClass>();

            //  act
            bool putResult = PPocoPool.Put(instance);
            int  result    = PPocoPool.GetAvailable <TestClass>();

            //  assert
            Assert.IsTrue(putResult, "Pool indicated object was not reclaimed");
            Assert.AreEqual(available + 1, result, "Available object count incorrect");
        }
Example #5
0
        public void Get_EmptyPoolInstantiatesObject()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            TestClass instance;

            //  act
            instance = PPocoPool.Get <TestClass>();
            bool uniqueHash = prefab.GetHashCode() != instance.GetHashCode();

            //  assert
            Assert.IsNotNull(prefab, "Prefab is null");
            Assert.IsNotNull(instance, "Instance is null");
            Assert.AreNotSame(prefab, instance, "Prefab and instance are the same object");
            Assert.IsTrue(uniqueHash, "Prefab and Instanced object have same hashCode {0}", prefab.GetHashCode());
        }
Example #6
0
        public void Get_PoolProvidesExistingObject()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            TestClass first, second;
            int       firstHash, secondHash;

            //  act
            first     = PPocoPool.Get <TestClass>();
            firstHash = first.GetHashCode();
            PPocoPool.Put(first);

            second     = PPocoPool.Get <TestClass>();
            secondHash = second.GetHashCode();

            //  assert
            Assert.AreEqual(firstHash, secondHash, "Hashcodes do not match");
        }
Example #7
0
        public void SetLimit_AssignsPersistenceLimit()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            float duration = 0f;

            PPocoPool.SetLimit <TestClass>(staleDuration: duration);

            //  act
            TestClass instance = PPocoPool.Get <TestClass>();

            PPocoPool.Put(instance);
            //  Using a duration of '0' should trigger the time-based culling.
            int available = PPocoPool.GetAvailable <TestClass>();

            //  assert
            Assert.AreEqual(0, available, "Pool kept objects past expiration");
        }
Example #8
0
        public void SetLimit_AssignsCreationLimit()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //  arrange
            int limit = 3;

            PPocoPool.SetLimit <TestClass>(poolSize: limit);

            //  act
            TestClass[] g = new TestClass[limit + 1];
            for (int i = 0; i < limit + 1; i++)
            {
                g[i] = PPocoPool.Get <TestClass>();
            }

            //  assert
            Assert.IsNull(g[limit], "Pool created objects in excess of assigned limit");
        }
Example #9
0
        public void Get_ReturnsNullWhenAtMaxLimit()
        {
            //  setup
            PPocoPool.Clear <TestClass>();

            //	arrange
            int count          = 9;
            List <TestClass> g = new List <TestClass>();

            PPocoPool.SetLimit <TestClass>(poolSize: count);

            //	act
            for (int i = 0; i < count + 1; i++)
            {
                TestClass go = PPocoPool.Get <TestClass>();
                if (go != null)
                {
                    g.Add(go);
                }
            }

            //	assert
            Assert.AreEqual(count, g.Count, "Pool created wrong number of objects");
        }