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

            byte[] tmparr = new byte[1];

            BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(1, delayedWriter));
            await s.WriteAsync(tmparr, 0, 1);

            await s.WriteAsync(tmparr, 0, 1);

            streamMock.Verify(f => f.WriteByte(It.IsAny <byte>()), Times.Never());
        }
Exemplo n.º 2
0
        public async Task WriteAsync_Cancel_ThrowsOpCancelled()
        {
            byte[] inputBuffer  = Enumerable.Range(1, 10).Select(f => (byte)f).ToArray();
            var    taskBlocker1 = new TaskCompletionSource <object>();
            var    taskBlocker2 = new TaskCompletionSource <object>();
            var    taskBlocker3 = new TaskCompletionSource <object>();
            bool   triggered    = false;
            var    streamMock   = new Mock <Stream>(MockBehavior.Strict);

            streamMock
            .Setup(p => p.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Returns(async(byte[] buffer, int offset, int count, CancellationToken token) =>
            {
                taskBlocker1.SetResult(null);
                await taskBlocker3.Task;
                triggered = token.IsCancellationRequested;
                taskBlocker2.SetResult(null);
                token.ThrowIfCancellationRequested();
            });
            CancellationTokenSource ts    = new CancellationTokenSource();
            CancellationToken       token = ts.Token;
            BufferedStreamWriter    s     = new BufferedStreamWriter(streamMock.Object, GetConfig(4));
            Task task = s.WriteAsync(inputBuffer, 0, inputBuffer.Length, token);
            await taskBlocker1.Task;

            ts.Cancel();
            taskBlocker3.SetResult(null);
            await taskBlocker2.Task;

            Assert.IsTrue(triggered);
            Assert.ThrowsAsync <OperationCanceledException>(async() => await task);
        }
Exemplo n.º 3
0
        public static async Task New_WriteAsync(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            byte[] buffer = Enumerable.Range(0, 10).Select(f => (byte)f).ToArray();
            using (var s = File.Create(fileName))
                using (var sr = new BufferedStreamWriter(s))
                    await sr.WriteAsync(buffer);
        }
Exemplo n.º 4
0
        public async Task DisposeAsync_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);

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

            Assert.AreEqual(count, stream.Position);
        }
Exemplo n.º 5
0
        public async Task DisposeAsync_WriterAbortsAsync(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));

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

            writerMock.Verify(f => f.AbortAsync(), Times.Once());
        }
Exemplo n.º 6
0
        public async Task FlushAsync_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)
            {
                await s.WriteAsync(inputBuffer, ix, Math.Min(totBytes - ix, writeBlockLength));
            }

            await s.FlushAsync();

            Assert.AreEqual(inputBuffer, destBuffer);
        }
Exemplo n.º 7
0
        public async Task DisposeAsync_StreamFlushesAsync()
        {
            byte[] inputBuffer = Enumerable.Range(1, 10).Select(f => (byte)f).ToArray();
            var    streamMock  = new Mock <Stream>(MockBehavior.Strict);

            streamMock
            .Setup(p => p.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Returns((byte[] buffer, int offset, int count, CancellationToken token) => Task.CompletedTask);
            streamMock
            .Setup(p => p.FlushAsync(It.IsAny <CancellationToken>()))
            .Returns((CancellationToken token) => Task.CompletedTask);

            await using (BufferedStreamWriter s = new BufferedStreamWriter(streamMock.Object, GetConfig(4)))
                await s.WriteAsync(inputBuffer, 0, inputBuffer.Length, CancellationToken.None);

            streamMock.Verify(f => f.FlushAsync(It.IsAny <CancellationToken>()), Times.Once());
        }
Exemplo n.º 8
0
        public async Task Dispose_InnerStreamDoesntDisposeAsync(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)))
            {
                await s.WriteAsync(new byte[] { 1 }, 0, 1);
            }

            streamMock.Verify(f => f.Close(), Times.Never());
        }