コード例 #1
0
        public void GrowCapacity()
        {
            NetworkReaderPool.Capacity = 1;

            // create and recycle one
            PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            NetworkReaderPool.Recycle(a);

            // grow capacity
            NetworkReaderPool.Capacity = 2;

            // get two
            PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>));
            PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            Assert.That(b, !Is.Null);
            Assert.That(c, !Is.Null);

            // exactly one should be reused, one should be new
            bool bReused = b == a;
            bool cReused = c == a;

            Assert.That((bReused && !cReused) ||
                        (!bReused && cReused));
        }
コード例 #2
0
        public void ShrinkCapacity()
        {
            NetworkReaderPool.Capacity = 2;

            // get Reader and recycle so we have 2 in there, hence 'next' is at limit
            PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>));
            PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            NetworkReaderPool.Recycle(a);
            NetworkReaderPool.Recycle(b);

            // shrink
            NetworkReaderPool.Capacity = 1;

            // get one. should return the only one which is still in there.
            PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            Assert.That(c, !Is.Null);
            Assert.That(c == a || c == b);
        }
コード例 #3
0
        public void PoolReUsesReadersUpToSizeLimit()
        {
            NetworkReaderPool.Capacity = 1;

            // get 2 Readers
            PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>));
            PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            // recycle all
            NetworkReaderPool.Recycle(a);
            NetworkReaderPool.Recycle(b);

            // get 2 new ones
            PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>));
            PooledNetworkReader d = NetworkReaderPool.GetReader(default(ArraySegment <byte>));

            // exactly one should be reused, one should be new
            bool cReused = c == a || c == b;
            bool dReused = d == a || d == b;

            Assert.That((cReused && !dReused) ||
                        (!cReused && dReused));
        }
コード例 #4
0
 public void Dispose()
 {
     NetworkReaderPool.Recycle(this);
 }