|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using WorkflowCore.Interface; |
| 5 | +using WorkflowCore.Models; |
| 6 | +using Xunit; |
| 7 | +using FluentAssertions; |
| 8 | +using System.Linq; |
| 9 | +using WorkflowCore.Testing; |
| 10 | + |
| 11 | +namespace WorkflowCore.IntegrationTests.Scenarios |
| 12 | +{ |
| 13 | + public class MultistepCompensationScenario : WorkflowTest<MultistepCompensationScenario.Workflow, Object> |
| 14 | + { |
| 15 | + public class Workflow : IWorkflow |
| 16 | + { |
| 17 | + public static int Compensation1Fired = -1; |
| 18 | + public static int Compensation2Fired = -1; |
| 19 | + public static int Compensation3Fired = -1; |
| 20 | + public static int Compensation4Fired = -1; |
| 21 | + public static int CompensationCounter = 0; |
| 22 | + |
| 23 | + public string Id => "CompensatingWorkflow"; |
| 24 | + public int Version => 1; |
| 25 | + public void Build(IWorkflowBuilder<object> builder) |
| 26 | + { |
| 27 | + builder |
| 28 | + .StartWith(context => ExecutionResult.Next()) |
| 29 | + .Saga(x => x |
| 30 | + .StartWith(context => ExecutionResult.Next()) |
| 31 | + .CompensateWith(context => |
| 32 | + { |
| 33 | + CompensationCounter++; |
| 34 | + Compensation1Fired = CompensationCounter; |
| 35 | + }) |
| 36 | + .Then(context => ExecutionResult.Next()) |
| 37 | + .CompensateWith(context => |
| 38 | + { |
| 39 | + CompensationCounter++; |
| 40 | + Compensation2Fired = CompensationCounter; |
| 41 | + }) |
| 42 | + .Then(context => ExecutionResult.Next()) |
| 43 | + .CompensateWith(context => |
| 44 | + { |
| 45 | + CompensationCounter++; |
| 46 | + Compensation3Fired = CompensationCounter; |
| 47 | + }) |
| 48 | + .Then(context => throw new Exception()) |
| 49 | + .CompensateWith(context => |
| 50 | + { |
| 51 | + CompensationCounter++; |
| 52 | + Compensation4Fired = CompensationCounter; |
| 53 | + }) |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public MultistepCompensationScenario() |
| 59 | + { |
| 60 | + Setup(); |
| 61 | + Workflow.Compensation1Fired = -1; |
| 62 | + Workflow.Compensation2Fired = -1; |
| 63 | + Workflow.Compensation3Fired = -1; |
| 64 | + Workflow.Compensation4Fired = -1; |
| 65 | + Workflow.CompensationCounter = 0; |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void MultiCompensationStepOrder() |
| 70 | + { |
| 71 | + var workflowId = StartWorkflow(null); |
| 72 | + WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30)); |
| 73 | + |
| 74 | + GetStatus(workflowId).Should().Be(WorkflowStatus.Complete); |
| 75 | + UnhandledStepErrors.Count.Should().Be(1); |
| 76 | + |
| 77 | + Workflow.Compensation1Fired.Should().Be(4); |
| 78 | + Workflow.Compensation2Fired.Should().Be(3); |
| 79 | + Workflow.Compensation3Fired.Should().Be(2); |
| 80 | + Workflow.Compensation4Fired.Should().Be(1); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments