Skip to content

Commit

Permalink
fix: writing failed events
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff2 committed Dec 9, 2024
1 parent e00f0b7 commit 640a3e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public UnitOfWorkTest()

_unitOfWorkFactory = new UnitOfWorkFactory(
new ServiceCollection()
.AddScoped(_ => GetUnitOfWork())
.AddTransient(_ => GetUnitOfWork())
.BuildServiceProvider());
}

Expand Down Expand Up @@ -332,6 +332,33 @@ await Assert.ThrowsAsync<InvalidOperationException>(async () =>
});
}

[Fact]
public async Task FailingCommitDoesNotCacheEventsToWrite()
{
_policies.Add(new AllowAllPolicy());

try
{
await _unitOfWorkFactory.Commit(async unitOfWork =>
{
var aggregate = await unitOfWork.GetAggregate<TestAggregate, int>(100);
await aggregate.Create();

throw new Exception();
});
}
catch
{
}

await _unitOfWorkFactory.Commit(_ => ValueTask.CompletedTask);

var unitOfWork = GetUnitOfWork();
var aggregate = await unitOfWork.GetAggregate<TestAggregate, int>(100);

Assert.False(aggregate.Exists);
}

private record TestRootReadModel : RootReadModel
{
public int GotEvents { get; private init; }
Expand Down
2 changes: 2 additions & 0 deletions src/Fluss/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public ValueTask Return()
_validator = null;
_userIdProvider = null;
_consistentVersion = null;

PublishedEventEnvelopes.Clear();
_readModels.Clear();
_isInstantiated = false;

Expand Down

0 comments on commit 640a3e6

Please sign in to comment.