< Summary

Information
Class: Elsa.Testing.Shared.Services.AsyncWorkflowRunner
Assembly: Elsa.Testing.Shared.Component
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared.Component/Services/AsyncWorkflowRunner.cs
Line coverage
88%
Covered lines: 30
Uncovered lines: 4
Coverable lines: 34
Total lines: 84
Line coverage: 88.2%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RunAndAwaitWorkflowCompletionAsync()100%11100%
OnWorkflowStateCommitted(...)100%22100%
OnActivityExecutedLogUpdated(...)100%22100%
GetSignalName(...)100%11100%
Dispose()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared.Component/Services/AsyncWorkflowRunner.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using Elsa.Testing.Shared.Models;
 3using Elsa.Workflows;
 4using Elsa.Workflows.Models;
 5using Elsa.Workflows.Runtime;
 6using Elsa.Workflows.Runtime.Entities;
 7using Elsa.Workflows.Runtime.Messages;
 8
 9namespace Elsa.Testing.Shared.Services;
 10
 11/// <summary>
 12/// Provides functionality to execute workflows asynchronously and await their completion for testing purposes.
 13/// Tracks activity execution records and workflow completion signals.
 14/// </summary>
 15public class AsyncWorkflowRunner : IDisposable
 16{
 17    private readonly IWorkflowRuntime _workflowRuntime;
 18    private readonly IIdentityGenerator _identityGenerator;
 19    private readonly SignalManager _signalManager;
 20    private readonly WorkflowEvents _workflowEvents;
 2221    private readonly ConcurrentDictionary<string, ActivityExecutionRecord> _activityExecutionRecords = new();
 22
 23    /// <summary>
 24    /// Initializes a new instance of the <see cref="AsyncWorkflowRunner"/> class.
 25    /// </summary>
 2226    public AsyncWorkflowRunner(IWorkflowRuntime workflowRuntime, IIdentityGenerator identityGenerator, SignalManager sig
 27    {
 2228        _workflowRuntime = workflowRuntime;
 2229        _identityGenerator = identityGenerator;
 2230        _signalManager = signalManager;
 2231        _workflowEvents = workflowEvents;
 32
 2233        _workflowEvents.WorkflowStateCommitted += OnWorkflowStateCommitted;
 2234        _workflowEvents.ActivityExecutedLogUpdated += OnActivityExecutedLogUpdated;
 2235    }
 36
 37    /// <summary>
 38    /// Runs the specified workflow definition asynchronously and waits for its completion.
 39    /// Returns the workflow execution context and activity execution records.
 40    /// </summary>
 41    /// <param name="workflowDefinitionHandle">The handle of the workflow definition to execute.</param>
 42    /// <returns>A <see cref="TestWorkflowExecutionResult"/> containing the workflow execution context and activity exec
 43    public async Task<TestWorkflowExecutionResult> RunAndAwaitWorkflowCompletionAsync(WorkflowDefinitionHandle workflowD
 44    {
 1945        var workflowInstanceId = _identityGenerator.GenerateId();
 1946        var workflowClient = await _workflowRuntime.CreateClientAsync(workflowInstanceId);
 1947        await workflowClient.CreateInstanceAsync(new()
 1948        {
 1949            WorkflowDefinitionHandle = workflowDefinitionHandle
 1950        });
 1951        _activityExecutionRecords.Clear();
 1952        await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty);
 1953        var signalName = GetSignalName(workflowInstanceId);
 1954        var workflowExecutionContext = await _signalManager.WaitAsync<WorkflowExecutionContext>(signalName);
 1955        return new(workflowExecutionContext, _activityExecutionRecords.Values.ToList());
 1956    }
 57
 58    private void OnWorkflowStateCommitted(object? sender, WorkflowStateCommittedEventArgs e)
 59    {
 611360        if (e.WorkflowExecutionContext.Status != WorkflowStatus.Finished)
 59461            return;
 62
 551963        var signalName = GetSignalName(e.WorkflowExecutionContext.Id);
 551964        _signalManager.Trigger(signalName, e.WorkflowExecutionContext);
 551965    }
 66
 67    private void OnActivityExecutedLogUpdated(object? sender, ActivityExecutedLogUpdatedEventArgs e)
 68    {
 11501269        foreach (var record in e.Records)
 5139370            _activityExecutionRecords[record.Id] = record;
 611371    }
 72
 553873    private static string GetSignalName(string workflowInstanceId) => $"WorkflowInstanceCompleted-{workflowInstanceId}";
 74
 75    /// <summary>
 76    /// Unsubscribes from workflow events and releases resources.
 77    /// </summary>
 78    public void Dispose()
 79    {
 080        _workflowEvents.WorkflowStateCommitted -= OnWorkflowStateCommitted;
 081        _workflowEvents.ActivityExecutedLogUpdated -= OnActivityExecutedLogUpdated;
 082        GC.SuppressFinalize(this);
 083    }
 84}