From cfcebd3c2cbc7fc67da8beecf08f0e1bf582c16e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 12 Jul 2022 21:13:59 +0200 Subject: [PATCH] Harden tests. --- .../Consume/EventConsumerProcessor.cs | 4 ++- .../Consume/EventConsumerState.cs | 29 +++++++------------ .../Consume/EventConsumerProcessorTests.cs | 7 +++-- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerProcessor.cs b/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerProcessor.cs index 9e165bff8..11f0529c2 100644 --- a/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerProcessor.cs +++ b/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerProcessor.cs @@ -175,6 +175,7 @@ namespace Squidex.Infrastructure.EventSourcing.Consume private async Task UpdateAsync(Func action, string? position, [CallerMemberName] string? caller = null) { + // We do not want to deal with concurrency in this class, therefore we just use a lock. using (await asyncLock.EnterAsync()) { var previousState = State; @@ -200,7 +201,8 @@ namespace Squidex.Infrastructure.EventSourcing.Consume State = previousState.Stopped(ex); } - if (State != previousState) + // The state is a record, therefore we can check for value equality. + if (!Equals(previousState, State)) { await state.WriteAsync(); } diff --git a/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerState.cs b/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerState.cs index f11cce66c..ccbabfcf1 100644 --- a/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerState.cs +++ b/backend/src/Squidex.Infrastructure/EventSourcing/Consume/EventConsumerState.cs @@ -7,19 +7,13 @@ using Squidex.Infrastructure.Reflection; +#pragma warning disable SA1313 // Parameter names should begin with lower-case letter + namespace Squidex.Infrastructure.EventSourcing.Consume { - public sealed class EventConsumerState + public sealed record EventConsumerState(string? Position, int Count, bool IsStopped = false, string? Error = null) { - public static readonly EventConsumerState Initial = new EventConsumerState(); - - public bool IsStopped { get; init; } - - public string? Error { get; init; } - - public string? Position { get; init; } - - public int Count { get; init; } + public static readonly EventConsumerState Initial = new EventConsumerState(null, 0); public bool IsPaused { @@ -32,16 +26,10 @@ namespace Squidex.Infrastructure.EventSourcing.Consume } public EventConsumerState() + : this(null, 0) { } - public EventConsumerState(string? position, int count) - { - Position = position; - - Count = count; - } - public EventConsumerState Handled(string position, int offset = 1) { return new EventConsumerState(position, Count + offset); @@ -49,7 +37,7 @@ namespace Squidex.Infrastructure.EventSourcing.Consume public EventConsumerState Stopped(Exception? ex = null) { - return new EventConsumerState(Position, Count) { IsStopped = true, Error = ex?.Message }; + return new EventConsumerState(Position, Count, true, ex?.Message); } public EventConsumerState Started() @@ -59,7 +47,10 @@ namespace Squidex.Infrastructure.EventSourcing.Consume public EventConsumerInfo ToInfo(string name) { - return SimpleMapper.Map(this, new EventConsumerInfo { Name = name }); + return SimpleMapper.Map(this, new EventConsumerInfo + { + Name = name + }); } } } diff --git a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Consume/EventConsumerProcessorTests.cs b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Consume/EventConsumerProcessorTests.cs index e0eaf6368..33b2e54ab 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Consume/EventConsumerProcessorTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Consume/EventConsumerProcessorTests.cs @@ -250,10 +250,13 @@ namespace Squidex.Infrastructure.EventSourcing.Consume } [Fact] - public async Task Should_invoke_and_update_position_if_event_received_batched() + public async Task Should_invoke_and_update_position_if_events_received_batched() { A.CallTo(() => eventConsumer.BatchSize) - .Returns(100); + .Returns(5); + + A.CallTo(() => eventConsumer.BatchDelay) + .Returns(int.MaxValue); await sut.InitializeAsync(default); await sut.ActivateAsync();