コード例 #1
0
        void TestPrimitiveObjectPoolsDispose()
        {
            PrimitiveObjectPool <TestObjectPoolItem>          primPool   = new PrimitiveObjectPool <TestObjectPoolItem>(1);
            ResizablePrimitiveObjectPool <TestObjectPoolItem> resizePool = new ResizablePrimitiveObjectPool <TestObjectPoolItem>(1);
            BlockingPrimitiveObjectPool <TestObjectPoolItem>  blockPool  = new BlockingPrimitiveObjectPool <TestObjectPoolItem>(1);

            primPool.Dispose();
            resizePool.Dispose();
            blockPool.Dispose();

            Assert.Throws <ObjectDisposedException>(() => primPool.InUse);
            Assert.Throws <ObjectDisposedException>(() => primPool.PoolCount);
            Assert.Throws <ObjectDisposedException>(() => primPool.Size);
            Assert.Throws <ObjectDisposedException>(() => primPool.TryObtain(out var item));
            Assert.Throws <ObjectDisposedException>(() => primPool.TryRelease(new TestObjectPoolItem()));
            Assert.Throws <ObjectDisposedException>(() => primPool.Obtain());
            Assert.Throws <ObjectDisposedException>(() => primPool.Release(new TestObjectPoolItem()));

            Assert.Throws <ObjectDisposedException>(() => resizePool.InUse);
            Assert.Throws <ObjectDisposedException>(() => resizePool.PoolCount);
            Assert.Throws <ObjectDisposedException>(() => resizePool.Size);
            Assert.Throws <ObjectDisposedException>(() => resizePool.TryObtain(out var item));
            Assert.Throws <ObjectDisposedException>(() => resizePool.TryRelease(new TestObjectPoolItem()));
            Assert.Throws <ObjectDisposedException>(() => resizePool.Obtain());
            Assert.Throws <ObjectDisposedException>(() => resizePool.Release(new TestObjectPoolItem()));

            Assert.Throws <ObjectDisposedException>(() => blockPool.InUse);
            Assert.Throws <ObjectDisposedException>(() => blockPool.PoolCount);
            Assert.Throws <ObjectDisposedException>(() => blockPool.Size);
            Assert.Throws <ObjectDisposedException>(() => blockPool.TryObtain(out var item));
            Assert.Throws <ObjectDisposedException>(() => blockPool.TryRelease(new TestObjectPoolItem()));
            Assert.Throws <ObjectDisposedException>(() => blockPool.Obtain());
            Assert.Throws <ObjectDisposedException>(() => blockPool.Release(new TestObjectPoolItem()));
        }
コード例 #2
0
        void TestPrimitiveObjectPoolsCreation()
        {
            PrimitiveObjectPool <TestObjectPoolItem>          primPool   = null;
            ResizablePrimitiveObjectPool <TestObjectPoolItem> resizePool = null;
            BlockingPrimitiveObjectPool <TestObjectPoolItem>  blockPool  = null;

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                primPool = new PrimitiveObjectPool <TestObjectPoolItem>(0);
            });

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                resizePool = new ResizablePrimitiveObjectPool <TestObjectPoolItem>(0);
            });

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                blockPool = new BlockingPrimitiveObjectPool <TestObjectPoolItem>(0);
            });

            Assert.Null(primPool);
            Assert.Null(resizePool);
            Assert.Null(blockPool);

            primPool   = new PrimitiveObjectPool <TestObjectPoolItem>(1);
            resizePool = new ResizablePrimitiveObjectPool <TestObjectPoolItem>(1);
            blockPool  = new BlockingPrimitiveObjectPool <TestObjectPoolItem>(1);

            Assert.NotNull(primPool);
            Assert.NotNull(resizePool);
            Assert.NotNull(blockPool);
        }
コード例 #3
0
 protected override void SetupClient()
 {
     this.sendArgsPool = PrimitiveObjectPool.Create(100, CreateSendAsyncEventArgs);
     this.sentBytesCount = new Counter(MessageSize * MessageCount);
     this.clientSocket = new Socket(EndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
     this.clientSocket.Connect(EndPoint);
 }
コード例 #4
0
 protected override void SetupServer()
 {
     this.receivedBytesCount = new Counter(MessageSize * MessageCount);
     this.receiveArgsPool = PrimitiveObjectPool.Create(100, CreateReceiveAsyncEventArgs);
     this.serverSocket = new Socket(EndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
     this.serverSocket.Bind(EndPoint);
     this.serverSocket.Listen(1);
 }
コード例 #5
0
        void TestPrimitiveObjectPoolObtainRelease()
        {
            PrimitiveObjectPool <TestObjectPoolItem> primPool = new PrimitiveObjectPool <TestObjectPoolItem>(3);

            Assert.Equal(3, primPool.Size);
            Assert.Equal(0, primPool.InUse);
            Assert.Equal(3, primPool.PoolCount);

            TestObjectPoolItem test = new TestObjectPoolItem();

            Assert.False(primPool.TryRelease(test));

            Assert.Equal(3, primPool.Size);
            Assert.Equal(0, primPool.InUse);
            Assert.Equal(3, primPool.PoolCount);

            Assert.Throws <InvalidOperationException>(() => primPool.Release(test));

            Assert.Equal(3, primPool.Size);
            Assert.Equal(0, primPool.InUse);
            Assert.Equal(3, primPool.PoolCount);

            TestObjectPoolItem test1;
            TestObjectPoolItem test2;
            TestObjectPoolItem test3;
            TestObjectPoolItem test4;

            Assert.True(primPool.TryObtain(out test1));

            Assert.NotNull(test1);
            Assert.Equal(3, primPool.Size);
            Assert.Equal(1, primPool.InUse);
            Assert.Equal(2, primPool.PoolCount);

            test2 = primPool.Obtain();

            Assert.NotNull(test2);
            Assert.Equal(3, primPool.Size);
            Assert.Equal(2, primPool.InUse);
            Assert.Equal(1, primPool.PoolCount);

            test3 = primPool.Obtain();

            Assert.NotNull(test3);
            Assert.Equal(3, primPool.Size);
            Assert.Equal(3, primPool.InUse);
            Assert.Equal(0, primPool.PoolCount);

            Assert.False(primPool.TryObtain(out test4));

            Assert.Null(test4);
            Assert.Equal(3, primPool.Size);
            Assert.Equal(3, primPool.InUse);
            Assert.Equal(0, primPool.PoolCount);

            Assert.Throws <InvalidOperationException>(() => test4 = primPool.Obtain());

            Assert.Null(test4);
            Assert.Equal(3, primPool.Size);
            Assert.Equal(3, primPool.InUse);
            Assert.Equal(0, primPool.PoolCount);

            Assert.True(primPool.TryRelease(test1));

            Assert.Equal(3, primPool.Size);
            Assert.Equal(2, primPool.InUse);
            Assert.Equal(1, primPool.PoolCount);

            primPool.Release(test1);

            Assert.Equal(3, primPool.Size);
            Assert.Equal(1, primPool.InUse);
            Assert.Equal(2, primPool.PoolCount);

            primPool.Release(test1);

            Assert.Equal(3, primPool.Size);
            Assert.Equal(0, primPool.InUse);
            Assert.Equal(3, primPool.PoolCount);
        }