Skip to content

Commit 07ed1e6

Browse files
Merge pull request #906 from TransactionProcessing/bug/#merchantfeesettledevent_retry_on_wrongexpectedversion_exception
add retry to MerchantFeeSettledEvent
2 parents bf400ea + b3584b5 commit 07ed1e6

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantStatementDomainEventHandlerTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class MerchantStatementDomainEventHandlerTests : DomainEventHandlerTests
2222
public MerchantStatementDomainEventHandlerTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
2323
{
2424
this.EventHandler = new MerchantStatementDomainEventHandler(this.Mediator.Object);
25+
Logger.Initialise(new NullLogger());
2526
}
2627

2728
[Fact]
@@ -64,6 +65,37 @@ public async Task MerchantStatementDomainEventHandler_Handle_MerchantFeeSettledE
6465
result.IsSuccess.ShouldBeTrue();
6566
}
6667

68+
[Fact]
69+
public async Task MerchantStatementDomainEventHandler_Handle_MerchantFeeSettledEvent_WrongExpectedRetried_EventIsHandled()
70+
{
71+
List<String> errors = new() { "WrongExpectedVersion" };
72+
73+
this.Mediator.SetupSequence(m => m.Send(It.IsAny<IRequest<Result>>(), It.IsAny<CancellationToken>()))
74+
.ReturnsAsync(Result.Failure(errors))
75+
.ReturnsAsync(Result.Success());
76+
77+
Result result = await this.EventHandler.Handle(TestData.DomainEvents.MerchantFeeSettledEvent, CancellationToken.None);
78+
result.IsSuccess.ShouldBeTrue();
79+
this.Mediator.Verify(m => m.Send(It.IsAny<IRequest<Result>>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
80+
}
81+
82+
[Fact]
83+
public async Task MerchantStatementDomainEventHandler_Handle_MerchantFeeSettledEvent_WrongExpectedRetried_AllRetriesFailed()
84+
{
85+
List<String> errors = new() { "WrongExpectedVersion" };
86+
this.Mediator.SetupSequence(m => m.Send(It.IsAny<IRequest<Result>>(), It.IsAny<CancellationToken>()))
87+
.ReturnsAsync(Result.Failure(errors))
88+
.ReturnsAsync(Result.Failure(errors))
89+
.ReturnsAsync(Result.Failure(errors))
90+
.ReturnsAsync(Result.Failure(errors))
91+
.ReturnsAsync(Result.Failure(errors))
92+
.ReturnsAsync(Result.Failure(errors));
93+
94+
Result result = await this.EventHandler.Handle(TestData.DomainEvents.MerchantFeeSettledEvent, CancellationToken.None);
95+
result.IsFailed.ShouldBeTrue();
96+
this.Mediator.Verify(m => m.Send(It.IsAny<IRequest<Result>>(), It.IsAny<CancellationToken>()), Times.Exactly(6));
97+
}
98+
6799
[Fact]
68100
public async Task MerchantStatementDomainEventHandler_Handle_StatementCreatedForDateEvent_EventIsHandled()
69101
{

TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System;
2-
using MediatR;
1+
using MediatR;
32
using Polly;
3+
using Prometheus;
44
using Shared.DomainDrivenDesign.EventSourcing;
5+
using Shared.EventStore.Aggregate;
56
using Shared.EventStore.EventHandling;
7+
using Shared.Exceptions;
8+
using Shared.Logger;
9+
using Shared.ValueObjects;
610
using SimpleResults;
11+
using System;
712
using System.Diagnostics;
813
using System.IO;
914
using System.Net.Http;
1015
using System.Threading;
1116
using System.Threading.Tasks;
12-
using Prometheus;
13-
using Shared.Logger;
1417
using TransactionProcessor.BusinessLogic.Common;
1518
using TransactionProcessor.BusinessLogic.Requests;
1619
using TransactionProcessor.BusinessLogic.Services;
1720
using TransactionProcessor.DomainEvents;
18-
using Shared.EventStore.Aggregate;
19-
using Shared.ValueObjects;
2021

2122
namespace TransactionProcessor.BusinessLogic.EventHandling
2223
{
@@ -144,12 +145,32 @@ private async Task<Result> HandleSpecificDomainEvent(MerchantDomainEvents.Withdr
144145
private async Task<Result> HandleSpecificDomainEvent(SettlementDomainEvents.MerchantFeeSettledEvent domainEvent,
145146
CancellationToken cancellationToken)
146147
{
147-
MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.FeeCalculatedDateTime,
148-
PositiveMoney.Create(Money.Create(domainEvent.CalculatedValue)), domainEvent.TransactionId, domainEvent.FeeId);
148+
IAsyncPolicy<Result> retryPolicy = PolicyFactory.CreatePolicy(policyTag: "MerchantStatementDomainEventHandler - HandleSpecificDomainEvent<SettlementDomainEvents.MerchantFeeSettledEvent>");
149+
150+
try
151+
{
152+
return await PolicyFactory.ExecuteWithPolicyAsync(async () =>
153+
{
154+
MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.FeeCalculatedDateTime,
155+
PositiveMoney.Create(Money.Create(domainEvent.CalculatedValue)), domainEvent.TransactionId, domainEvent.FeeId);
156+
157+
Result result = await this.Mediator.Send(command, cancellationToken);
158+
return result;
159+
160+
}, retryPolicy, "MerchantStatementDomainEventHandler - HandleSpecificDomainEvent<SettlementDomainEvents.MerchantFeeSettledEvent>");
161+
}
162+
catch (Exception ex)
163+
{
164+
return Result.Failure(ex.GetExceptionMessages());
165+
}
166+
//MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.FeeCalculatedDateTime,
167+
// PositiveMoney.Create(Money.Create(domainEvent.CalculatedValue)), domainEvent.TransactionId, domainEvent.FeeId);
168+
169+
////return await this.Mediator.Send(command, cancellationToken);
170+
//Result result = await this.Mediator.Send(command, cancellationToken);
171+
//return result;
172+
149173

150-
//return await this.Mediator.Send(command, cancellationToken);
151-
Result result = await this.Mediator.Send(command, cancellationToken);
152-
return result;
153174
}
154175

155176
#endregion

0 commit comments

Comments
 (0)