< Summary

Information
Class: Elsa.Workflows.Runtime.WorkflowCommitNotificationBuffer
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCommitNotificationBuffer.cs
Line coverage
95%
Covered lines: 41
Uncovered lines: 2
Coverable lines: 43
Total lines: 90
Line coverage: 95.3%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Begin()100%11100%
TryAdd(...)100%22100%
.ctor(...)100%11100%
Add(...)100%11100%
FlushAsync(...)100%11100%
FlushEntriesAsync()100%88100%
Dispose()75%4483.33%
ThrowIfDisposed()50%2266.66%
get_Notification()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCommitNotificationBuffer.cs

#LineLine coverage
 1using Elsa.Mediator.Contracts;
 2using Microsoft.Extensions.Logging;
 3
 4namespace Elsa.Workflows.Runtime;
 5
 6/// <inheritdoc />
 7247public class WorkflowCommitNotificationBuffer(IMediator mediator, ILogger<WorkflowCommitNotificationBuffer> logger) : IW
 8{
 7249    private readonly IMediator _mediator = mediator;
 72410    private readonly ILogger<WorkflowCommitNotificationBuffer> _logger = logger;
 72411    private readonly AsyncLocal<Scope?> _currentScope = new();
 12
 13    /// <inheritdoc />
 14    public IWorkflowCommitNotificationScope Begin()
 15    {
 56216        var scope = new Scope(this, _currentScope.Value);
 56217        _currentScope.Value = scope;
 56218        return scope;
 19    }
 20
 21    internal bool TryAdd(INotification notification, IEventPublishingStrategy? strategy)
 22    {
 1765423        var scope = _currentScope.Value;
 1765424        if (scope == null)
 1479525            return false;
 26
 285927        scope.Add(notification, strategy);
 285928        return true;
 29    }
 30
 56231    private class Scope(WorkflowCommitNotificationBuffer owner, Scope? parent) : IWorkflowCommitNotificationScope
 32    {
 56233        private readonly List<Entry> _entries = [];
 34        private bool _disposed;
 35
 36        public void Add(INotification notification, IEventPublishingStrategy? strategy)
 37        {
 285938            _entries.Add(new(notification, strategy));
 285939        }
 40
 41        public Task FlushAsync(CancellationToken cancellationToken = default)
 42        {
 56043            ThrowIfDisposed();
 56044            owner._currentScope.Value = parent;
 56045            return FlushEntriesAsync(cancellationToken);
 46        }
 47
 48        private async Task FlushEntriesAsync(CancellationToken cancellationToken)
 49        {
 56050            List<Exception>? exceptions = null;
 51
 683552            foreach (var entry in _entries)
 53            {
 54                try
 55                {
 285856                    await owner._mediator.SendAsync(entry.Notification, entry.Strategy, cancellationToken);
 285557                }
 358                catch (Exception ex) when (ex is not OperationCanceledException and not OutOfMemoryException and not Sta
 59                {
 260                    owner._logger.LogError(ex, "Failed to publish buffered workflow commit notification {NotificationTyp
 261                    exceptions ??= [];
 262                    exceptions.Add(ex);
 263                }
 285764            }
 65
 55966            _entries.Clear();
 67
 55968            if (exceptions is { Count: > 0 })
 269                throw new AggregateException("One or more workflow commit notifications failed.", exceptions);
 55770        }
 71
 72        public void Dispose()
 73        {
 56274            if (_disposed)
 075                return;
 76
 56277            _disposed = true;
 56278            if (ReferenceEquals(owner._currentScope.Value, this))
 479                owner._currentScope.Value = parent;
 56280        }
 81
 82        private void ThrowIfDisposed()
 83        {
 56084            if (_disposed)
 085                throw new ObjectDisposedException(nameof(IWorkflowCommitNotificationScope));
 56086        }
 87    }
 88
 857789    private record Entry(INotification Notification, IEventPublishingStrategy? Strategy);
 90}