< Summary

Information
Class: AsiBackbone.Storage.InMemory.Audit.InMemoryAuditResidueLifecycleStore
Assembly: AsiBackbone.Storage.InMemory
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Storage.InMemory/Audit/InMemoryAuditResidueLifecycleStore.cs
Line coverage
38%
Covered lines: 10
Uncovered lines: 16
Coverable lines: 26
Total lines: 77
Line coverage: 38.4%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
AppendAsync(...)50%22100%
FindByEventIdAsync(...)100%11100%
FindByCorrelationIdAsync(...)100%210%
FindByAuditResidueIdAsync(...)100%210%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Storage.InMemory/Audit/InMemoryAuditResidueLifecycleStore.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using AsiBackbone.Core.Audit;
 3
 4namespace AsiBackbone.Storage.InMemory.Audit;
 5
 6/// <summary>
 7/// In-memory audit residue lifecycle store for tests, samples, and development hosts.
 8/// </summary>
 9/// <remarks>
 10/// This store is not durable across process restarts. Production hosts should use a durable provider such as EF Core or
 11/// </remarks>
 12public sealed class InMemoryAuditResidueLifecycleStore : IAsiBackboneAuditResidueLifecycleStore
 13{
 114    private readonly ConcurrentDictionary<string, AuditResidueLifecycleEvent> events = new(StringComparer.Ordinal);
 15
 16    /// <inheritdoc />
 17    public ValueTask<AuditResidueLifecycleEvent> AppendAsync(
 18        AuditResidueLifecycleEvent lifecycleEvent,
 19        CancellationToken cancellationToken = default)
 20    {
 121        ArgumentNullException.ThrowIfNull(lifecycleEvent);
 122        cancellationToken.ThrowIfCancellationRequested();
 23
 124        return !events.TryAdd(lifecycleEvent.EventId, lifecycleEvent)
 125            ? throw new InvalidOperationException($"Lifecycle event '{lifecycleEvent.EventId}' already exists.")
 126            : ValueTask.FromResult(lifecycleEvent);
 27    }
 28
 29    /// <inheritdoc />
 30    public ValueTask<AuditResidueLifecycleEvent?> FindByEventIdAsync(
 31        string eventId,
 32        CancellationToken cancellationToken = default)
 33    {
 134        ArgumentException.ThrowIfNullOrWhiteSpace(eventId);
 135        cancellationToken.ThrowIfCancellationRequested();
 36
 137        _ = events.TryGetValue(eventId.Trim(), out AuditResidueLifecycleEvent? lifecycleEvent);
 38
 139        return ValueTask.FromResult(lifecycleEvent);
 40    }
 41
 42    /// <inheritdoc />
 43    public ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByCorrelationIdAsync(
 44        string correlationId,
 45        CancellationToken cancellationToken = default)
 46    {
 047        ArgumentException.ThrowIfNullOrWhiteSpace(correlationId);
 048        cancellationToken.ThrowIfCancellationRequested();
 49
 050        string normalizedCorrelationId = correlationId.Trim();
 51
 052        IReadOnlyList<AuditResidueLifecycleEvent> matches = [.. events.Values
 053            .Where(lifecycleEvent => string.Equals(lifecycleEvent.CorrelationId, normalizedCorrelationId, StringComparis
 054            .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc)
 055            .ThenBy(lifecycleEvent => lifecycleEvent.EventId)];
 56
 057        return ValueTask.FromResult(matches);
 58    }
 59
 60    /// <inheritdoc />
 61    public ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByAuditResidueIdAsync(
 62        string auditResidueId,
 63        CancellationToken cancellationToken = default)
 64    {
 065        ArgumentException.ThrowIfNullOrWhiteSpace(auditResidueId);
 066        cancellationToken.ThrowIfCancellationRequested();
 67
 068        string normalizedAuditResidueId = auditResidueId.Trim();
 69
 070        IReadOnlyList<AuditResidueLifecycleEvent> matches = [.. events.Values
 071            .Where(lifecycleEvent => string.Equals(lifecycleEvent.AuditResidueId, normalizedAuditResidueId, StringCompar
 072            .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc)
 073            .ThenBy(lifecycleEvent => lifecycleEvent.EventId)];
 74
 075        return ValueTask.FromResult(matches);
 76    }
 77}