< Summary

Information
Class: Elsa.Workflows.Activities.Sequence
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Sequence.cs
Line coverage
67%
Covered lines: 29
Uncovered lines: 14
Coverable lines: 43
Total lines: 98
Line coverage: 67.4%
Branch coverage
50%
Covered branches: 8
Total branches: 16
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%
ScheduleChildrenAsync()100%11100%
HandleItemAsync()100%44100%
OnChildCompleted()100%44100%
OnScheduleChildActivityAsync()0%7280%
OnBreakSignalReceived(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Sequence.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3using Elsa.Workflows.Attributes;
 4using Elsa.Workflows.Options;
 5using Elsa.Workflows.Signals;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Workflows.Activities;
 9
 10/// <summary>
 11/// Execute a set of activities in sequence.
 12/// </summary>
 13/// <remarks>
 14/// Rescheduled direct children retain completion callback ownership by this sequence.
 15/// </remarks>
 16[Category("Workflows")]
 17[Activity("Elsa", "Workflows", "Execute a set of activities in sequence.")]
 18[PublicAPI]
 19[Browsable(false)]
 20public class Sequence : Container
 21{
 22    private const string CurrentIndexProperty = "CurrentIndex";
 23
 24    /// <inheritdoc />
 1007625    public Sequence([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 26    {
 1007627        OnSignalReceived<BreakSignal>(OnBreakSignalReceived);
 1007628        OnSignalReceived<ScheduleChildActivity>(OnScheduleChildActivityAsync);
 1007629    }
 30
 31    /// <inheritdoc />
 32    protected override async ValueTask ScheduleChildrenAsync(ActivityExecutionContext context)
 33    {
 16334        await HandleItemAsync(context);
 16335    }
 36
 37    private async ValueTask HandleItemAsync(ActivityExecutionContext context, ActivityExecutionContext? completedChildCo
 38    {
 44039        var currentIndex = context.GetProperty<int>(CurrentIndexProperty);
 44040        var childActivities = Activities.ToList();
 41
 44042        if (currentIndex >= childActivities.Count)
 43        {
 11744            await context.CompleteActivityAsync();
 11745            return;
 46        }
 47
 32348        var nextActivity = childActivities.ElementAt(currentIndex);
 32349        var options = new ScheduleWorkOptions
 32350        {
 32351            CompletionCallback = OnChildCompleted,
 32352            SchedulingActivityExecutionId = completedChildContext?.Id
 32353        };
 32354        await context.ScheduleActivityAsync(nextActivity, options);
 64655        context.UpdateProperty<int>(CurrentIndexProperty, x => x + 1);
 44056    }
 57
 58    private async ValueTask OnChildCompleted(ActivityCompletedContext context)
 59    {
 28860        var targetContext = context.TargetContext;
 28861        var childContext = context.ChildContext;
 28862        var isBreaking = targetContext.GetIsBreaking();
 28863        var completedActivity = childContext.Activity;
 64
 65        // If the complete activity is a terminal node, complete the sequence immediately.
 28866        if (isBreaking || completedActivity is ITerminalNode)
 67        {
 1168            await targetContext.CompleteActivityAsync();
 1169            return;
 70        }
 71
 27772        await HandleItemAsync(targetContext, childContext);
 28873    }
 74
 75    private async ValueTask OnScheduleChildActivityAsync(ScheduleChildActivity signal, SignalContext context)
 76    {
 077        var sequenceContext = context.ReceiverActivityExecutionContext;
 078        var childActivity = signal.ActivityExecutionContext?.Activity ?? signal.Activity;
 79
 080        if (childActivity == null || !Activities.Contains(childActivity))
 81        {
 082            return;
 83        }
 84
 085        context.StopPropagation();
 086        await sequenceContext.ScheduleActivityAsync(childActivity, new ScheduleWorkOptions
 087        {
 088            ExistingActivityExecutionContext = signal.ActivityExecutionContext,
 089            CompletionCallback = OnChildCompleted,
 090            Input = signal.Input
 091        });
 092    }
 93
 94    private void OnBreakSignalReceived(BreakSignal signal, SignalContext signalContext)
 95    {
 096        signalContext.ReceiverActivityExecutionContext.SetIsBreaking();
 097    }
 98}