public void TestConstructor()
        {
            // Arrange
            MemoryStream memStream = new MemoryStream();

            // Act
            AsyncRequestChunkBuffer buffer = new AsyncRequestChunkBuffer(
                memStream, 1024, 512, 4);

            // Assert
            Assert.That(buffer.ChunkSize, Is.EqualTo(1024));
            Assert.That(buffer.MaxChunkCount, Is.EqualTo(512));
            Assert.That(buffer.ReadAheadChunkCount, Is.EqualTo(4));
        }
        public void TestStartFillingBuffer()
        {
            const int timeoutMs = 8000;
            const int numBuffers = 16;
            const int buffSize = 65536;

            // Arrange
            using(var inputStream = new FileStream(
                Path.Combine(UnitTestSettings.Default.TestDataPath, "Canyon.jpg"),
                FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            using(AsyncRequestChunkBuffer buffer = new AsyncRequestChunkBuffer(
                    inputStream, buffSize, numBuffers, 4))
            {
                Assert.That(inputStream.Length, Is.GreaterThanOrEqualTo(buffSize * numBuffers),
                    string.Format("Sample file must be greather than {0} bytes.",
                                      buffSize*numBuffers));

                // Act
                buffer.StartFillingBuffer();

                // Assert
                // Wait until the buffer is full
                var t = DateTime.UtcNow;
                while (buffer.ReadyChunkCount < numBuffers * buffSize)
                {
                    Assert.That(Math.Abs((DateTime.UtcNow - t).TotalMilliseconds),
                        Is.LessThan(timeoutMs),
                        string.Format("Buffers not filled in {0} ms.", timeoutMs));

                    Thread.Sleep(10);
                }

                var timespan = DateTime.UtcNow - t;
                Console.WriteLine("Read {0} bytes in {1} ms.",
                    buffSize * numBuffers, timespan.TotalMilliseconds);
            }
        }
 public ChunkInfo(AsyncRequestChunkBuffer parent, int index, int next)
 {
     Index = index;
     Prev = -1;
     Next = next;
 }