Add project files.

This commit is contained in:
2025-08-18 17:11:43 +03:30
parent 807dc263be
commit e5eb10d604
3943 changed files with 223719 additions and 0 deletions
@@ -0,0 +1,13 @@
using Elsa.Extensions;
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables.Activities;
public class CountdownStep : Activity
{
protected override void Execute(ActivityExecutionContext context)
{
var counter = context.GetVariable<int>("Counter");
context.SetVariable("Counter", counter - 1);
context?.CreateBookmark();
}
}
@@ -0,0 +1,71 @@
using Elsa.Common.Models;
using Elsa.Expressions.Helpers;
using Elsa.Extensions;
using Elsa.Workflows.ComponentTests.Abstractions;
using Elsa.Workflows.ComponentTests.Fixtures;
using Elsa.Workflows.ComponentTests.Scenarios.Variables.Workflows;
using Elsa.Workflows.Management;
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Entities;
using Elsa.Workflows.Runtime.Filters;
using Elsa.Workflows.Runtime.Messages;
using Elsa.Workflows.State;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables;
public class CountdownWorkflowTests(App app) : AppComponentTest(app)
{
[Fact(DisplayName = "Variable is persisted across workflow runs")]
public async Task VariableIsPersistedAcrossWorkflowRuns()
{
var workflowRuntime = Scope.ServiceProvider.GetRequiredService<IWorkflowRuntime>();
var workflowClient = await workflowRuntime.CreateClientAsync();
var workflowInstanceStore = Scope.ServiceProvider.GetRequiredService<IWorkflowInstanceStore>();
var bookmarkStore = Scope.ServiceProvider.GetRequiredService<IBookmarkStore>();
var runAndCreateRequest = new CreateAndRunWorkflowInstanceRequest
{
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(CountdownWorkflow.DefinitionId, VersionOptions.Latest),
};
var runResponse = await workflowClient.CreateAndRunInstanceAsync(runAndCreateRequest);
var workflowInstanceId = runResponse.WorkflowInstanceId;
var createdBookmarks = await bookmarkStore.FindManyAsync(new()
{
WorkflowInstanceId = workflowInstanceId
});
var bookmarks = new Stack<StoredBookmark>(createdBookmarks);
var expectedCounter = 3;
while (bookmarks.Any())
{
var workflowInstance = await workflowInstanceStore.FindAsync(workflowInstanceId);
var workflowState = workflowInstance!.WorkflowState;
var rootWorkflowActivityExecutionContext = workflowState.ActivityExecutionContexts.Single(x => x.ParentContextId == null);
var variables = GetVariablesDictionary(rootWorkflowActivityExecutionContext);
var actualCounter = variables["counterVariable"].ConvertTo<int>();
Assert.Equal(--expectedCounter, actualCounter);
var bookmark = bookmarks.Pop();
var runRequest = new RunWorkflowInstanceRequest
{
BookmarkId = bookmark.Id,
};
await workflowClient.RunInstanceAsync(runRequest);
createdBookmarks = await bookmarkStore.FindManyAsync(new()
{
WorkflowInstanceId = workflowInstanceId
});
foreach (var newBookmark in createdBookmarks)
bookmarks.Push(newBookmark);
}
}
private VariablesDictionary GetVariablesDictionary(ActivityExecutionContextState context)
{
return context.Properties.GetOrAdd(WorkflowInstanceStorageDriver.VariablesDictionaryStateKey, () => new VariablesDictionary());
}
}
@@ -0,0 +1,34 @@
using Elsa.Extensions;
using Elsa.Workflows.Activities;
using Elsa.Workflows.ComponentTests.Scenarios.Variables.Activities;
namespace Elsa.Workflows.ComponentTests.Scenarios.Variables.Workflows;
public class CountdownWorkflow : WorkflowBase
{
public static readonly string DefinitionId = Guid.NewGuid().ToString();
protected override void Build(IWorkflowBuilder builder)
{
builder.WithDefinitionId(DefinitionId);
var counter = builder.WithVariable("Counter", 3).WithWorkflowStorage();
builder.Root = new Sequence
{
Activities =
{
new While(context => counter.Get(context) > 0)
{
Body = new Sequence
{
Activities =
{
new WriteLine(context => $"Counter: {counter.Get(context)}"),
new CountdownStep()
}
}
}
}
};
}
}