public void CachedOverread() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 90; int numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.AreEqual(10, numRead); AssertSequenced(buffer, 0, 10, 90); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[buffer.Length]; cacheStream.Position = 90; numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.AreEqual(10, numRead); AssertSequenced(buffer, 0, 10, 90); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn); }
public void Write() { MemoryStream ms = CreateSequencedMemStream(100, true); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 10; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 10); cacheStream.Position = 20; cacheStream.Write(new byte[10], 0, 10); Assert.AreEqual(30, cacheStream.Position); cacheStream.Position = 10; buffer = new byte[30]; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0, 10, 10); AssertSequenced(buffer, 20, 10, 30); Assert.AreEqual(0, buffer[10]); Assert.AreEqual(0, buffer[19]); }
public void CacheBlockRecycle() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 50, LargeReadSize = 100 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[50]; cacheStream.Position = 10; int numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.AreEqual(50, numRead); AssertSequenced(buffer, 10); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[40]; cacheStream.Position = 50; numRead = cacheStream.Read(buffer, 0, buffer.Length); Assert.AreEqual(40, numRead); AssertSequenced(buffer, 50); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn); }
public void UnalignedCachedRead() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[20]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(0, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(1, cacheStream.Statistics.TotalReadsIn); buffer = new byte[buffer.Length]; cacheStream.Position = 3; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 3); Assert.AreEqual(0, cacheStream.Statistics.LargeReadsIn); Assert.AreEqual(1, cacheStream.Statistics.ReadCacheHits); Assert.AreEqual(2, cacheStream.Statistics.TotalReadsIn); }
public void FailWrite() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[25]; cacheStream.Position = 0; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0); int freeBefore = cacheStream.Statistics.FreeReadBlocks; cacheStream.Position = 11; try { cacheStream.Write(new byte[10], 0, 10); } catch (NotSupportedException) { Assert.AreEqual(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks); } }
public void Bug5203_IncreaseSize() { MemoryStream ms = new MemoryStream(); BlockCacheSettings settings = new BlockCacheSettings { BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024 }; BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); // Pre-load read cache with a 'short' block bcs.Write(new byte[11], 0, 11); bcs.Position = 0; bcs.Read(new byte[11], 0, 11); // Extend stream for(int i = 0; i < 20; ++i) { bcs.Write(new byte[11], 0, 11); } // Try to read from first block beyond length of original cached short length // Bug was throwing exception here bcs.Position = 60; bcs.Read(new byte[20], 0, 20); }
public void Bug5203_IncreaseSize() { MemoryStream ms = new MemoryStream(); BlockCacheSettings settings = new BlockCacheSettings { BlockSize = 64, LargeReadSize = 128, OptimumReadSize = 64, ReadCacheSize = 1024 }; BlockCacheStream bcs = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); // Pre-load read cache with a 'short' block bcs.Write(new byte[11], 0, 11); bcs.Position = 0; bcs.Read(new byte[11], 0, 11); // Extend stream for (int i = 0; i < 20; ++i) { bcs.Write(new byte[11], 0, 11); } // Try to read from first block beyond length of original cached short length // Bug was throwing exception here bcs.Position = 60; bcs.Read(new byte[20], 0, 20); }
public void FailWrite() { MemoryStream ms = CreateSequencedMemStream(100, false); BlockCacheSettings settings = new BlockCacheSettings() { BlockSize = 10, OptimumReadSize = 20, ReadCacheSize = 100, LargeReadSize = 30 }; BlockCacheStream cacheStream = new BlockCacheStream(SparseStream.FromStream(ms, Ownership.Dispose), Ownership.Dispose, settings); byte[] buffer = new byte[25]; cacheStream.Position = 0; cacheStream.Read(buffer, 0, buffer.Length); AssertSequenced(buffer, 0); int freeBefore = cacheStream.Statistics.FreeReadBlocks; cacheStream.Position = 11; try { cacheStream.Write(new byte[10], 0, 10); } catch(NotSupportedException) { Assert.AreEqual(freeBefore + 2, cacheStream.Statistics.FreeReadBlocks); } }