From 640a3e6fabf24109a7604024726699b60ee3c14b Mon Sep 17 00:00:00 2001 From: mvarendorff Date: Mon, 9 Dec 2024 10:02:13 +0100 Subject: [PATCH] fix: writing failed events --- .../Core/UnitOfWork/UnitOfWorkTest.cs | 29 ++++++++++++++++++- src/Fluss/UnitOfWork/UnitOfWork.cs | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs b/src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs index e6baf78..d186c5e 100644 --- a/src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs +++ b/src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs @@ -35,7 +35,7 @@ public UnitOfWorkTest() _unitOfWorkFactory = new UnitOfWorkFactory( new ServiceCollection() - .AddScoped(_ => GetUnitOfWork()) + .AddTransient(_ => GetUnitOfWork()) .BuildServiceProvider()); } @@ -332,6 +332,33 @@ await Assert.ThrowsAsync(async () => }); } + [Fact] + public async Task FailingCommitDoesNotCacheEventsToWrite() + { + _policies.Add(new AllowAllPolicy()); + + try + { + await _unitOfWorkFactory.Commit(async unitOfWork => + { + var aggregate = await unitOfWork.GetAggregate(100); + await aggregate.Create(); + + throw new Exception(); + }); + } + catch + { + } + + await _unitOfWorkFactory.Commit(_ => ValueTask.CompletedTask); + + var unitOfWork = GetUnitOfWork(); + var aggregate = await unitOfWork.GetAggregate(100); + + Assert.False(aggregate.Exists); + } + private record TestRootReadModel : RootReadModel { public int GotEvents { get; private init; } diff --git a/src/Fluss/UnitOfWork/UnitOfWork.cs b/src/Fluss/UnitOfWork/UnitOfWork.cs index ce7bd4a..7c49bb5 100644 --- a/src/Fluss/UnitOfWork/UnitOfWork.cs +++ b/src/Fluss/UnitOfWork/UnitOfWork.cs @@ -105,6 +105,8 @@ public ValueTask Return() _validator = null; _userIdProvider = null; _consistentVersion = null; + + PublishedEventEnvelopes.Clear(); _readModels.Clear(); _isInstantiated = false;