-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed: in EF Core, when synchronously projecting multiple times in a …
…single tx (e.g. because projector itself publishes events), do not project the already projected events again
- Loading branch information
1 parent
9e5b2b6
commit 7557918
Showing
6 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
Providers/EFCore/Tests/Revo.EFCore.Tests/Projections/EFCoreSyncProjectionHookTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using NSubstitute; | ||
using Revo.Core.Commands; | ||
using Revo.Core.Events; | ||
using Revo.Core.Transactions; | ||
using Revo.Domain.Events; | ||
using Revo.EFCore.Projections; | ||
using Revo.Infrastructure.Projections; | ||
using Revo.Testing.Infrastructure; | ||
using Xunit; | ||
|
||
namespace Revo.EFCore.Tests.Projections; | ||
|
||
public class EFCoreSyncProjectionHookTests | ||
{ | ||
private EFCoreSyncProjectionHook sut; | ||
private ICommandContext commandContext = Substitute.For<ICommandContext>(); | ||
private IEFCoreProjectionSubSystem projectionSubSystem = Substitute.For<IEFCoreProjectionSubSystem>(); | ||
private IUnitOfWork unitOfWork = Substitute.For<IUnitOfWork>(); | ||
private IPublishEventBuffer eventBuffer = Substitute.For<IPublishEventBuffer>(); | ||
private List<IEventMessage> events = new(); | ||
|
||
public EFCoreSyncProjectionHookTests() | ||
{ | ||
unitOfWork.EventBuffer.Returns(eventBuffer); | ||
eventBuffer.Events.Returns(events); | ||
|
||
sut = new EFCoreSyncProjectionHook(commandContext, projectionSubSystem); | ||
} | ||
|
||
[Fact] | ||
public async Task ProjectsBeforeCommit() | ||
{ | ||
events.Add(new TestEvent().ToMessageDraft()); | ||
commandContext.UnitOfWork.Returns(unitOfWork); | ||
|
||
await sut.OnBeforeCommitAsync(); | ||
|
||
projectionSubSystem.Received(1).ExecuteProjectionsAsync( | ||
Arg.Is<IReadOnlyCollection<IEventMessage<DomainAggregateEvent>>>( | ||
evs => evs.SequenceEqual(events)), | ||
unitOfWork, | ||
Arg.Is<EFCoreEventProjectionOptions>(x => x.IsSynchronousProjection)); | ||
} | ||
|
||
[Fact] | ||
public async Task ProjectsOnlyAdditionalEvents() | ||
{ | ||
events.Add(new TestEvent().ToMessageDraft()); | ||
commandContext.UnitOfWork.Returns(unitOfWork); | ||
|
||
await sut.OnBeforeCommitAsync(); | ||
events.Add(new TestEvent().ToMessageDraft()); | ||
await sut.OnBeforeCommitAsync(); | ||
|
||
projectionSubSystem.ReceivedWithAnyArgs(2).ExecuteProjectionsAsync(null, null, null); | ||
|
||
projectionSubSystem.Received(1).ExecuteProjectionsAsync( | ||
Arg.Is<IReadOnlyCollection<IEventMessage<DomainAggregateEvent>>>( | ||
evs => evs.SequenceEqual(new[] { events[0] })), | ||
unitOfWork, | ||
Arg.Is<EFCoreEventProjectionOptions>(x => x.IsSynchronousProjection)); | ||
|
||
projectionSubSystem.Received(1).ExecuteProjectionsAsync( | ||
Arg.Is<IReadOnlyCollection<IEventMessage<DomainAggregateEvent>>>( | ||
evs => evs.SequenceEqual(new[] { events[1] })), | ||
unitOfWork, | ||
Arg.Is<EFCoreEventProjectionOptions>(x => x.IsSynchronousProjection)); | ||
} | ||
|
||
[Fact] | ||
public async Task ProjectsAgainAfterCommitSucceeded() | ||
{ | ||
events.Add(new TestEvent().ToMessageDraft()); | ||
commandContext.UnitOfWork.Returns(unitOfWork); | ||
|
||
await sut.OnBeforeCommitAsync(); | ||
await sut.OnCommitSucceededAsync(); | ||
await sut.OnBeforeCommitAsync(); | ||
|
||
projectionSubSystem.Received(2).ExecuteProjectionsAsync( | ||
Arg.Is<IReadOnlyCollection<IEventMessage<DomainAggregateEvent>>>( | ||
evs => evs.SequenceEqual(events)), | ||
unitOfWork, | ||
Arg.Is<EFCoreEventProjectionOptions>(x => x.IsSynchronousProjection)); | ||
} | ||
|
||
[Fact] | ||
public async Task ProjectsAgainAfterCommitFailed() | ||
{ | ||
events.Add(new TestEvent().ToMessageDraft()); | ||
commandContext.UnitOfWork.Returns(unitOfWork); | ||
|
||
await sut.OnBeforeCommitAsync(); | ||
await sut.OnCommitFailedAsync(); | ||
await sut.OnBeforeCommitAsync(); | ||
|
||
projectionSubSystem.Received(2).ExecuteProjectionsAsync( | ||
Arg.Is<IReadOnlyCollection<IEventMessage<DomainAggregateEvent>>>( | ||
evs => evs.SequenceEqual(events)), | ||
unitOfWork, | ||
Arg.Is<EFCoreEventProjectionOptions>(x => x.IsSynchronousProjection)); | ||
} | ||
|
||
private class TestEvent : DomainAggregateEvent | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters