Skip to content

Commit

Permalink
Merge pull request #215 from PapstIO/bugfix/transaction-conflict-cosmos
Browse files Browse the repository at this point in the history
Add Integration tests
  • Loading branch information
MPapst authored Aug 26, 2024
2 parents c7d5dc0 + a53c18d commit fa727e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Papst.EventStore.AzureCosmos/CosmosEventStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private async Task CommitTransactionAsync(List<EventStreamDocumentTemplate> 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,
Expand All @@ -388,7 +388,13 @@ private async Task CommitTransactionAsync(List<EventStreamDocumentTemplate> 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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Papst.EventStore.AzureCosmos/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEventStore>();
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<IEventStore>();
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");
}
}

0 comments on commit fa727e0

Please sign in to comment.