Example #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>();
            PreFetchReader pfh = new PreFetchReader(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);
        }
Example #2
0
        public void Read_Returns0BytesAtEOF(int totalPreFetchBlocks, int shadowBufferSize, int totBytes)
        {
            byte[] originalBuffer             = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray();
            BufferedStreamReaderConfig config = GetConfig(shadowBufferSize, totalPreFetchBlocks);
            PreFetchReader             pfh    = new PreFetchReader(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);
        }
Example #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);
            PreFetchReader             pfh    = new PreFetchReader(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);
        }