Skip to content

Commit 667abca

Browse files
authored
Merge pull request danielgerlag#895 from vladimir-kovalyuk/issues/894
Fix issue 894
2 parents a6c8408 + 0ba1b75 commit 667abca

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/WorkflowCore/Primitives/Foreach.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public override ExecutionResult Run(IStepExecutionContext context)
1515
{
1616
if (context.PersistenceData == null)
1717
{
18-
var values = Collection.Cast<object>();
18+
var values = Collection.Cast<object>().ToList();
19+
if (!values.Any())
20+
{
21+
return ExecutionResult.Next();
22+
}
23+
1924
if (RunParallel)
2025
{
2126
return ExecutionResult.Branch(new List<object>(values), new IteratorPersistenceData { ChildrenActive = true });

test/WorkflowCore.IntegrationTests/Scenarios/ForeachScenario.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class ForeachScenario : WorkflowTest<ForeachScenario.ForeachWorkflow, For
1818

1919
public class DoSomething : StepBody
2020
{
21-
2221
public override ExecutionResult Run(IStepExecutionContext context)
2322
{
2423
Step2Ticker++;
@@ -29,6 +28,9 @@ public override ExecutionResult Run(IStepExecutionContext context)
2928

3029
public class MyDataClass
3130
{
31+
public List<int> Numbers { get; set; } = new List<int>();
32+
33+
public bool IsParallel { get; set; } = true;
3234
}
3335

3436
public class ForeachWorkflow : IWorkflow<MyDataClass>
@@ -43,7 +45,7 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
4345
Step1Ticker++;
4446
return ExecutionResult.Next();
4547
})
46-
.ForEach(x => new List<int> { 2, 2, 3 })
48+
.ForEach(x => x.Numbers, x => x.IsParallel)
4749
.Do(x => x.StartWith<DoSomething>())
4850
.Then(context =>
4951
{
@@ -57,12 +59,18 @@ public void Build(IWorkflowBuilder<MyDataClass> builder)
5759
public ForeachScenario()
5860
{
5961
Setup();
62+
63+
Step1Ticker = 0;
64+
Step2Ticker = 0;
65+
Step3Ticker = 0;
66+
AfterLoopValue = 0;
67+
CheckSum = 0;
6068
}
6169

6270
[Fact]
6371
public void Scenario()
6472
{
65-
var workflowId = StartWorkflow(null);
73+
var workflowId = StartWorkflow(new MyDataClass { Numbers = new List<int> { 2, 2, 3 } });
6674
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
6775

6876
Step1Ticker.Should().Be(1);
@@ -73,5 +81,20 @@ public void Scenario()
7381
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
7482
UnhandledStepErrors.Count.Should().Be(0);
7583
}
84+
85+
[Fact]
86+
public void EmptyCollectionSequentialScenario()
87+
{
88+
var workflowId = StartWorkflow(new MyDataClass { IsParallel = false });
89+
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));
90+
91+
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
92+
UnhandledStepErrors.Count.Should().Be(0);
93+
Step1Ticker.Should().Be(1);
94+
Step2Ticker.Should().Be(0);
95+
Step3Ticker.Should().Be(1);
96+
AfterLoopValue.Should().Be(0);
97+
CheckSum.Should().Be(0);
98+
}
7699
}
77100
}

0 commit comments

Comments
 (0)