Skip to content

Commit 7879324

Browse files
authored
Merge pull request danielgerlag#1075 from badihi/master
Fixes a bug that occures when a compensation is nested in a branch
2 parents 2e46c11 + 4eb025c commit 7879324

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/WorkflowCore/Services/FluentBuilders/WorkflowBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public void AttachBranch(IWorkflowBuilder branch)
8484
if (step2.Children[i] == oldId)
8585
step2.Children[i] = step.Id;
8686
}
87+
88+
if (step2.CompensationStepId == oldId)
89+
{
90+
step2.CompensationStepId = step.Id;
91+
}
8792
}
8893
}
8994

@@ -104,6 +109,11 @@ public void AttachBranch(IWorkflowBuilder branch)
104109
if (step2.Children[i] == oldId)
105110
step2.Children[i] = step.Id;
106111
}
112+
113+
if (step2.CompensationStepId == oldId)
114+
{
115+
step2.CompensationStepId = step.Id;
116+
}
107117
}
108118
}
109119

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using WorkflowCore.Interface;
4+
using WorkflowCore.Models;
5+
using Xunit;
6+
using FluentAssertions;
7+
using WorkflowCore.Testing;
8+
9+
namespace WorkflowCore.IntegrationTests.Scenarios
10+
{
11+
public class ForeachWithCompensationScenario : WorkflowTest<ForeachWithCompensationScenario.ForeachWorkflow, ForeachWithCompensationScenario.MyDataClass>
12+
{
13+
internal static int Step1Ticker = 0;
14+
internal static int Step2Ticker = 0;
15+
internal static int Step3Ticker = 0;
16+
internal static int CompensateTicker = 0;
17+
18+
public class MyDataClass
19+
{
20+
}
21+
22+
public class ForeachWorkflow : IWorkflow<MyDataClass>
23+
{
24+
public string Id => "ForeachWithCompensationWorkflow";
25+
public int Version => 1;
26+
public void Build(IWorkflowBuilder<MyDataClass> builder)
27+
{
28+
builder
29+
.StartWith(data => {
30+
Step1Ticker++;
31+
})
32+
.Then(data => {
33+
Step2Ticker++;
34+
})
35+
.ForEach(step => new List<int> { 1 })
36+
.Do(then => then
37+
.Decide(data => 1)
38+
.Branch(1, builder.CreateBranch()
39+
.StartWith(data => {
40+
Step3Ticker++;
41+
throw new Exception();
42+
})
43+
.CompensateWithSequence(builder => builder.StartWith(_ => {
44+
CompensateTicker++;
45+
})))
46+
);
47+
}
48+
}
49+
50+
public ForeachWithCompensationScenario()
51+
{
52+
Setup();
53+
}
54+
55+
[Fact]
56+
public void Scenario()
57+
{
58+
var workflowId = StartWorkflow(new MyDataClass());
59+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
60+
61+
Step1Ticker.Should().Be(1);
62+
Step2Ticker.Should().Be(1);
63+
Step3Ticker.Should().Be(1);
64+
CompensateTicker.Should().Be(1);
65+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
66+
UnhandledStepErrors.Count.Should().Be(1);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)