Skip to content

Commit 456e9d1

Browse files
David Garzasfmskywalker
authored andcommitted
Refactor commit strategy resolution for improved clarity and efficiency; remove unused example files and add a new CommitTracker helper for testing.
1 parent 5563de2 commit 456e9d1

7 files changed

Lines changed: 22 additions & 173 deletions

File tree

src/modules/Elsa.Workflows.Core/CommitStates/USAGE_EXAMPLE.md

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/modules/Elsa.Workflows.Core/Middleware/Activities/DefaultActivityInvokerMiddleware.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,10 @@ private async Task EvaluateInputPropertiesAsync(ActivityExecutionContext context
130130
private bool ShouldCommit(ActivityExecutionContext context, ActivityLifetimeEvent lifetimeEvent)
131131
{
132132
var strategyName = context.Activity.GetCommitStrategy();
133-
IActivityCommitStrategy? strategy;
134133

135-
if (!string.IsNullOrWhiteSpace(strategyName))
136-
{
137-
strategy = commitStrategyRegistry.FindActivityStrategy(strategyName);
138-
}
139-
else
140-
{
141-
// Fall back to the default strategy if configured
142-
strategy = commitStateOptions.Value.DefaultActivityCommitStrategy;
143-
}
134+
IActivityCommitStrategy? strategy = !string.IsNullOrWhiteSpace(strategyName)
135+
? commitStrategyRegistry.FindActivityStrategy(strategyName)
136+
: commitStateOptions.Value.DefaultActivityCommitStrategy;
144137

145138
var commitAction = CommitAction.Default;
146139

@@ -160,17 +153,9 @@ private bool ShouldCommit(ActivityExecutionContext context, ActivityLifetimeEven
160153
{
161154
var workflowStrategyName = context.WorkflowExecutionContext.Workflow.Options.CommitStrategyName;
162155

163-
IWorkflowCommitStrategy? workflowStrategy;
164-
165-
if (!string.IsNullOrWhiteSpace(workflowStrategyName))
166-
{
167-
workflowStrategy = commitStrategyRegistry.FindWorkflowStrategy(workflowStrategyName);
168-
}
169-
else
170-
{
171-
// Fall back to the default strategy if configured
172-
workflowStrategy = commitStateOptions.Value.DefaultWorkflowCommitStrategy;
173-
}
156+
IWorkflowCommitStrategy? workflowStrategy = !string.IsNullOrWhiteSpace(workflowStrategyName)
157+
? commitStrategyRegistry.FindWorkflowStrategy(workflowStrategyName)
158+
: commitStateOptions.Value.DefaultWorkflowCommitStrategy;
174159

175160
if (workflowStrategy == null)
176161
return false;

src/modules/Elsa.Workflows.Core/Middleware/Workflows/DefaultActivitySchedulerMiddleware.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,9 @@ private async Task ExecuteWorkItemAsync(WorkflowExecutionContext context, Activi
6464
private async Task ConditionallyCommitStateAsync(WorkflowExecutionContext context, WorkflowLifetimeEvent lifetimeEvent)
6565
{
6666
var strategyName = context.Workflow.Options.CommitStrategyName;
67-
IWorkflowCommitStrategy? strategy;
68-
69-
if (!string.IsNullOrWhiteSpace(strategyName))
70-
{
71-
strategy = commitStrategyRegistry.FindWorkflowStrategy(strategyName);
72-
}
73-
else
74-
{
75-
// Fall back to the default strategy if configured
76-
strategy = commitStateOptions.Value.DefaultWorkflowCommitStrategy;
77-
}
67+
IWorkflowCommitStrategy? strategy = !string.IsNullOrWhiteSpace(strategyName)
68+
? commitStrategyRegistry.FindWorkflowStrategy(strategyName)
69+
: commitStateOptions.Value.DefaultWorkflowCommitStrategy;
7870

7971
if (strategy == null)
8072
return;

test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/Tests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
using Elsa.Workflows.CommitStates;
44
using Elsa.Workflows.CommitStates.Strategies;
55
using Elsa.Workflows.CommitStates.Tasks;
6+
using Elsa.Workflows.IntegrationTests.SharedHelpers;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Options;
89
using Xunit.Abstractions;
910

1011
namespace Elsa.Workflows.IntegrationTests.Scenarios.DefaultActivityCommitStrategy;
1112

12-
public class Tests(ITestOutputHelper testOutputHelper)
13+
public class Tests
1314
{
14-
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
15+
private readonly ITestOutputHelper _testOutputHelper;
16+
17+
public Tests(ITestOutputHelper testOutputHelper)
18+
{
19+
_testOutputHelper = testOutputHelper;
20+
}
1521

1622
[Fact(DisplayName = "Activity without explicit strategy uses default commit strategy")]
1723
public async Task ActivityUsesDefaultCommitStrategy()
@@ -106,7 +112,7 @@ public async Task NoCommitsWithoutAnyStrategy()
106112
}
107113

108114
[Fact(DisplayName = "Default activity strategy is not visible in commit strategy registry")]
109-
public async Task DefaultStrategyNotInRegistry()
115+
public void DefaultStrategyNotInRegistry()
110116
{
111117
// Arrange
112118
var defaultStrategy = new ExecutedActivityStrategy();
@@ -128,7 +134,6 @@ public async Task DefaultStrategyNotInRegistry()
128134
Assert.Empty(activityStrategies);
129135
Assert.NotNull(options.Value.DefaultActivityCommitStrategy);
130136
Assert.Same(defaultStrategy, options.Value.DefaultActivityCommitStrategy);
131-
await Task.CompletedTask;
132137
}
133138

134139
[Fact(DisplayName = "Default activity strategy with standard strategies does not duplicate")]

test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/CommitTracker.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultWorkflowCommitStrategy/Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Elsa.Testing.Shared;
33
using Elsa.Workflows.CommitStates;
44
using Elsa.Workflows.CommitStates.Strategies;
5+
using Elsa.Workflows.IntegrationTests.SharedHelpers;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Options;
78
using Xunit.Abstractions;

test/integration/Elsa.Workflows.IntegrationTests/Scenarios/DefaultActivityCommitStrategy/CommitTracker.cs renamed to test/integration/Elsa.Workflows.IntegrationTests/SharedHelpers/CommitTracker.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Elsa.Workflows.CommitStates;
22
using Elsa.Workflows.State;
33

4-
namespace Elsa.Workflows.IntegrationTests.Scenarios.DefaultActivityCommitStrategy;
4+
namespace Elsa.Workflows.IntegrationTests.SharedHelpers;
55

66
/// <summary>
77
/// Test helper to track commit invocations
@@ -13,12 +13,14 @@ public class CommitTracker : ICommitStateHandler
1313
public Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default)
1414
{
1515
CommitCount++;
16+
1617
return Task.CompletedTask;
1718
}
1819

1920
public Task CommitAsync(WorkflowExecutionContext workflowExecutionContext, WorkflowState workflowState, CancellationToken cancellationToken = default)
2021
{
2122
CommitCount++;
23+
2224
return Task.CompletedTask;
2325
}
2426
}

0 commit comments

Comments
 (0)