Beispiel #1
0
        public async Task FlushMultipleBlocks()
        {
            byte[] buffer;
            var    blocking = new BlockingWriter();
            var    memory   = new MemoryWriter(blocking, Piece.BlockSize * 3);

            // Write 3 blocks
            for (int i = 0; i < 3; i++)
            {
                buffer = Enumerable.Repeat((byte)(i + 1), Piece.BlockSize).ToArray();
                await memory.WriteAsync(file, Piece.BlockSize *i, buffer, 0, buffer.Length).WithTimeout();
            }

            // Flush them all
            var flushTask = memory.FlushAsync(file);

            // Process the first flush
            blocking.Writes.TakeWithTimeout().tcs.SetResult(null);

            // write a new block
            buffer = Enumerable.Repeat((byte)1, Piece.BlockSize).ToArray();
            await memory.WriteAsync(file, Piece.BlockSize, buffer, 0, buffer.Length).WithTimeout();

            // Process the remaining two flushes
            blocking.Writes.TakeWithTimeout().tcs.SetResult(null);
            blocking.Writes.TakeWithTimeout().tcs.SetResult(null);

            await flushTask.WithTimeout();
        }
Beispiel #2
0
        public async Task WriteBlockWhileFlushing()
        {
            var blocking = new BlockingWriter();
            var memory   = new MemoryWriter(blocking, Piece.BlockSize * 3);

            await memory.WriteAsync(file, Piece.BlockSize, new byte[Piece.BlockSize], 0, Piece.BlockSize).WithTimeout();

            // Begin flushing the piece, but write another block to the cache while the flush is in-progress
            var flushTask = memory.FlushAsync(file);
            await memory.WriteAsync(file, Piece.BlockSize, new byte[Piece.BlockSize], 0, Piece.BlockSize).WithTimeout();

            blocking.Writes.TakeWithTimeout().tcs.SetResult(null);
            await flushTask.WithTimeout();

            // At the end we should have one block still in the cache.
            Assert.AreEqual(Piece.BlockSize, memory.CacheUsed);
        }
        public async Task FillFirstBuffer()
        {
            // Write 4 blocks to the stream and then verify they can all be read
            for (int i = 0; i < 4; i++)
            {
                var buffer = Enumerable.Repeat((byte)(i + 1), Piece.BlockSize).ToArray();
                await level1.WriteAsync(file, Piece.BlockSize *i, buffer, 0, buffer.Length);
            }

            Assert.AreEqual(Piece.BlockSize * 3, level1.CacheUsed, "#1");
            Assert.AreEqual(Piece.BlockSize, level2.CacheUsed, "#2");

            // Read them all back out and verify them
            for (int i = 0; i < 4; i++)
            {
                var buffer = new byte[Piece.BlockSize];
                await level1.ReadAsync(file, Piece.BlockSize *i, buffer, 0, Piece.BlockSize);

                Assert.That(buffer, Is.All.EqualTo((byte)(i + 1)));
            }
            Assert.AreEqual(Piece.BlockSize * 3, level1.CacheHits, "#3");
            Assert.AreEqual(Piece.BlockSize, level1.CacheMisses, "#4");
            Assert.AreEqual(Piece.BlockSize, level2.CacheHits, "#5");
        }