Beispiel #1
0
        public void WhenAllBuffersReadAndReturnedPoolIsEmpty()
        {
            // arrange
            var bufferPool = new BufferPool(2, 9, new Mock <ILog>().Object);

            var fullBuffer = bufferPool.AcquireForWriting();

            fullBuffer.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 0, 9);
            bufferPool.Release(fullBuffer);

            var partialBuffer = bufferPool.AcquireForWriting();

            partialBuffer.Write(new byte[] { 0, 1, 2 }, 0, 3);
            bufferPool.Release(partialBuffer);

            // act
            var fullBufferFromPool = bufferPool.AcquireForReading(TimeSpan.MaxValue);

            fullBufferFromPool.Reset();
            bufferPool.Release(fullBufferFromPool);

            var partialBufferFromPool = bufferPool.AcquireForReading(TimeSpan.MaxValue);

            partialBufferFromPool.Reset();
            bufferPool.Release(partialBufferFromPool);

            // assert
            Assert.IsTrue(bufferPool.IsEmpty);
        }
Beispiel #2
0
        public void WhenDataReadTheTotalReadableBuffersCountIsZero()
        {
            // arrange
            var bufferPool   = new BufferPool(1, 9, new Mock <ILog>().Object);
            var memoryStream = bufferPool.AcquireForWriting();

            memoryStream.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 0, 9);
            bufferPool.Release(memoryStream);

            var readableBuffersBefore = bufferPool.ReadableBuffers;

            // act
            var writableStream = bufferPool.AcquireForReading(TimeSpan.MaxValue);

            var buffer = new byte[9];

            using (var bufferStream = new MemoryStream(buffer))
            {
                writableStream.WriteTo(bufferStream);
                writableStream.Reset();
            }
            bufferPool.Release(writableStream);

            // assert
            Assert.AreEqual(1, readableBuffersBefore);
            Assert.AreEqual(0, bufferPool.ReadableBuffers);
        }
Beispiel #3
0
        private MemoryStream GetBuffer()
        {
            _isIdle = true;

            var buffer = _bufferPool.AcquireForReading(ReadTimeout);

            _isIdle = buffer == null;

            return(buffer);
        }
Beispiel #4
0
        public void WhenNoDataBlockingOccurs()
        {
            // arrange
            var bufferPool = new BufferPool(2, 5, new Mock <ILog>().Object);

            // act
            var task = Task.Run(() =>
            {
                bufferPool.AcquireForReading(TimeSpan.MaxValue);
            });
            var waitResult = task.Wait(TimeSpan.FromMilliseconds(2000));

            // assert
            Assert.IsFalse(waitResult);
        }
Beispiel #5
0
        public void WhenOneBufferIsFullOtherPartialBufferIsNotReturnedForReading()
        {
            // arrange
            var bufferPool = new BufferPool(2, 9, new Mock <ILog>().Object);

            var fullBuffer = bufferPool.AcquireForWriting();

            fullBuffer.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 0, 9);
            bufferPool.Release(fullBuffer);

            var partialBuffer = bufferPool.AcquireForWriting();

            partialBuffer.Write(new byte[] { 0, 1, 2 }, 0, 3);
            bufferPool.Release(partialBuffer);

            // act
            var buffer = bufferPool.AcquireForReading(TimeSpan.MaxValue);

            // assert
            Assert.AreSame(fullBuffer, buffer);
        }
Beispiel #6
0
        public void WhenDataBecomesAvailableBlockingClears()
        {
            // arrange
            var bufferPool = new BufferPool(2, 5, new Mock <ILog>().Object);

            // act
            var task = Task.Run(() =>
            {
                bufferPool.AcquireForReading(TimeSpan.MaxValue);
            });
            var waitResult = task.Wait(TimeSpan.FromMilliseconds(2000));

            // assert
            Assert.IsFalse(waitResult);

            var stream = bufferPool.AcquireForWriting();

            stream.WriteByte(1);
            bufferPool.Release(stream);

            waitResult = task.Wait(TimeSpan.FromMilliseconds(2000));
            Assert.IsTrue(waitResult);
        }