public async Task FlushCommands_absorbs_exception_caused_by_that_some_pending_command_already_deleted_since_loaded()
        {
            // Arrange
            var messageBus = new CompletableMessageBus();
            var serializer = new JsonMessageSerializer();
            var sut        = new SqlCommandPublisher(
                () => new ProcessManagerDbContext(_dbContextOptions),
                serializer,
                messageBus,
                Mock.Of <IScheduledMessageBus>());

            var processManager = new FakeProcessManager();

            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                db.PendingCommands.AddRange(
                    new[]
                {
                    new FakeCommand(),
                    new FakeCommand(),
                    new FakeCommand(),
                }
                    .Select(c => new Envelope(c))
                    .Select(e => PendingCommand.FromEnvelope(processManager, e, serializer)));
                await db.SaveChangesAsync();
            }

            // Act
            Func <Task> action = async() =>
            {
                Task flushTask = sut.FlushCommands(processManager.Id, CancellationToken.None);
                using (var db = new ProcessManagerDbContext(_dbContextOptions))
                {
                    List <PendingCommand> pendingCommands = await db
                                                            .PendingCommands
                                                            .Where(c => c.ProcessManagerId == processManager.Id)
                                                            .OrderByDescending(c => c.Id)
                                                            .Take(1)
                                                            .ToListAsync();

                    db.PendingCommands.RemoveRange(pendingCommands);
                    await db.SaveChangesAsync();
                }

                messageBus.Complete();
                await flushTask;
            };

            // Assert
            action.Should().NotThrow();
        }
Esempio n. 2
0
        public async Task FlushPendingEvents_absorbs_exception_caused_by_that_some_pending_event_already_deleted_since_loaded()
        {
            // Arrange
            var messageBus = new CompletableMessageBus();
            var sut        = new AzureEventPublisher(s_eventTable, s_serializer, messageBus);

            var fixture = new Fixture();
            var user    = new FakeUser(Guid.NewGuid(), fixture.Create <string>());

            user.ChangeUsername(fixture.Create <string>());

            var pendingEvents = new List <PendingEvent>(
                from message in user.FlushPendingEvents()
                let messageId = Guid.NewGuid()
                                let envelope = new Envelope <IDomainEvent>(messageId, message)
                                               select PendingEvent.Create(typeof(FakeUser), envelope, s_serializer));

            var batch = new TableBatchOperation();

            foreach (PendingEvent pendingEvent in pendingEvents)
            {
                batch.Insert(pendingEvent);
            }

            await s_eventTable.ExecuteBatchAsync(batch);

            // Act
            Func <Task> action = async() =>
            {
                Task flushTask = sut.FlushPendingEvents <FakeUser>(user.Id, CancellationToken.None);
                await s_eventTable.ExecuteAsync(TableOperation.Delete(pendingEvents.OrderBy(e => e.GetHashCode()).First()));

                messageBus.Complete();
                await flushTask;
            };

            // Assert
            action.ShouldNotThrow();
            string filter = PendingEvent.GetFilter(typeof(FakeUser), user.Id);
            var    query  = new TableQuery {
                FilterString = filter
            };
            TableQuerySegment actual = await s_eventTable.ExecuteQuerySegmentedAsync(query, default);

            actual.Should().BeEmpty();
        }