< Summary

Information
Class: MediatR.WhenAllPublisher
Assembly: Pozitron.Extensions.MediatR
File(s): /home/runner/work/Pozitron.Extensions.MediatR/Pozitron.Extensions.MediatR/src/Pozitron.Extensions.MediatR/Publishers/WhenAllPublisher.cs
Tag: 25_16436701542
Line coverage
96%
Covered lines: 29
Uncovered lines: 1
Coverable lines: 30
Total lines: 72
Line coverage: 96.6%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Publish()100%28.032896.66%

File(s)

/home/runner/work/Pozitron.Extensions.MediatR/Pozitron.Extensions.MediatR/src/Pozitron.Extensions.MediatR/Publishers/WhenAllPublisher.cs

#LineLine coverage
 1namespace MediatR;
 2
 3internal class WhenAllPublisher : INotificationPublisher
 4{
 5    public async Task Publish(
 6        IEnumerable<NotificationHandlerExecutor> handlerExecutors,
 7        INotification notification,
 8        CancellationToken cancellationToken)
 9    {
 910        List<Exception>? exceptions = null;
 911        var tasks = new List<Task>();
 12
 13        // Some of the tasks may throw an immediate exception, synchronously, and we can't guarantee they're utilizing a
 14        // e.g. public Task Handle(CancellationToken cancellationToken) => throw new Exception();
 15        // In that case, WhenAll won't even be executed. So, we must iterate and catch these exceptions beforehand.
 6016        foreach (var handlerExecutor in handlerExecutors)
 17        {
 18            try
 19            {
 2120                tasks.Add(handlerExecutor.HandlerCallback(notification, cancellationToken));
 1621            }
 222            catch (AggregateException ex)
 23            {
 224                (exceptions ??= []).AddRange(ex.Flatten().InnerExceptions);
 225            }
 326            catch (Exception ex)
 27            {
 328                (exceptions ??= []).Add(ex);
 329            }
 30        }
 31
 32        try
 33        {
 934            await Task.WhenAll(tasks).ConfigureAwait(false);
 435        }
 536        catch
 37        {
 3438            foreach (var task in tasks)
 39            {
 1240                if (task.IsFaulted)
 41                {
 542                    if (task.Exception.InnerExceptions.Count > 1 || task.Exception.InnerException is AggregateException)
 43                    {
 344                        (exceptions ??= []).AddRange(task.Exception.Flatten().InnerExceptions);
 45                    }
 246                    else if (task.Exception.InnerException is not null)
 47                    {
 248                        (exceptions ??= []).Add(task.Exception.InnerException);
 49                    }
 50                }
 751                else if (task.IsCanceled)
 52                {
 53                    try
 54                    {
 55                        // This will force the task to throw the exception if it's canceled.
 56                        // This will preserve all the information compared to creating a new TaskCanceledException manua
 357                        task.GetAwaiter().GetResult();
 058                    }
 359                    catch (Exception ex)
 60                    {
 361                        (exceptions ??= []).Add(ex);
 362                    }
 63                }
 64            }
 565        }
 66
 967        if (exceptions?.Count > 0)
 68        {
 769            throw new AggregateException(exceptions);
 70        }
 271    }
 72}

Methods/Properties

Publish()