< Summary

Information
Class: Intentum.Core.Streaming.BoundedMemoryBehaviorStreamConsumer
Assembly: Intentum.Core
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Core/Streaming/BoundedMemoryBehaviorStreamConsumer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 50
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%220%
PostAsync(...)100%110%
Complete()100%110%
ReadAllAsync()0%220%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Core/Streaming/BoundedMemoryBehaviorStreamConsumer.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Threading.Channels;
 3
 4namespace Intentum.Core.Streaming;
 5
 6/// <summary>
 7/// In-memory behavior stream consumer with bounded capacity for backpressure.
 8/// When the channel is full, <see cref="PostAsync"/> waits until space is available (backpressure).
 9/// </summary>
 10public sealed class BoundedMemoryBehaviorStreamConsumer : IBehaviorStreamConsumer
 11{
 12    private readonly Channel<BehaviorEventBatch> _channel;
 13
 14    /// <summary>
 15    /// Creates a bounded consumer. When capacity is reached, PostAsync will wait (backpressure).
 16    /// </summary>
 17    /// <param name="capacity">Maximum number of batches to buffer; must be positive.</param>
 018    public BoundedMemoryBehaviorStreamConsumer(int capacity)
 019    {
 020        if (capacity <= 0)
 021            throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "Capacity must be positive.");
 022        _channel = Channel.CreateBounded<BehaviorEventBatch>(new BoundedChannelOptions(capacity)
 023        {
 024            FullMode = BoundedChannelFullMode.Wait
 025        });
 026    }
 27
 28    /// <summary>
 29    /// Posts a batch to the stream. If the channel is full, waits until space is available (backpressure).
 30    /// </summary>
 31    public ValueTask PostAsync(BehaviorEventBatch batch, CancellationToken cancellationToken = default)
 032    {
 033        return _channel.Writer.WriteAsync(batch, cancellationToken);
 034    }
 35
 36    /// <summary>
 37    /// Completes the writer so ReadAllAsync will finish after all posted batches are read.
 38    /// </summary>
 39    public void Complete()
 040    {
 041        _channel.Writer.Complete();
 042    }
 43
 44    /// <inheritdoc />
 45    public async IAsyncEnumerable<BehaviorEventBatch> ReadAllAsync([EnumeratorCancellation] CancellationToken cancellati
 046    {
 047        await foreach (var batch in _channel.Reader.ReadAllAsync(cancellationToken))
 048            yield return batch;
 049    }
 50}