Beispiel #1
0
        public void ResetShouldAllowStreamReuse()
        {
            int initialOffset = 0;
            var manager       = new BufferManageMock();

            using (var stream = new BufferManagerStream(manager, initialOffset, 4, 1024))
            {
                stream.Write(_input, 0, 4);
                manager.ReturnBuffer(stream.GetArrayAndClear().Array);
                manager.AssertEverythingIsReturned();

                initialOffset = 1;
                stream.Reset(manager, initialOffset, 4);

                Assert.AreEqual(stream.Position, 0, "Position should be 0 after reset");

                byte[] otherInput = new byte[] { 3, 2, 1 };
                stream.Write(otherInput, 0, otherInput.Length);

                var array = stream.GetArrayAndClear();
                VerifyBufferContents(array, initialOffset, new ArraySegment <byte>(otherInput));
                manager.ReturnBuffer(array.Array);
                manager.AssertEverythingIsReturned();
            }
        }
Beispiel #2
0
        public void ShouldHandleThrowingAllocationWithoutMemoryLeak()
        {
            bool            shouldTrow    = false;
            Func <int, int> getBufferSize = size =>
            {
                if (shouldTrow)
                {
                    throw new InsufficientMemoryException();
                }
                else
                {
                    return(size);
                }
            };

            var manager = new BufferManageMock(getBufferSize);

            using (var stream = new BufferManagerStream(manager, 0, 4, 1024))
            {
                stream.Write(_input, 0, 2); // Write some into first buffer

                shouldTrow = true;
                ExceptionHelper.ExpectException <InsufficientMemoryException>(() =>
                                                                              stream.Write(_input, 2, 10)
                                                                              );
            }

            manager.AssertEverythingIsReturned();
        }
Beispiel #3
0
        private ArraySegment <byte> VerifyStreamContents(BufferManagerStream stream, int expectedOffset, int count, BufferManageMock manager, byte[] expectedContents = null)
        {
            Assert.AreEqual(count, stream.Position, "Stream position should equal count");

            var buffer = stream.GetArrayAndClear();

            VerifyBufferContents(buffer, expectedOffset, new ArraySegment <byte>(expectedContents ?? _input, 0, count));

            // By returning buffer we also ensure that it was allocated through the buffer manager
            manager.ReturnBuffer(buffer.Array);
            manager.AssertEverythingIsReturned();
            return(buffer);
        }