|
| 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