public async Task PassthroughIfAllFlushesAreAwaited() { using (var slabPool = new SlabMemoryPool()) using (var diagnosticPool = new DiagnosticMemoryPool(slabPool)) { var pipeWriterFlushTcsArray = new[] { new TaskCompletionSource <FlushResult>(TaskCreationOptions.RunContinuationsAsynchronously), new TaskCompletionSource <FlushResult>(TaskCreationOptions.RunContinuationsAsynchronously), }; var sync = new object(); var mockPipeWriter = new MockPipeWriter(pipeWriterFlushTcsArray); var concurrentPipeWriter = new ConcurrentPipeWriter(mockPipeWriter, diagnosticPool, sync); ValueTask <FlushResult> flushTask; lock (sync) { var memory = concurrentPipeWriter.GetMemory(); Assert.Equal(1, mockPipeWriter.GetMemoryCallCount); concurrentPipeWriter.Advance(memory.Length); Assert.Equal(1, mockPipeWriter.AdvanceCallCount); flushTask = concurrentPipeWriter.FlushAsync(); Assert.Equal(1, mockPipeWriter.FlushCallCount); pipeWriterFlushTcsArray[0].SetResult(default);
public async Task DoesNotThrowWithLateReturns() { var memoryPool = new DiagnosticMemoryPool(new PinnedBlockMemoryPool(), allowLateReturn: true); var block = memoryPool.Rent(); memoryPool.Dispose(); block.Dispose(); await memoryPool.WhenAllBlocksReturnedAsync(TimeSpan.FromSeconds(5)); }
public MemoryPool <byte> Create() { lock (_pools) { var pool = new DiagnosticMemoryPool(new SlabMemoryPool(), _allowLateReturn, _rentTracking); _pools.Add(pool); return(pool); } }
public void ExceptionsContainStackTraceWhenEnabled() { var memoryPool = new DiagnosticMemoryPool(new PinnedBlockMemoryPool(), rentTracking: true); var block = memoryPool.Rent(); ExpectDisposeException(memoryPool); var exception = Assert.Throws <InvalidOperationException>(() => block.Memory); Assert.Contains("Block is backed by disposed slab", exception.Message); Assert.Contains("ExceptionsContainStackTraceWhenEnabled", exception.Message); }
public async Task ThrowsOnAccessToLateBlocks() { var memoryPool = new DiagnosticMemoryPool(new PinnedBlockMemoryPool(), allowLateReturn: true); var block = memoryPool.Rent(); memoryPool.Dispose(); var exception = Assert.Throws <InvalidOperationException>(() => block.Memory); Assert.Equal("Block is backed by disposed slab", exception.Message); block.Dispose(); var aggregateException = await Assert.ThrowsAsync <AggregateException>(async() => await memoryPool.WhenAllBlocksReturnedAsync(TimeSpan.FromSeconds(5))); Assert.Equal(new Exception [] { exception }, aggregateException.InnerExceptions); }
internal DiagnosticPoolBlock(DiagnosticMemoryPool pool, IMemoryOwner <byte> memoryOwner) { _pool = pool; _memoryOwner = memoryOwner; _memory = memoryOwner.Memory; }