From a53c18d519f07cf8e55b536bac183ede176d82d2 Mon Sep 17 00:00:00 2001 From: Marco Papst Date: Mon, 26 Aug 2024 09:53:37 +0200 Subject: [PATCH] Add Integration tests Fix Bug where the Tranaction ended in a Conflict due to duplicate Versions --- .../CosmosEventStream.cs | 10 ++++- src/Papst.EventStore.AzureCosmos/Logging.cs | 3 ++ .../CosmosEventStreamTests.cs | 37 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Papst.EventStore.AzureCosmos/CosmosEventStream.cs b/src/Papst.EventStore.AzureCosmos/CosmosEventStream.cs index 50c54bc..e9a42fb 100755 --- a/src/Papst.EventStore.AzureCosmos/CosmosEventStream.cs +++ b/src/Papst.EventStore.AzureCosmos/CosmosEventStream.cs @@ -370,7 +370,7 @@ private async Task CommitTransactionAsync(List even { EventStreamDocumentEntity document = new() { - Id = await idStrategy.GenerateIdAsync(StreamId, currentVersion, EventStreamDocumentType.Event), + Id = await idStrategy.GenerateIdAsync(StreamId, currentVersion + 1, EventStreamDocumentType.Event), DocumentId = evt.DocumentId, StreamId = StreamId, Version = currentVersion, @@ -388,7 +388,13 @@ private async Task CommitTransactionAsync(List even currentVersion++; } - await batch.ExecuteAsync(cancellationToken).ConfigureAwait(false); + TransactionalBatchResponse result = await batch.ExecuteAsync(cancellationToken).ConfigureAwait(false); + if (!result.IsSuccessStatusCode) + { + throw new EventStreamException(StreamId, $"Failed to commit events {string.Join(", ", result.Select(itm => $"{itm.StatusCode}"))}"); + } + + logger.TransactionCompleted(StreamId, result.Count); } catch (Exception e) { diff --git a/src/Papst.EventStore.AzureCosmos/Logging.cs b/src/Papst.EventStore.AzureCosmos/Logging.cs index 7775e76..820a701 100644 --- a/src/Papst.EventStore.AzureCosmos/Logging.cs +++ b/src/Papst.EventStore.AzureCosmos/Logging.cs @@ -23,4 +23,7 @@ internal static partial class Logging [LoggerMessage(LogLevel.Warning, "Exception during Transaction Commit of Stream {StreamId}")] public static partial void TransactionException(this ILogger logger, Exception ex, Guid streamId); + + [LoggerMessage(LogLevel.Information, "Successful Appended Transaction to Stream {StreamId} with {Count} events")] + public static partial void TransactionCompleted(this ILogger logger, Guid streamId, int count); } diff --git a/tests/Papst.EventStore.AzureCosmos.Tests/IntegrationTests/CosmosEventStreamTests.cs b/tests/Papst.EventStore.AzureCosmos.Tests/IntegrationTests/CosmosEventStreamTests.cs index 0401eff..5495f79 100755 --- a/tests/Papst.EventStore.AzureCosmos.Tests/IntegrationTests/CosmosEventStreamTests.cs +++ b/tests/Papst.EventStore.AzureCosmos.Tests/IntegrationTests/CosmosEventStreamTests.cs @@ -42,4 +42,41 @@ public async Task AppendAsync_ShouldAppendEvent(Guid streamId, Guid documentId) var events = await stream.ListAsync().ToListAsync(); events.Should().Contain(d => d.Id == documentId); } + + [Theory, AutoData] + public async Task AppendTransactionAsync_ShouldAppendEvents(Guid streamId) + { + // arrange + var services = _fixture.BuildServiceProvider(); + var store = services.GetRequiredService(); + var stream = await CreateStreamAsync(store, streamId); + + // act + var batch = await stream.CreateTransactionalBatchAsync(); + batch.Add(Guid.NewGuid(), new TestAppendedEvent()); + batch.Add(Guid.NewGuid(), new TestAppendedEvent()); + batch.Add(Guid.NewGuid(), new TestAppendedEvent()); + batch.Add(Guid.NewGuid(), new TestAppendedEvent()); + await batch.CommitAsync(); + + // assert + var events = await stream.ListAsync(0).ToListAsync(); + events.Should().HaveCount(5); + + } + [Theory, AutoData] + public async Task ListAsync_ShouldListEvents(Guid streamId) + { + // arrange + var services = _fixture.BuildServiceProvider(); + var store = services.GetRequiredService(); + var stream = await CreateStreamAsync(store, streamId, new TestAppendedEvent(), new TestAppendedEvent(), new TestAppendedEvent()); + + // act + var list = stream.ListAsync(0); + var events = await list.ToListAsync(); + + // assert + events.Count.Should().Be(4, "3 TestAppendedEvent + 1 TestCreatedEvent"); + } }