Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event validation #1

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Fluss.Testing/AggregateTestBed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
public AggregateTestBed()
{
var validator = new Mock<IRootValidator>();
validator.Setup(v => v.ValidateEvent(It.IsAny<EventEnvelope>()))
.Returns<EventEnvelope>(_ => Task.CompletedTask);
validator.Setup(v => v.ValidateEvent(It.IsAny<EventEnvelope>(), It.IsAny<IReadOnlyList<EventEnvelope>?>()))
.Returns<EventEnvelope, IReadOnlyList<EventEnvelope>?>((_, _) => Task.CompletedTask);

Check warning on line 21 in src/Fluss.Testing/AggregateTestBed.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss.Testing/AggregateTestBed.cs#L21

Added line #L21 was not covered by tests
validator.Setup(v => v.ValidateAggregate(It.IsAny<AggregateRoot>(), It.IsAny<UnitOfWork.UnitOfWork>()))
.Returns<AggregateRoot, UnitOfWork.UnitOfWork>((_, _) => Task.CompletedTask);

Expand Down
4 changes: 2 additions & 2 deletions src/Fluss.UnitTest/Core/UnitOfWork/UnitOfWorkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public UnitOfWorkTest()
_policies = new List<Policy>();

_validator = new Mock<IRootValidator>(MockBehavior.Strict);
_validator.Setup(v => v.ValidateEvent(It.IsAny<EventEnvelope>()))
.Returns<EventEnvelope>(_ => Task.CompletedTask);
_validator.Setup(v => v.ValidateEvent(It.IsAny<EventEnvelope>(), It.IsAny<IReadOnlyList<EventEnvelope>?>()))
.Returns<EventEnvelope, IReadOnlyList<EventEnvelope>?>((_, _) => Task.CompletedTask);
_validator.Setup(v => v.ValidateAggregate(It.IsAny<AggregateRoot>(), It.IsAny<Fluss.UnitOfWork.UnitOfWork>()))
.Returns<AggregateRoot, Fluss.UnitOfWork.UnitOfWork>((_, _) => Task.CompletedTask);

Expand Down
7 changes: 6 additions & 1 deletion src/Fluss/UnitOfWork/UnitOfWork.Aggregates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ internal async ValueTask CommitInternal()
{
using var activity = FlussActivitySource.Source.StartActivity();

await Task.WhenAll(PublishedEventEnvelopes.Select(envelope => _validator.ValidateEvent(envelope)));
var validatedEnvelopes = new List<EventEnvelope>();
foreach (var envelope in PublishedEventEnvelopes)
{
await _validator.ValidateEvent(envelope, validatedEnvelopes);
validatedEnvelopes.Add(envelope);
}

await _eventRepository.Publish(PublishedEventEnvelopes);
_consistentVersion += PublishedEventEnvelopes.Count;
Expand Down
13 changes: 10 additions & 3 deletions src/Fluss/Validation/RootValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface IRootValidator
{
public Task ValidateEvent(EventEnvelope envelope);
public Task ValidateEvent(EventEnvelope envelope, IReadOnlyList<EventEnvelope>? PreviousEnvelopes = null);
public Task ValidateAggregate(AggregateRoot aggregate, Fluss.UnitOfWork.UnitOfWork unitOfWork);
}

Expand Down Expand Up @@ -54,11 +54,18 @@
}
}

public async Task ValidateEvent(EventEnvelope envelope)
public async Task ValidateEvent(EventEnvelope envelope, IReadOnlyList<EventEnvelope>? previousEnvelopes = null)
{
var unitOfWork = _serviceProvider.GetUserUnitOfWork(envelope.By ?? SystemUser.SystemUserGuid);

var versionedUnitOfWork = unitOfWork.WithPrefilledVersion(envelope.Version - 1);
var willBePublishedEnvelopes = previousEnvelopes ?? new List<EventEnvelope>();

var versionedUnitOfWork = unitOfWork.WithPrefilledVersion(envelope.Version - willBePublishedEnvelopes.Count - 1);

Check warning on line 63 in src/Fluss/Validation/RootValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss/Validation/RootValidator.cs#L63

Added line #L63 was not covered by tests
foreach (var willBePublishedEnvelope in willBePublishedEnvelopes)
{
versionedUnitOfWork.PublishedEventEnvelopes.Enqueue(willBePublishedEnvelope);
}

Check warning on line 67 in src/Fluss/Validation/RootValidator.cs

View check run for this annotation

Codecov / codecov/patch

src/Fluss/Validation/RootValidator.cs#L65-L67

Added lines #L65 - L67 were not covered by tests

var type = envelope.Event.GetType();

if (!_eventValidators.ContainsKey(type)) return;
Expand Down
Loading