public async Task GetNextPartitionAsync_1GB_WithReadOnlyMemory() { long memoryStart; long memoryEnd; var buffersRead = 0L; var length = (1L * Constants.GB) - (1 * Constants.MB) - (1 * Constants.KB); using (var expectedStream = new MockNonSeekableStream(length)) using (var reader = new StreamPartitioner(expectedStream)) { memoryStart = GC.GetTotalMemory(true); Assert.IsTrue(expectedStream.CanRead); Assert.IsFalse(expectedStream.CanSeek); do { var position = expectedStream.Position; using (StreamPartition buffer = await reader.GetNextPartitionAsync()) { if (buffer.Length == 0) { Assert.AreEqual(expectedStream.Length, expectedStream.Position); break; } else { buffersRead++; Assert.IsTrue(buffer.CanRead); Assert.IsTrue(buffer.CanSeek); buffer.Read(out ReadOnlyMemory <byte> memory, (int)buffer.Length); Assert.AreEqual((int)buffer.Length, memory.Length); } } Assert.IsTrue(GC.GetTotalMemory(true) - memoryStart < 8 * Storage.Constants.DefaultBufferSize); // TODO Assuming at most 8 buffers allocated }while (true); } memoryEnd = GC.GetTotalMemory(true); //logger.LogInformation($"{buffersRead} buffers read"); //logger.LogInformation($"{nameof(memoryStart)} = {memoryStart}; {nameof(memoryEnd)} = {memoryEnd}"); //logger.LogInformation($"delta = {memoryEnd - memoryStart}"); Assert.AreEqual(Math.Ceiling(1d * length / Storage.Constants.DefaultBufferSize), buffersRead); Assert.IsTrue(memoryEnd - memoryStart < 8 * Storage.Constants.DefaultBufferSize); // TODO Assuming at most 8 buffers allocated }
public async Task Read_WithReadOnlyMemory() { var expected = TestHelper.GetRandomBuffer(10 * Constants.MB); var actual = new byte[expected.Length]; Assert.AreNotSame(expected, actual); using (var expectedStream = new NonSeekableStream(expected)) using (var reader = new StreamPartitioner(expectedStream)) { Assert.IsTrue(expectedStream.CanRead); Assert.IsFalse(expectedStream.CanSeek); do { var position = expectedStream.Position; using (StreamPartition buffer = await reader.GetNextPartitionAsync()) { if (buffer.Length == 0) { Assert.AreEqual(expectedStream.Length, expectedStream.Position); break; } else { Assert.IsTrue(buffer.CanRead); Assert.IsTrue(buffer.CanSeek); buffer.Read(out ReadOnlyMemory <byte> memory, (int)buffer.Length); memory.CopyTo(new Memory <byte>(actual, (int)position, (int)buffer.Length)); } } }while (true); TestHelper.AssertSequenceEqual(expected, actual); } }