| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Elsa.Testing.Shared.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Messages; |
| | | 8 | | |
| | | 9 | | namespace 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> |
| | | 15 | | public class AsyncWorkflowRunner : IDisposable |
| | | 16 | | { |
| | | 17 | | private readonly IWorkflowRuntime _workflowRuntime; |
| | | 18 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 19 | | private readonly SignalManager _signalManager; |
| | | 20 | | private readonly WorkflowEvents _workflowEvents; |
| | 22 | 21 | | private readonly ConcurrentDictionary<string, ActivityExecutionRecord> _activityExecutionRecords = new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="AsyncWorkflowRunner"/> class. |
| | | 25 | | /// </summary> |
| | 22 | 26 | | public AsyncWorkflowRunner(IWorkflowRuntime workflowRuntime, IIdentityGenerator identityGenerator, SignalManager sig |
| | | 27 | | { |
| | 22 | 28 | | _workflowRuntime = workflowRuntime; |
| | 22 | 29 | | _identityGenerator = identityGenerator; |
| | 22 | 30 | | _signalManager = signalManager; |
| | 22 | 31 | | _workflowEvents = workflowEvents; |
| | | 32 | | |
| | 22 | 33 | | _workflowEvents.WorkflowStateCommitted += OnWorkflowStateCommitted; |
| | 22 | 34 | | _workflowEvents.ActivityExecutedLogUpdated += OnActivityExecutedLogUpdated; |
| | 22 | 35 | | } |
| | | 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 | | { |
| | 19 | 45 | | var workflowInstanceId = _identityGenerator.GenerateId(); |
| | 19 | 46 | | var workflowClient = await _workflowRuntime.CreateClientAsync(workflowInstanceId); |
| | 19 | 47 | | await workflowClient.CreateInstanceAsync(new() |
| | 19 | 48 | | { |
| | 19 | 49 | | WorkflowDefinitionHandle = workflowDefinitionHandle |
| | 19 | 50 | | }); |
| | 19 | 51 | | _activityExecutionRecords.Clear(); |
| | 19 | 52 | | await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty); |
| | 19 | 53 | | var signalName = GetSignalName(workflowInstanceId); |
| | 19 | 54 | | var workflowExecutionContext = await _signalManager.WaitAsync<WorkflowExecutionContext>(signalName); |
| | 19 | 55 | | return new(workflowExecutionContext, _activityExecutionRecords.Values.ToList()); |
| | 19 | 56 | | } |
| | | 57 | | |
| | | 58 | | private void OnWorkflowStateCommitted(object? sender, WorkflowStateCommittedEventArgs e) |
| | | 59 | | { |
| | 6113 | 60 | | if (e.WorkflowExecutionContext.Status != WorkflowStatus.Finished) |
| | 594 | 61 | | return; |
| | | 62 | | |
| | 5519 | 63 | | var signalName = GetSignalName(e.WorkflowExecutionContext.Id); |
| | 5519 | 64 | | _signalManager.Trigger(signalName, e.WorkflowExecutionContext); |
| | 5519 | 65 | | } |
| | | 66 | | |
| | | 67 | | private void OnActivityExecutedLogUpdated(object? sender, ActivityExecutedLogUpdatedEventArgs e) |
| | | 68 | | { |
| | 115012 | 69 | | foreach (var record in e.Records) |
| | 51393 | 70 | | _activityExecutionRecords[record.Id] = record; |
| | 6113 | 71 | | } |
| | | 72 | | |
| | 5538 | 73 | | 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 | | { |
| | 0 | 80 | | _workflowEvents.WorkflowStateCommitted -= OnWorkflowStateCommitted; |
| | 0 | 81 | | _workflowEvents.ActivityExecutedLogUpdated -= OnActivityExecutedLogUpdated; |
| | 0 | 82 | | GC.SuppressFinalize(this); |
| | 0 | 83 | | } |
| | | 84 | | } |