Exemplo n.º 1
0
        public void Write_WriteByteNotCalledOnInnerStream(bool delayedWriter)
        {
            var streamMock = new Mock <Stream>();

            byte[] tmparr = new byte[1];

            BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(1, delayedWriter));

            s.Write(tmparr, 0, 1);
            s.Write(tmparr, 0, 1);

            streamMock.Verify(f => f.WriteByte(It.IsAny <byte>()), Times.Never());
        }
Exemplo n.º 2
0
        public void Dispose_WriterAborts(bool delayedWriter)
        {
            int shadowBufferSize = 4;
            var config           = GetConfig(shadowBufferSize, delayedWriter);
            var writerMock       = new Mock <IWriter>(MockBehavior.Loose);

            writerMock.Setup(p => p.RequestBuffer())
            .Returns(() => new ShadowBufferData(shadowBufferSize));

            using (BufferedStreamWriter sw = new BufferedStreamWriter(new MemoryStream(), config, writerMock.Object))
                sw.Write(new byte[] { 1, 2 }, 0, 2);

            writerMock.Verify(f => f.Abort(), Times.Once());
        }
Exemplo n.º 3
0
        public void Dispose_PositionMatchesInnerStreams(int count, int shadowBufferSize, bool delayedWriter)
        {
            var originalBuffer = Enumerable.Range(1, 10).Select(f => (byte)f).ToArray();

            byte[] finalBuffer = new byte[originalBuffer.Length];
            var    stream      = new MemoryStream(finalBuffer);

            using (BufferedStreamWriter s = new BufferedStreamWriter(stream, GetConfig(shadowBufferSize, delayedWriter)))
            {
                s.Write(originalBuffer, 0, count);
            }

            Assert.AreEqual(count, stream.Position);
        }
Exemplo n.º 4
0
        public void Dispose_StreamFlushes(bool delayedWriter)
        {
            byte[] inputBuffer = Enumerable.Range(1, 10).Select(f => (byte)f).ToArray();
            var    streamMock  = new Mock <Stream>(MockBehavior.Strict);

            streamMock
            .Setup(p => p.Write(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()));
            streamMock
            .Setup(p => p.Flush());

            using (BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(4, delayedWriter)))
                s.Write(inputBuffer, 0, inputBuffer.Length);

            streamMock.Verify(f => f.Flush(), Times.Once());
        }
Exemplo n.º 5
0
        public void Flush_CorrectBytesAreSaved(int shadowBufferSize, int totBytes, int writeBlockLength, bool delayedWriter)
        {
            byte[]               inputBuffer = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray();
            byte[]               destBuffer  = new byte[totBytes];
            MemoryStream         destStream  = new MemoryStream(destBuffer);
            BufferedStreamWriter s           = new BufferedStreamWriter(destStream, GetConfig(shadowBufferSize, delayedWriter));

            for (int ix = 0; ix < totBytes; ix += writeBlockLength)
            {
                s.Write(inputBuffer, ix, Math.Min(totBytes - ix, writeBlockLength));
            }

            s.Flush();

            Assert.AreEqual(inputBuffer, destBuffer);
        }
Exemplo n.º 6
0
        public void Dispose_InnerStreamDoesntDispose(bool canSeek, bool delayedWriter)
        {
            var streamMock = new Mock <Stream>(MockBehavior.Loose);

            streamMock.SetupGet(p => p.CanSeek)
            .Returns(canSeek);
            streamMock.SetupGet(p => p.Position)
            .Returns(0);
            streamMock.SetupGet(p => p.Length)
            .Returns(1);

            using (BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(1, delayedWriter)))
            {
                s.Write(new byte[] { 1 }, 0, 1);
            }

            streamMock.Verify(f => f.Close(), Times.Never());
        }
Exemplo n.º 7
0
        public void RequestBuffer_BufferReleasedAfterWrite(int writeBlockLength, int repCount, bool delayedWriter)
        {
            int  totBytes         = repCount * writeBlockLength;
            int  shadowBufferSize = 4;
            bool isRented         = false;
            var  config           = GetConfig(shadowBufferSize, delayedWriter: delayedWriter);
            var  writerMock       = new Mock <IWriter>(MockBehavior.Loose);

            writerMock.Setup(p => p.RequestBuffer())
            .Returns(() =>
            {
                if (isRented)
                {
                    throw new InvalidOperationException();
                }
                isRented = true;
                return(new ShadowBufferData(shadowBufferSize));
            });
            writerMock.Setup(p => p.ReturnBufferAndWrite(It.IsAny <ShadowBufferData>()))
            .Callback <ShadowBufferData>((sourceBuffer) =>
            {
                if (!isRented)
                {
                    throw new InvalidOperationException();
                }
                isRented = false;
            });
            byte[] tmpBuf     = new byte[writeBlockLength];
            byte[] destBuffer = new byte[totBytes];

            TestDelegate body = delegate()
            {
                using (BufferedStreamWriter sw = new BufferedStreamWriter(new MemoryStream(destBuffer), config, writerMock.Object))
                {
                    for (int ix = 0, f = 0; f < repCount; ix += writeBlockLength, f++)
                    {
                        sw.Write(tmpBuf, 0, writeBlockLength);
                    }
                }
            };

            Assert.DoesNotThrow(body);
        }