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

96 lines
4.2 KiB
C#
Raw Normal View History

Add BulkDispatchWorkflows tests and improve activity coverage (#7026) * Refactor BulkDispatchWorkflows tests and improve activity coverage - Migrate `BulkDispatchWorkflows` tests to a new structure under `Scenarios/BulkDispatch`. - Introduce comprehensive test cases for bulk dispatch scenarios, including empty items, correlation IDs, child ports, dictionary items, and invalid definitions. - Add multiple reusable workflows such as `BulkChildWorkflow`, `BulkDispatchAndWaitWorkflow`, and `BulkDispatchFireAndForgetWorkflow` for enhanced test coverage. - Update namespaces and project files to reflect the restructuring and new workflow definitions. - Enhance functionality for `IWorkflowInstanceVariableManager` by adding methods for variable retrieval from workflow states and instances. * Add additional assertions for WriteLine activity tests and update workflow builder in BulkDispatch tests - Enhanced `BulkDispatchWorkflowsTests` by validating written texts for accuracy. - Refactored `BulkDispatchFireAndForgetWorkflow` to update activity and namespace references. * Add `SlowBulkChildWorkflow` and update `BulkDispatchFireAndForgetWorkflow` test structure - Introduced `SlowBulkChildWorkflow` with a delay and logging functionality for bulk dispatch testing. - Modified `BulkDispatchFireAndForgetWorkflow` to use `SlowBulkChildWorkflow` and updated test items. - Enhanced `BulkDispatchWorkflowsTests` with child workflow completion validation and timing assertions. * Refactor BulkDispatch tests by removing dictionary item workflow and streamlining child workflow handling - Removed `BulkDispatchWithDictionaryItemsWorkflow` and associated test cases. - Updated `BulkChildWorkflow` to use input binding. - Streamlined child workflow event handling in `BulkDispatchWorkflowsTests` using a reusable helper method. - Enhanced `CorrelationIdFunction` support with JavaScript expression handling. * Move unit test workflows to `Scenarios/Activities` and update namespace references - Relocated all test workflows from `Scenarios/BulkDispatch` and `Scenarios/ExecuteWorkflows` to `Scenarios/Activities`. - Updated namespaces and imports to reflect the new folder structure. * Refactor BulkDispatch and FlowJoin test workflows: - Moved `BulkDispatch` workflows to `Scenarios/Activities/BulkDispatchWorkflows`. - Renamed `FlowJoins` to `FlowJoin` and updated namespaces to match. - Adjusted imports and references to align with new folder structure and updated class definitions. * Remove unused folder reference from `Elsa.Workflows.ComponentTests.csproj`. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/Activities/BulkDispatchWorkflows/BulkDispatchWorkflowsTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-04 14:44:44 +00:00
using System.Collections.Concurrent;
using Elsa.Common.Models;
Add BulkDispatchWorkflows tests and improve activity coverage (#7026) * Refactor BulkDispatchWorkflows tests and improve activity coverage - Migrate `BulkDispatchWorkflows` tests to a new structure under `Scenarios/BulkDispatch`. - Introduce comprehensive test cases for bulk dispatch scenarios, including empty items, correlation IDs, child ports, dictionary items, and invalid definitions. - Add multiple reusable workflows such as `BulkChildWorkflow`, `BulkDispatchAndWaitWorkflow`, and `BulkDispatchFireAndForgetWorkflow` for enhanced test coverage. - Update namespaces and project files to reflect the restructuring and new workflow definitions. - Enhance functionality for `IWorkflowInstanceVariableManager` by adding methods for variable retrieval from workflow states and instances. * Add additional assertions for WriteLine activity tests and update workflow builder in BulkDispatch tests - Enhanced `BulkDispatchWorkflowsTests` by validating written texts for accuracy. - Refactored `BulkDispatchFireAndForgetWorkflow` to update activity and namespace references. * Add `SlowBulkChildWorkflow` and update `BulkDispatchFireAndForgetWorkflow` test structure - Introduced `SlowBulkChildWorkflow` with a delay and logging functionality for bulk dispatch testing. - Modified `BulkDispatchFireAndForgetWorkflow` to use `SlowBulkChildWorkflow` and updated test items. - Enhanced `BulkDispatchWorkflowsTests` with child workflow completion validation and timing assertions. * Refactor BulkDispatch tests by removing dictionary item workflow and streamlining child workflow handling - Removed `BulkDispatchWithDictionaryItemsWorkflow` and associated test cases. - Updated `BulkChildWorkflow` to use input binding. - Streamlined child workflow event handling in `BulkDispatchWorkflowsTests` using a reusable helper method. - Enhanced `CorrelationIdFunction` support with JavaScript expression handling. * Move unit test workflows to `Scenarios/Activities` and update namespace references - Relocated all test workflows from `Scenarios/BulkDispatch` and `Scenarios/ExecuteWorkflows` to `Scenarios/Activities`. - Updated namespaces and imports to reflect the new folder structure. * Refactor BulkDispatch and FlowJoin test workflows: - Moved `BulkDispatch` workflows to `Scenarios/Activities/BulkDispatchWorkflows`. - Renamed `FlowJoins` to `FlowJoin` and updated namespaces to match. - Adjusted imports and references to align with new folder structure and updated class definitions. * Remove unused folder reference from `Elsa.Workflows.ComponentTests.csproj`. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/Activities/BulkDispatchWorkflows/BulkDispatchWorkflowsTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-04 14:44:44 +00:00
using Elsa.Testing.Shared.Models;
using Elsa.Workflows;
Add `WorkflowStateCommitted` notification support and update state handling logic - Introduced `WorkflowStateCommitted` notification to encapsulate workflow execution context, state, and instance details. - Updated `DefaultCommitStateHandler` to publish `WorkflowStateCommitted` via `IMediator`. - Adjusted `DispatchWorkflowExtensions` to use `WorkflowStateCommitted` for workflow completion. Updates KubernetesClient and Microsoft packages (#6917) * Remove Proto.Cluster.Kubernetes dependency due to vulnerability - Temporarily removed `Proto.Cluster.Kubernetes` package and provider integration because of a vulnerability in its dependency (https://avd.aquasec.com/nvd/2025/cve-2025-9708). - Adjusted related cluster provider and remote configuration logic. - Updated `PortAttribute` default parameter for clarity. * Revert "Remove Proto.Cluster.Kubernetes dependency due to vulnerability" This reverts commit 0720d970968e4f7338825407258b34ddffb1d2a4. * Add KubernetesClient package and update MicrosoftVersion to 9.0.9 - Added `KubernetesClient` package to the project dependencies. - Updated `MicrosoftVersion` to `9.0.9` in `Directory.Packages.props`. Update Polly packages - Bump Polly and Polly.Extensions package versions to 8.6.3. Update `Microsoft.AspNetCore.Authorization` to use `MicrosoftVersion` property Ensure Docker images ship CA trust and add TLS smoke tests (#6918) Remove TlsSmoke project and related solution references - Deleted `TlsSmoke` project files (`Program.cs` and `TlsSmoke.csproj`). - Removed `TlsSmoke` project reference from the solution file (`Elsa.sln`). Add comprehensive Copilot coding agent instructions for repository onboarding (#6920) * Initial plan * Add comprehensive .github/copilot-instructions.md with validated build instructions Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> Add ForEach tests, introduce asynchronous workflow runner and enhance workflow events. (#6926) * Introduce asynchronous workflow runner and enhance workflow events. - Added `AsyncWorkflowRunner` to enable asynchronous workflow execution and result tracking. - Introduced new event arguments, such as `ActivityExecutedEventArgs` and `WorkflowStateCommittedEventArgs`. - Expanded `WorkflowEvents` class to include `ActivityExecuted`, `ActivityExecutedLogUpdated`, and `WorkflowStateCommitted` events. - Refactored event arguments into the `Elsa.Testing.Shared.EventArgs` namespace. - Enhanced tests with `AsyncWorkflowRunner` and new event-driven workflow scenarios. * Refactor event argument classes to unify namespace and simplify inheritance * Add shared component DotSettings file to support namespace exclusions Refactor `WaitAsync` call in `DispatchWorkflowsTests` to remove unnecessary generic type.
2025-09-25 18:58:21 +00:00
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Entities;
using Elsa.Workflows.Runtime.Messages;
Add BulkDispatchWorkflows tests and improve activity coverage (#7026) * Refactor BulkDispatchWorkflows tests and improve activity coverage - Migrate `BulkDispatchWorkflows` tests to a new structure under `Scenarios/BulkDispatch`. - Introduce comprehensive test cases for bulk dispatch scenarios, including empty items, correlation IDs, child ports, dictionary items, and invalid definitions. - Add multiple reusable workflows such as `BulkChildWorkflow`, `BulkDispatchAndWaitWorkflow`, and `BulkDispatchFireAndForgetWorkflow` for enhanced test coverage. - Update namespaces and project files to reflect the restructuring and new workflow definitions. - Enhance functionality for `IWorkflowInstanceVariableManager` by adding methods for variable retrieval from workflow states and instances. * Add additional assertions for WriteLine activity tests and update workflow builder in BulkDispatch tests - Enhanced `BulkDispatchWorkflowsTests` by validating written texts for accuracy. - Refactored `BulkDispatchFireAndForgetWorkflow` to update activity and namespace references. * Add `SlowBulkChildWorkflow` and update `BulkDispatchFireAndForgetWorkflow` test structure - Introduced `SlowBulkChildWorkflow` with a delay and logging functionality for bulk dispatch testing. - Modified `BulkDispatchFireAndForgetWorkflow` to use `SlowBulkChildWorkflow` and updated test items. - Enhanced `BulkDispatchWorkflowsTests` with child workflow completion validation and timing assertions. * Refactor BulkDispatch tests by removing dictionary item workflow and streamlining child workflow handling - Removed `BulkDispatchWithDictionaryItemsWorkflow` and associated test cases. - Updated `BulkChildWorkflow` to use input binding. - Streamlined child workflow event handling in `BulkDispatchWorkflowsTests` using a reusable helper method. - Enhanced `CorrelationIdFunction` support with JavaScript expression handling. * Move unit test workflows to `Scenarios/Activities` and update namespace references - Relocated all test workflows from `Scenarios/BulkDispatch` and `Scenarios/ExecuteWorkflows` to `Scenarios/Activities`. - Updated namespaces and imports to reflect the new folder structure. * Refactor BulkDispatch and FlowJoin test workflows: - Moved `BulkDispatch` workflows to `Scenarios/Activities/BulkDispatchWorkflows`. - Renamed `FlowJoins` to `FlowJoin` and updated namespaces to match. - Adjusted imports and references to align with new folder structure and updated class definitions. * Remove unused folder reference from `Elsa.Workflows.ComponentTests.csproj`. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/Activities/BulkDispatchWorkflows/BulkDispatchWorkflowsTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-04 14:44:44 +00:00
namespace Elsa.Testing.Shared.Services;
Add `WorkflowStateCommitted` notification support and update state handling logic - Introduced `WorkflowStateCommitted` notification to encapsulate workflow execution context, state, and instance details. - Updated `DefaultCommitStateHandler` to publish `WorkflowStateCommitted` via `IMediator`. - Adjusted `DispatchWorkflowExtensions` to use `WorkflowStateCommitted` for workflow completion. Updates KubernetesClient and Microsoft packages (#6917) * Remove Proto.Cluster.Kubernetes dependency due to vulnerability - Temporarily removed `Proto.Cluster.Kubernetes` package and provider integration because of a vulnerability in its dependency (https://avd.aquasec.com/nvd/2025/cve-2025-9708). - Adjusted related cluster provider and remote configuration logic. - Updated `PortAttribute` default parameter for clarity. * Revert "Remove Proto.Cluster.Kubernetes dependency due to vulnerability" This reverts commit 0720d970968e4f7338825407258b34ddffb1d2a4. * Add KubernetesClient package and update MicrosoftVersion to 9.0.9 - Added `KubernetesClient` package to the project dependencies. - Updated `MicrosoftVersion` to `9.0.9` in `Directory.Packages.props`. Update Polly packages - Bump Polly and Polly.Extensions package versions to 8.6.3. Update `Microsoft.AspNetCore.Authorization` to use `MicrosoftVersion` property Ensure Docker images ship CA trust and add TLS smoke tests (#6918) Remove TlsSmoke project and related solution references - Deleted `TlsSmoke` project files (`Program.cs` and `TlsSmoke.csproj`). - Removed `TlsSmoke` project reference from the solution file (`Elsa.sln`). Add comprehensive Copilot coding agent instructions for repository onboarding (#6920) * Initial plan * Add comprehensive .github/copilot-instructions.md with validated build instructions Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> Add ForEach tests, introduce asynchronous workflow runner and enhance workflow events. (#6926) * Introduce asynchronous workflow runner and enhance workflow events. - Added `AsyncWorkflowRunner` to enable asynchronous workflow execution and result tracking. - Introduced new event arguments, such as `ActivityExecutedEventArgs` and `WorkflowStateCommittedEventArgs`. - Expanded `WorkflowEvents` class to include `ActivityExecuted`, `ActivityExecutedLogUpdated`, and `WorkflowStateCommitted` events. - Refactored event arguments into the `Elsa.Testing.Shared.EventArgs` namespace. - Enhanced tests with `AsyncWorkflowRunner` and new event-driven workflow scenarios. * Refactor event argument classes to unify namespace and simplify inheritance * Add shared component DotSettings file to support namespace exclusions Refactor `WaitAsync` call in `DispatchWorkflowsTests` to remove unnecessary generic type.
2025-09-25 18:58:21 +00:00
/// <summary>
/// Provides functionality to execute workflows asynchronously and await their completion for testing purposes.
/// Tracks activity execution records and workflow completion signals.
/// </summary>
public class AsyncWorkflowRunner : IDisposable
{
private readonly IWorkflowRuntime _workflowRuntime;
private readonly IIdentityGenerator _identityGenerator;
private readonly SignalManager _signalManager;
private readonly WorkflowEvents _workflowEvents;
private readonly ConcurrentDictionary<string, ActivityExecutionRecord> _activityExecutionRecords = new();
/// <summary>
/// Initializes a new instance of the <see cref="AsyncWorkflowRunner"/> class.
/// </summary>
public AsyncWorkflowRunner(IWorkflowRuntime workflowRuntime, IIdentityGenerator identityGenerator, SignalManager signalManager, WorkflowEvents workflowEvents)
{
_workflowRuntime = workflowRuntime;
_identityGenerator = identityGenerator;
_signalManager = signalManager;
_workflowEvents = workflowEvents;
_workflowEvents.WorkflowStateCommitted += OnWorkflowStateCommitted;
_workflowEvents.ActivityExecutedLogUpdated += OnActivityExecutedLogUpdated;
}
/// <summary>
/// Runs the specified workflow definition asynchronously and waits for its completion.
/// Returns the workflow execution context and activity execution records.
/// </summary>
/// <param name="workflowDefinitionHandle">The handle of the workflow definition to execute.</param>
/// <returns>A <see cref="TestWorkflowExecutionResult"/> containing the workflow execution context and activity execution records.</returns>
public async Task<TestWorkflowExecutionResult> RunAndAwaitWorkflowCompletionAsync(WorkflowDefinitionHandle workflowDefinitionHandle)
{
var workflowInstanceId = _identityGenerator.GenerateId();
var workflowClient = await _workflowRuntime.CreateClientAsync(workflowInstanceId);
await workflowClient.CreateInstanceAsync(new()
{
WorkflowDefinitionHandle = workflowDefinitionHandle
});
_activityExecutionRecords.Clear();
await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty);
var signalName = GetSignalName(workflowInstanceId);
var workflowExecutionContext = await _signalManager.WaitAsync<WorkflowExecutionContext>(signalName);
return new(workflowExecutionContext, _activityExecutionRecords.Values.ToList());
}
public async Task RunWorkflowAsync(string definitionId, string? correlationId = null)
{
var workflowClient = await _workflowRuntime.CreateClientAsync();
await workflowClient.CreateInstanceAsync(new()
{
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Published),
CorrelationId = correlationId
});
await workflowClient.RunInstanceAsync(RunWorkflowInstanceRequest.Empty);
}
Add `WorkflowStateCommitted` notification support and update state handling logic - Introduced `WorkflowStateCommitted` notification to encapsulate workflow execution context, state, and instance details. - Updated `DefaultCommitStateHandler` to publish `WorkflowStateCommitted` via `IMediator`. - Adjusted `DispatchWorkflowExtensions` to use `WorkflowStateCommitted` for workflow completion. Updates KubernetesClient and Microsoft packages (#6917) * Remove Proto.Cluster.Kubernetes dependency due to vulnerability - Temporarily removed `Proto.Cluster.Kubernetes` package and provider integration because of a vulnerability in its dependency (https://avd.aquasec.com/nvd/2025/cve-2025-9708). - Adjusted related cluster provider and remote configuration logic. - Updated `PortAttribute` default parameter for clarity. * Revert "Remove Proto.Cluster.Kubernetes dependency due to vulnerability" This reverts commit 0720d970968e4f7338825407258b34ddffb1d2a4. * Add KubernetesClient package and update MicrosoftVersion to 9.0.9 - Added `KubernetesClient` package to the project dependencies. - Updated `MicrosoftVersion` to `9.0.9` in `Directory.Packages.props`. Update Polly packages - Bump Polly and Polly.Extensions package versions to 8.6.3. Update `Microsoft.AspNetCore.Authorization` to use `MicrosoftVersion` property Ensure Docker images ship CA trust and add TLS smoke tests (#6918) Remove TlsSmoke project and related solution references - Deleted `TlsSmoke` project files (`Program.cs` and `TlsSmoke.csproj`). - Removed `TlsSmoke` project reference from the solution file (`Elsa.sln`). Add comprehensive Copilot coding agent instructions for repository onboarding (#6920) * Initial plan * Add comprehensive .github/copilot-instructions.md with validated build instructions Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com> Add ForEach tests, introduce asynchronous workflow runner and enhance workflow events. (#6926) * Introduce asynchronous workflow runner and enhance workflow events. - Added `AsyncWorkflowRunner` to enable asynchronous workflow execution and result tracking. - Introduced new event arguments, such as `ActivityExecutedEventArgs` and `WorkflowStateCommittedEventArgs`. - Expanded `WorkflowEvents` class to include `ActivityExecuted`, `ActivityExecutedLogUpdated`, and `WorkflowStateCommitted` events. - Refactored event arguments into the `Elsa.Testing.Shared.EventArgs` namespace. - Enhanced tests with `AsyncWorkflowRunner` and new event-driven workflow scenarios. * Refactor event argument classes to unify namespace and simplify inheritance * Add shared component DotSettings file to support namespace exclusions Refactor `WaitAsync` call in `DispatchWorkflowsTests` to remove unnecessary generic type.
2025-09-25 18:58:21 +00:00
private void OnWorkflowStateCommitted(object? sender, WorkflowStateCommittedEventArgs e)
{
if (e.WorkflowExecutionContext.Status != WorkflowStatus.Finished)
return;
var signalName = GetSignalName(e.WorkflowExecutionContext.Id);
_signalManager.Trigger(signalName, e.WorkflowExecutionContext);
}
private void OnActivityExecutedLogUpdated(object? sender, ActivityExecutedLogUpdatedEventArgs e)
{
foreach (var record in e.Records)
_activityExecutionRecords[record.Id] = record;
}
private static string GetSignalName(string workflowInstanceId) => $"WorkflowInstanceCompleted-{workflowInstanceId}";
/// <summary>
/// Unsubscribes from workflow events and releases resources.
/// </summary>
public void Dispose()
{
_workflowEvents.WorkflowStateCommitted -= OnWorkflowStateCommitted;
_workflowEvents.ActivityExecutedLogUpdated -= OnActivityExecutedLogUpdated;
GC.SuppressFinalize(this);
}
}