Skip to content

Commit

Permalink
ClaimCertificateCommandHandler correctly handles transient exceptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
tnickelsen authored Sep 17, 2024
1 parent ae6ac5c commit 71cca02
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NSubstitute.ReceivedExtensions;
using ProjectOrigin.WalletSystem.Server;
using ProjectOrigin.WalletSystem.Server.Activities.Exceptions;
using ProjectOrigin.WalletSystem.Server.CommandHandlers;
using ProjectOrigin.WalletSystem.Server.Database;
using ProjectOrigin.WalletSystem.Server.Models;
Expand Down Expand Up @@ -60,6 +63,35 @@ public async Task MatchingSlices()
_processBuilder.ReceivedCalls().Count().Should().Be(2);
}

[Fact]
public async Task ReserveQuantityThrowsTransientException_TransientException()
{
// arrange
var command = new ClaimCertificateCommand
{
Owner = _owner,
ClaimId = Guid.NewGuid(),
ConsumptionRegistry = _registryName,
ConsumptionCertificateId = Guid.NewGuid(),
ProductionRegistry = _registryName,
ProductionCertificateId = Guid.NewGuid(),
Quantity = 123
};
_context.Message.Returns(command);
_unitOfWork.CertificateRepository.ReserveQuantity(
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<Guid>(),
Arg.Any<uint>())
.ThrowsAsync(_ => throw new TransientException("Failed to handle claim at this time."));

// act
var sut = () => _commandHandler.Consume(_context);

// assert
await sut.Should().ThrowAsync<TransientException>();
}

[Fact]
public async Task SingleConsumption_TwoProduction_NoRemainder()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MassTransit;
using MassTransit.Courier.Contracts;
using Microsoft.Extensions.Logging;
using ProjectOrigin.WalletSystem.Server.Activities.Exceptions;
using ProjectOrigin.WalletSystem.Server.Database;
using ProjectOrigin.WalletSystem.Server.Extensions;
using ProjectOrigin.WalletSystem.Server.Models;
Expand Down Expand Up @@ -62,6 +63,12 @@ public async Task Consume(ConsumeContext<ClaimCertificateCommand> context)
_unitOfWork.Rollback();
_logger.LogWarning(ex, "Claim is not allowed.");
}
catch (TransientException ex)
{
_unitOfWork.Rollback();
_logger.LogWarning(ex, "Failed to handle claim at this time.");
throw;
}
catch (Exception ex)
{
_unitOfWork.Rollback();
Expand Down

0 comments on commit 71cca02

Please sign in to comment.