public async Task TestReadFromStream() { var options = new FileBufferOptions(100, 10); await using var buffers = new FileBuffers(options); using var memStream = new MemoryStream(); var data = new byte[150]; for (byte i = 0; i < data.Length; i++) { data[i] = i; } memStream.Write(data, 0, data.Length); var id = new BufferId(Guid.NewGuid(), "*"); await using var buffer = await buffers.AddForMememoryStream(id, memStream).NoSync(); var page = UseAndReturn.That(await buffer.GetPageAsync(0).ConfigureAwait(false)); AssertEqual(1, page.Data[1]); AssertEqual(100, page.Data.Length); await page.DisposeAsync().NoSync(); AssertEqual(1, buffers.Pool.Count); page = UseAndReturn.That(await buffer.GetPageAsync(0).ConfigureAwait(false)); AssertEqual(0L, page.Data.Number); AssertEqual(1, buffer.NumberOfBufferedPages); await page.DisposeAsync().NoSync(); }
public async Task TestPageCaching() { var options = new FileBufferOptions(10, 5); await using var buffers = new FileBuffers(options); var memStream = new MemoryStream(); var data = new byte[150]; for (byte i = 0; i < data.Length; i++) { data[i] = i; } memStream.Write(data, 0, data.Length); var id = new BufferId(Guid.NewGuid(), "*"); await using var buffer = await buffers.AddForMememoryStream(id, memStream).NoSync(); await using var page1 = await buffer.GetPageAsync(0).NoSync(); await using var page2 = await buffer.GetPageAsync(1).NoSync(); await using var page3 = await buffer.GetPageAsync(2).NoSync(); await using var page4 = await buffer.GetPageAsync(3).NoSync(); await using (var page5 = await buffer.GetPageAsync(4).NoSync()) { AssertEqual(0L, page1.Number); AssertEqual(1L, page2.Number); AssertEqual(2L, page3.Number); AssertEqual(3L, page4.Number); AssertEqual(4L, page5.Number); AssertEqual(5, buffer.NumberOfBufferedPages); await using var page6 = await buffer.GetPageAsync(5).ConfigureAwait(false); AssertEqual(5L, page6.Number); AssertEqual(6, buffer.NumberOfBufferedPages); await page6.UnPin().NoSync(); } AssertEqual(5, buffer.NumberOfBufferedPages); }