diff --git a/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerGrain.cs b/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerGrain.cs index d99ef13cd..c60440161 100644 --- a/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerGrain.cs +++ b/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerGrain.cs @@ -178,7 +178,7 @@ namespace Squidex.Infrastructure.EventSourcing.Grains await ClearAsync(); - State = EventConsumerState.Reset(); + State = EventConsumerState.Initial; Subscribe(); }); diff --git a/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerState.cs b/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerState.cs index 84a943dff..32461ecfc 100644 --- a/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerState.cs +++ b/backend/src/Squidex.Infrastructure/EventSourcing/Grains/EventConsumerState.cs @@ -12,13 +12,15 @@ namespace Squidex.Infrastructure.EventSourcing.Grains { public sealed class EventConsumerState { - public bool IsStopped { get; set; } + public static readonly EventConsumerState Initial = new EventConsumerState(); - public int Count { get; set; } + public bool IsStopped { get; init; } - public string? Error { get; set; } + public string? Error { get; init; } - public string? Position { get; set; } + public string? Position { get; init; } + + public int Count { get; init; } public bool IsPaused { @@ -41,11 +43,6 @@ namespace Squidex.Infrastructure.EventSourcing.Grains Count = count; } - public static EventConsumerState Reset() - { - return new EventConsumerState(); - } - public EventConsumerState Handled(string position, int offset = 1) { return new EventConsumerState(position, Count + offset); @@ -58,7 +55,7 @@ namespace Squidex.Infrastructure.EventSourcing.Grains public EventConsumerState Started() { - return new EventConsumerState(Position, Count) { IsStopped = false }; + return new EventConsumerState(Position, Count); } public EventConsumerInfo ToInfo(string name) diff --git a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerGrainTests.cs b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerGrainTests.cs index 593944228..185638518 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerGrainTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerGrainTests.cs @@ -70,7 +70,10 @@ namespace Squidex.Infrastructure.EventSourcing.Grains public EventConsumerGrainTests() { - grainState.Value.Position = initialPosition; + grainState.Value = new EventConsumerState + { + Position = initialPosition + }; consumerName = eventConsumer.GetType().Name; @@ -540,4 +543,4 @@ namespace Squidex.Infrastructure.EventSourcing.Grains grainState.Value.Should().BeEquivalentTo(state); } } -} \ No newline at end of file +} diff --git a/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerStateTests.cs b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerStateTests.cs new file mode 100644 index 000000000..a2db439af --- /dev/null +++ b/backend/tests/Squidex.Infrastructure.Tests/EventSourcing/Grains/EventConsumerStateTests.cs @@ -0,0 +1,32 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using FluentAssertions; +using Squidex.Infrastructure.TestHelpers; +using Xunit; + +namespace Squidex.Infrastructure.EventSourcing.Grains +{ + public class EventConsumerStateTests + { + [Fact] + public void Should_serialize_and_deserialize() + { + var state = new EventConsumerState + { + Count = 1, + IsStopped = true, + Error = "Error", + Position = "Position" + }; + + var serialized = state.SerializeAndDeserialize(); + + serialized.Should().BeEquivalentTo(state); + } + } +}