public async Task ExceptionAndCompensate() { var container = CreateContainer(); await using var scope = container.BeginLifetimeScope(); var masterIdentity = new ClaptrapIdentity(Guid.NewGuid().ToString(), "TestCode"); await using var claptrap = scope.CreateSagaClaptrap(masterIdentity, "testFlow", typeof(TestFlowData)); var sagaFlow = SagaFlowBuilder.Create() .WithStep <TestStep1, CompensateStep1>() .WithStep <ExceptionStep, CompensateStep2>() .WithUserData(new TestFlowData()) .Build(); var claptrapAccessor = scope.Resolve <IClaptrapAccessor>(); await claptrap.RunAsync(sagaFlow); var claptrapStateData = (SagaStateData <TestFlowData>)claptrapAccessor.Claptrap.State.Data; var flowState = claptrapStateData.SagaFlowState; flowState.IsCompleted.Should().BeFalse(); flowState.IsCompensating.Should().BeFalse(); flowState.IsCompensated.Should().BeTrue(); flowState.StepStatuses.Should().Equal(StepStatus.Completed, StepStatus.Error); flowState.CompensateStepStatuses.Should().Equal(StepStatus.Completed, StepStatus.Completed); var flowData = (TestFlowData)claptrapStateData.GetUserData(); flowData.Test1.Should().BeFalse(); flowData.Test2.Should().BeFalse(); await claptrap.RunAsync(sagaFlow); }
public async Task Success() { var container = CreateContainer(); await using var scope = container.BeginLifetimeScope(); var masterIdentity = new ClaptrapIdentity(Guid.NewGuid().ToString(), "TestCode"); await using var claptrap = scope.CreateSagaClaptrap(masterIdentity, "testFlow", typeof(TestFlowData)); var sagaFlow = SagaFlowBuilder.Create() .WithStep <TestStep1, CompensateStep1>() .WithStep <TestStep2, CompensateStep2>() .WithUserData(new TestFlowData()) .Build(); await claptrap.RunAsync(sagaFlow); var claptrapAccessor = scope.Resolve <IClaptrapAccessor>(); var claptrapStateData = (SagaStateData <TestFlowData>)claptrapAccessor.Claptrap.State.Data; claptrapStateData.SagaFlowState.IsCompleted.Should().BeTrue(); var flowData = (TestFlowData)claptrapStateData.GetUserData(); flowData.Test1.Should().BeTrue(); flowData.Test2.Should().BeTrue(); // there will be nothing change since flow completed. await claptrap.RunAsync(sagaFlow); }