public async Task PublishPendingEvents_does_not_delete_pending_events_if_fails_to_send()
        {
            // Arrange
            var userId = Guid.NewGuid();

            var userCreated     = fixture.Create <FakeUserCreated>();
            var usernameChanged = fixture.Create <FakeUsernameChanged>();
            var domainEvents    = new DomainEvent[] { userCreated, usernameChanged };

            RaiseEvents(userId, domainEvents);

            var envelopes = new List <Envelope>(domainEvents.Select(e => new Envelope(e)));

            var batchOperation = new TableBatchOperation();
            var pendingEvents  = new List <PendingEventTableEntity>(
                envelopes.Select(e => PendingEventTableEntity.FromEnvelope <FakeUser>(e, serializer)));

            pendingEvents.ForEach(batchOperation.Insert);
            await s_eventTable.ExecuteBatchAsync(batchOperation);

            batchOperation.Clear();
            envelopes
            .Take(1)
            .Select(e => EventTableEntity.FromEnvelope <FakeUser>(e, serializer))
            .ForEach(batchOperation.Insert);
            await s_eventTable.ExecuteBatchAsync(batchOperation);

            Mock.Get(messageBus)
            .Setup(
                x =>
                x.SendBatch(
                    It.IsAny <IEnumerable <Envelope> >(),
                    It.IsAny <CancellationToken>()))
            .Throws(new InvalidOperationException());

            // Act
            try
            {
                await sut.PublishPendingEvents <FakeUser>(userId, CancellationToken.None);
            }
            catch (InvalidOperationException)
            {
            }

            // Assert
            string partitionKey         = PendingEventTableEntity.GetPartitionKey(typeof(FakeUser), userId);
            var    query                = new TableQuery <PendingEventTableEntity>().Where($"PartitionKey eq '{partitionKey}'");
            IEnumerable <object> actual = s_eventTable.ExecuteQuery(query).Select(e => e.RowKey);

            actual.ShouldAllBeEquivalentTo(pendingEvents.Select(e => e.RowKey));
        }
Example #2
0
        public Task PublishPendingEvents <T>(
            Guid sourceId,
            CancellationToken cancellationToken)
            where T : class, IEventSourced
        {
            if (sourceId == Guid.Empty)
            {
                throw new ArgumentException(
                          $"{sourceId} cannot be empty.", nameof(sourceId));
            }

            string pendingPartition = PendingEventTableEntity.GetPartitionKey(typeof(T), sourceId);

            return(Publish(pendingPartition, cancellationToken));
        }
Example #3
0
        public async Task SaveEvents_inserts_pending_event_entities_correctly()
        {
            // Arrange
            var created         = fixture.Create <FakeUserCreated>();
            var usernameChanged = fixture.Create <FakeUsernameChanged>();
            var events          = new DomainEvent[] { created, usernameChanged };
            var correlationId   = Guid.NewGuid();

            RaiseEvents(userId, events);

            // Act
            await sut.SaveEvents <FakeUser>(events, correlationId);

            // Assert
            string partitionKey = PendingEventTableEntity.GetPartitionKey(typeof(FakeUser), userId);
            var    query        = new TableQuery <PendingEventTableEntity>().Where($"PartitionKey eq '{partitionKey}'");
            IEnumerable <PendingEventTableEntity> pendingEvents = s_eventTable.ExecuteQuery(query);

            foreach (var t in pendingEvents.Zip(events, (pending, source) =>
                                                new { Pending = pending, Source = source }))
            {
                var actual = new
                {
                    t.Pending.RowKey,
                    t.Pending.PersistentPartition,
                    t.Pending.Version,
                    t.Pending.CorrelationId,
                    Message = serializer.Deserialize(t.Pending.EventJson)
                };
                actual.ShouldBeEquivalentTo(new
                {
                    RowKey = PendingEventTableEntity.GetRowKey(t.Source.Version),
                    PersistentPartition = EventTableEntity.GetPartitionKey(typeof(FakeUser), userId),
                    t.Source.Version,
                    CorrelationId = correlationId,
                    Message       = t.Source
                },
                                            opts => opts.RespectingRuntimeTypes());
            }
        }
        public async Task PublishPendingEvents_deletes_all_pending_events()
        {
            // Arrange
            var userId = Guid.NewGuid();

            var userCreated     = fixture.Create <FakeUserCreated>();
            var usernameChanged = fixture.Create <FakeUsernameChanged>();
            var domainEvents    = new DomainEvent[] { userCreated, usernameChanged };

            RaiseEvents(userId, domainEvents);

            var envelopes = new List <Envelope>(domainEvents.Select(e => new Envelope(e)));

            var batchOperation = new TableBatchOperation();

            envelopes
            .Select(e => PendingEventTableEntity.FromEnvelope <FakeUser>(e, serializer))
            .ForEach(batchOperation.Insert);
            await s_eventTable.ExecuteBatchAsync(batchOperation);

            batchOperation.Clear();
            envelopes
            .Take(1)
            .Select(e => EventTableEntity.FromEnvelope <FakeUser>(e, serializer))
            .ForEach(batchOperation.Insert);
            await s_eventTable.ExecuteBatchAsync(batchOperation);

            // Act
            await sut.PublishPendingEvents <FakeUser>(userId, CancellationToken.None);

            // Assert
            string partitionKey = PendingEventTableEntity.GetPartitionKey(typeof(FakeUser), userId);
            var    query        = new TableQuery <PendingEventTableEntity>().Where($"PartitionKey eq '{partitionKey}'");
            List <PendingEventTableEntity> actual = s_eventTable.ExecuteQuery(query).ToList();

            actual.Should().BeEmpty();
        }