public void GetRowKey_returns_combination_of_prefix_and_correlation_id() { var correlationId = Guid.NewGuid(); string actual = CorrelationTableEntity.GetRowKey(correlationId); actual.Should().Be($"{CorrelationTableEntity.RowKeyPrefix}-{correlationId:n}"); }
public void GetPartitionKey_returns_the_same_as_EventTableEntity() { var sourceType = typeof(FakeUser); var sourceId = Guid.NewGuid(); string actual = CorrelationTableEntity.GetPartitionKey(sourceType, sourceId); actual.Should().Be(EventTableEntity.GetPartitionKey(sourceType, sourceId)); }
private async Task InsertEventsAndCorrelation <T>( List <Envelope> envelopes, Guid?correlationId, CancellationToken cancellationToken) where T : class, IEventSourced { var batch = new TableBatchOperation(); var firstEvent = (IDomainEvent)envelopes.First().Message; Guid sourceId = firstEvent.SourceId; foreach (Envelope envelope in envelopes) { batch.Insert(EventTableEntity.FromEnvelope <T>(envelope, _serializer)); } if (correlationId.HasValue) { batch.Insert(CorrelationTableEntity.Create(typeof(T), sourceId, correlationId.Value)); } try { await _eventTable.ExecuteBatchAsync(batch, cancellationToken).ConfigureAwait(false); } catch (StorageException exception) when(correlationId.HasValue) { string filter = CorrelationTableEntity.GetFilter(typeof(T), sourceId, correlationId.Value); var query = new TableQuery <CorrelationTableEntity>().Where(filter); if (await _eventTable.Any(query, cancellationToken)) { throw new DuplicateCorrelationException( typeof(T), sourceId, correlationId.Value, exception); } throw; } }
public async Task SaveEvents_inserts_CorrelationTableEntity_correctly() { // Arrange var created = fixture.Create <FakeUserCreated>(); var events = new DomainEvent[] { created }; var correlationId = Guid.NewGuid(); RaiseEvents(userId, events); // Act var now = DateTimeOffset.Now; await sut.SaveEvents <FakeUser>(events, correlationId); // Assert string partitionKey = CorrelationTableEntity.GetPartitionKey(typeof(FakeUser), userId); string rowKey = CorrelationTableEntity.GetRowKey(correlationId); var query = new TableQuery <CorrelationTableEntity>().Where($"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'"); IEnumerable <CorrelationTableEntity> correlations = s_eventTable.ExecuteQuery(query); correlations.Should() .ContainSingle(e => e.CorrelationId == correlationId) .Which.HandledAt.ToLocalTime().Should().BeCloseTo(now, 1000); }