コード例 #1
0
        public void RequestNewBuffer_NeverRunsPastShadowBufferSize(int totalPreFetchBlocks, int shadowBufferSize)
        {
            int stopReadAt  = 5;
            int curPos      = 0;
            int forceStopAt = 100;
            var streamMock  = new Mock <Stream>(MockBehavior.Loose);

            streamMock.Setup(p => p.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()))
            .Returns <byte[], int, int>((a, b, c) => { curPos += c; return(c); });
            BufferedStreamReaderConfig config = GetConfig(shadowBufferSize, totalPreFetchBlocks, stopReadAt);
            List <byte>       resArray        = new List <byte>();
            SynchronousReader pfh             = new SynchronousReader(streamMock.Object, config);

            int totRead = 0;
            int counter = 0;

            do
            {
                ShadowBufferData destBuffer = pfh.RequestNewBuffer();
                pfh.ReturnBuffer(destBuffer);
                totRead  = destBuffer?.byteCount ?? 0;
                counter += totRead;
            }while (totRead != 0 && counter < forceStopAt);

            Assert.AreEqual(stopReadAt, counter);
            Assert.AreEqual(stopReadAt, curPos);
        }
コード例 #2
0
        public void RequestNewBuffer_Returns0BytesAtEOF(int totalPreFetchBlocks, int shadowBufferSize, int totBytes)
        {
            byte[] originalBuffer             = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray();
            BufferedStreamReaderConfig config = GetConfig(shadowBufferSize, totalPreFetchBlocks);
            SynchronousReader          pfh    = new SynchronousReader(new MemoryStream(originalBuffer), config);

            while (true)
            {
                ShadowBufferData destBuffer = pfh.RequestNewBuffer();
                if (destBuffer == null || destBuffer.byteCount == 0)
                {
                    break;
                }
                pfh.ReturnBuffer(destBuffer);
            }
            ShadowBufferData lastDestBuffer = pfh.RequestNewBuffer();
            int lastRead = lastDestBuffer?.byteCount ?? 0;

            Assert.AreEqual(0, lastRead);
        }
コード例 #3
0
        public void RequestNewBuffer_CorrectBytesAreReturned(int totalPreFetchBlocks, int shadowBufferSize, int totBytes)
        {
            byte[]      originalBuffer        = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray();
            List <byte> resArray              = new List <byte>();
            BufferedStreamReaderConfig config = GetConfig(shadowBufferSize, totalPreFetchBlocks);
            SynchronousReader          pfh    = new SynchronousReader(new MemoryStream(originalBuffer), config);

            while (true)
            {
                ShadowBufferData destBuffer = pfh.RequestNewBuffer();
                if (destBuffer == null || destBuffer.byteCount == 0)
                {
                    break;
                }
                resArray.AddRange(destBuffer.buffer.Take(destBuffer.byteCount));
                pfh.ReturnBuffer(destBuffer);
            }

            Assert.AreEqual(originalBuffer, resArray.ToArray());
            Assert.DoesNotThrow(pfh.Abort);
        }