コード例 #1
0
        public void TestGetRelease_NonBucketSizes()
        {
            _pool = new TestPool(10, 10, MakeBucketSizeArray(2, 1, 4, 1, 6, 1));
            _stats.SetPool(_pool);

            _pool.Get(2);
            byte[] b1 = _pool.Get(7);
            _stats.Refresh();
            Assert.AreEqual(10, _stats.UsedBytes);
            Assert.AreEqual(2, _stats.UsedCount);
            _pool.Release(b1);
            _stats.Refresh();
            Assert.AreEqual(2, _stats.UsedBytes);
            Assert.AreEqual(1, _stats.UsedCount);
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(0, _stats.FreeCount);

            byte[] b2 = new byte[3];
            _pool.Release(b2);
            _stats.Refresh();
            Assert.AreEqual(2, _stats.UsedBytes);
            Assert.AreEqual(1, _stats.UsedCount);
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(0, _stats.FreeCount);
        }
コード例 #2
0
        public void TestRelease_BucketLengths()
        {
            _pool = new TestPool(int.MaxValue, int.MaxValue, MakeBucketSizeArray(2, 2));
            _stats.SetPool(_pool);

            byte[] b0 = _pool.Get(2);
            _pool.Get(2);
            _pool.Get(2);
            _stats.Refresh();
            var testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(3, 0) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));
            Assert.AreEqual(6, _stats.UsedBytes);
            Assert.AreEqual(3, _stats.UsedCount);
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(0, _stats.FreeCount);

            // Now release one of the buffers
            _pool.Release(b0);
            _stats.Refresh();
            testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(2, 0) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));
            Assert.AreEqual(4, _stats.UsedBytes);
            Assert.AreEqual(2, _stats.UsedCount);
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(0, _stats.FreeCount);
        }
コード例 #3
0
        public void Test_CanAllocate()
        {
            TestPool pool = new TestPool(4, 8);

            pool.Get(4);
            Assert.IsFalse(pool.IsMaxSizeSoftCapExceeded());
            Assert.IsTrue(pool.CanAllocate(2));
            pool.Get(2);
            Assert.IsTrue(pool.IsMaxSizeSoftCapExceeded());
            Assert.IsTrue(pool.CanAllocate(2));
            Assert.IsFalse(pool.CanAllocate(4));
        }
コード例 #4
0
        public void TestTrimToSize()
        {
            _pool = new TestPool(100, 100, MakeBucketSizeArray(2, 2, 4, 2, 6, 2));
            _stats.SetPool(_pool);

            // Allocate and release multiple buffers
            byte[] b1;
            _pool.Get(2);
            b1 = _pool.Get(2);
            _pool.Release(b1);
            b1 = _pool.Get(6);
            _pool.Release(b1);
            b1 = _pool.Get(4);
            _pool.Release(b1);

            _stats.Refresh();
            Assert.AreEqual(12, _stats.FreeBytes);
            Assert.AreEqual(2, _stats.UsedBytes);

            // Perform a dummy trim - nothing should happen
            _pool.TrimToSize(100);
            _stats.Refresh();
            Assert.AreEqual(12, _stats.FreeBytes);
            Assert.AreEqual(2, _stats.UsedBytes);

            // Now perform the real trim
            _pool.TrimToSize(8);
            _stats.Refresh();
            Assert.AreEqual(6, _stats.FreeBytes);
            Assert.AreEqual(2, _stats.UsedBytes);
            var testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(1, 0) },
                { 4, new Tuple <int, int>(0, 0) },
                { 6, new Tuple <int, int>(0, 1) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));

            // Perform another trim
            _pool.TrimToSize(1);
            _stats.Refresh();
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(2, _stats.UsedBytes);
            testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(1, 0) },
                { 4, new Tuple <int, int>(0, 0) },
                { 6, new Tuple <int, int>(0, 0) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));
        }
コード例 #5
0
        public void TestGet_AllocFailure()
        {
            TestPool pool = new TestPool(4, 5);

            pool.Get(4);
            try
            {
                pool.Get(4);
                Assert.Fail();
            }
            catch (PoolSizeViolationException)
            {
                // This is expected
            }
        }
コード例 #6
0
        public void TestGet_AllocAndTrim()
        {
            _pool = new TestPool(10, 10, MakeBucketSizeArray(2, 2, 4, 2, 6, 2));
            _stats.SetPool(_pool);

            // Allocate and release multiple buffers
            byte[] b1 = _pool.Get(2);
            _pool.Release(b1);
            b1 = _pool.Get(6);
            _pool.Release(b1);

            // Get current stats
            _stats.Refresh();
            var testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(0, 1) },
                { 4, new Tuple <int, int>(0, 0) },
                { 6, new Tuple <int, int>(0, 1) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));

            // Get a new buffer; this should cause an alloc and a trim
            _pool.Get(3);
            _stats.Refresh();

            // Validate stats
            testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 2, new Tuple <int, int>(0, 0) },
                { 4, new Tuple <int, int>(1, 0) },
                { 6, new Tuple <int, int>(0, 1) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));
            Assert.AreEqual(6, _stats.FreeBytes);
            Assert.AreEqual(4, _stats.UsedBytes);
            Assert.AreEqual(1, _stats.FreeCount);
            Assert.AreEqual(1, _stats.UsedCount);
        }
コード例 #7
0
        public void TestRelease_Free2()
        {
            // Create a new pool with a max size cap of zero.
            _pool = new TestPool(0, 10);
            _stats.SetPool(_pool);

            // get a buffer and release it - this should trigger the soft cap
            byte[] b1 = _pool.Get(4);
            _pool.Release(b1);
            _stats.Refresh();
            var testStat = new Dictionary <int, Tuple <int, int> >()
            {
                { 4, new Tuple <int, int>(0, 0) }
            };

            Assert.IsTrue(testStat.All(e => _stats.BucketStats.Contains(e)));
            Assert.AreEqual(0, _stats.FreeBytes);
            Assert.AreEqual(0, _stats.UsedBytes);
            Assert.AreEqual(0, _stats.FreeCount);
            Assert.AreEqual(0, _stats.UsedCount);
        }