Beispiel #1
0
        public void FromScheduledEnvelope_generates_PendingScheduledCommand_correctly()
        {
            // Arrange
            var random         = new Random();
            var processManager = new FakeProcessManager();
            var messageId      = Guid.NewGuid();
            var correlationId  = Guid.NewGuid();
            var message        = new FakeCommand
            {
                Int32Value  = random.Next(),
                StringValue = Guid.NewGuid().ToString(),
            };
            var scheduledEnvelope =
                new ScheduledEnvelope(
                    new Envelope(messageId, message, correlationId: correlationId),
                    DateTime.Now.AddTicks(random.Next()));
            var serializer = new JsonMessageSerializer();

            // Act
            var actual = PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer);

            // Assert
            actual.ProcessManagerType.Should().Be(typeof(FakeProcessManager).FullName);
            actual.ProcessManagerId.Should().Be(processManager.Id);
            actual.MessageId.Should().Be(messageId);
            actual.CorrelationId.Should().Be(correlationId);
            actual.CommandJson.Should().Be(serializer.Serialize(message));
            actual.ScheduledTimeUtc.Should().Be(scheduledEnvelope.ScheduledTimeUtc);
        }
 private ScheduledEnvelope RestoreScheduledEnvelope(PendingScheduledCommand scheduledCommand) =>
 new ScheduledEnvelope(
     new Envelope(
         scheduledCommand.MessageId,
         _serializer.Deserialize(scheduledCommand.CommandJson),
         correlationId: scheduledCommand.CorrelationId),
     scheduledCommand.ScheduledTimeUtc);
        public async Task FlushCommands_sends_all_scheduled_commands_associated_with_specified_process_manager_sequentially()
        {
            // Arrange
            var serializer          = new JsonMessageSerializer();
            var processManager      = new FakeProcessManager();
            var noiseProcessManager = new FakeProcessManager();

            var random  = new Random();
            var fixture = new Fixture();

            var scheduledEnvelopes = new List <ScheduledEnvelope>(
                from command in new[]
            {
                new FakeCommand {
                    Int32Value = random.Next(), StringValue = fixture.Create <string>()
                },
                new FakeCommand {
                    Int32Value = random.Next(), StringValue = fixture.Create <string>()
                },
                new FakeCommand {
                    Int32Value = random.Next(), StringValue = fixture.Create <string>()
                },
            }
                let envelope = new Envelope(Guid.NewGuid(), command, null, Guid.NewGuid(), null)
                               select new ScheduledEnvelope(envelope, DateTime.UtcNow.AddTicks(random.Next())));

            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                db.PendingScheduledCommands.AddRange(
                    from scheduledEnvelope in scheduledEnvelopes
                    select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));

                db.PendingScheduledCommands.AddRange(
                    from scheduledEnvelope in new[]
                {
                    new ScheduledEnvelope(new Envelope(new object()), DateTime.UtcNow),
                    new ScheduledEnvelope(new Envelope(new object()), DateTime.UtcNow),
                    new ScheduledEnvelope(new Envelope(new object()), DateTime.UtcNow),
                }
                    select PendingScheduledCommand.FromScheduledEnvelope(noiseProcessManager, scheduledEnvelope, serializer));

                await db.SaveChangesAsync();
            }

            var scheduledMessageBus = new ScheduledMessageBus();

            var sut = new SqlCommandPublisher(
                () => new ProcessManagerDbContext(_dbContextOptions),
                serializer,
                Mock.Of <IMessageBus>(),
                scheduledMessageBus);

            // Act
            await sut.FlushCommands(processManager.Id, CancellationToken.None);

            // Assert
            scheduledMessageBus.Sent.Should().BeEquivalentTo(scheduledEnvelopes, opts => opts.WithStrictOrdering().RespectingRuntimeTypes());
        }
Beispiel #4
0
        public void FromScheduledEnvelope_sets_ScheduledTime_correctly()
        {
            ScheduledEnvelope scheduledEnvelope = new Fixture().Create <ScheduledEnvelope>();

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                new FooProcessManager(),
                scheduledEnvelope,
                new JsonMessageSerializer());

            actual.ScheduledTime.Should().Be(scheduledEnvelope.ScheduledTime);
        }
Beispiel #5
0
        public void FromScheduledEnvelope_sets_ProcessManagerId_correctly()
        {
            var processManager = new FooProcessManager();

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                processManager,
                new Fixture().Create <ScheduledEnvelope>(),
                new JsonMessageSerializer());

            actual.ProcessManagerId.Should().Be(processManager.Id);
        }
        public async Task FlushCommands_absorbs_exception_caused_by_that_some_pending_scheduled_command_already_deleted_since_loaded()
        {
            // Arrange
            var scheduledMessageBus = new CompletableScheduledMessageBus();
            var serializer          = new JsonMessageSerializer();
            var sut = new SqlCommandPublisher(
                () => new ProcessManagerDbContext(_dbContextOptions),
                serializer,
                Mock.Of <IMessageBus>(),
                scheduledMessageBus);

            var processManager = new FakeProcessManager();

            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                db.PendingScheduledCommands.AddRange(
                    from command in new[]
                {
                    new FakeCommand(),
                    new FakeCommand(),
                    new FakeCommand(),
                }
                    let envelope = new Envelope(command)
                                   let scheduledEnvelope = new ScheduledEnvelope(envelope, DateTime.UtcNow)
                                                           select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));
                await db.SaveChangesAsync();
            }

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

                    db.PendingScheduledCommands.RemoveRange(pendingScheduledCommands);
                    await db.SaveChangesAsync();
                }

                scheduledMessageBus.Complete();
                await flushTask;
            };

            // Assert
            action.Should().NotThrow();
        }
Beispiel #7
0
        public void FromScheduledEnvelope_sets_CorrelationId_correctly()
        {
            var correlationId     = Guid.NewGuid();
            var scheduledEnvelope = new ScheduledEnvelope(
                new Envelope(correlationId, new object()),
                DateTimeOffset.Now);

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                new FooProcessManager(),
                scheduledEnvelope,
                new JsonMessageSerializer());

            actual.CorrelationId.Should().Be(scheduledEnvelope.Envelope.CorrelationId);
        }
        public async Task given_scheduled_message_bus_fails_FlushCommands_deletes_no_scheduled_command()
        {
            // Arrange
            var serializer        = new JsonMessageSerializer();
            var processManager    = new FooProcessManager();
            var fixture           = new Fixture();
            var scheduledCommands = new List <PendingScheduledCommand>(
                from command in fixture.CreateMany <FooCommand>()
                let envelope = new Envelope(command)
                               let scheduledEnvelope = new ScheduledEnvelope(envelope, fixture.Create <DateTimeOffset>())
                                                       select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));

            using (var db = new FooProcessManagerDbContext())
            {
                db.PendingScheduledCommands.AddRange(scheduledCommands);
                await db.SaveChangesAsync();
            }

            Guid poisonedMessageId = (from c in scheduledCommands
                                      orderby c.GetHashCode()
                                      select c.MessageId).First();

            IScheduledMessageBus scheduledMessageBus = Mock.Of <IScheduledMessageBus>();
            var exception = new InvalidOperationException();

            Mock.Get(scheduledMessageBus)
            .Setup(x => x.Send(It.Is <ScheduledEnvelope>(p => p.Envelope.MessageId == poisonedMessageId), CancellationToken.None))
            .ThrowsAsync(exception);

            var sut = new SqlCommandPublisher(
                () => new FooProcessManagerDbContext(),
                serializer,
                Mock.Of <IMessageBus>(),
                scheduledMessageBus);

            // Act
            Func <Task> action = () => sut.FlushCommands(processManager.Id, CancellationToken.None);

            // Assert
            action.ShouldThrow <InvalidOperationException>().Which.Should().BeSameAs(exception);
            using (var db = new FooProcessManagerDbContext())
            {
                IQueryable <PendingScheduledCommand> query = from c in db.PendingScheduledCommands
                                                             where c.ProcessManagerId == processManager.Id
                                                             select c;
                List <PendingScheduledCommand> actual = await query.ToListAsync();

                actual.ShouldAllBeEquivalentTo(scheduledCommands, opts => opts.RespectingRuntimeTypes());
            }
        }
 private static async Task RemoveScheduledCommand(
     ProcessManagerDbContext dbContext,
     PendingScheduledCommand scheduledCommand,
     CancellationToken cancellationToken)
 {
     try
     {
         dbContext.PendingScheduledCommands.Remove(scheduledCommand);
         await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
     }
     catch (DbUpdateConcurrencyException)
     {
         dbContext.Entry(scheduledCommand).State = EntityState.Detached;
     }
 }
        public async Task FlushCommands_deletes_all_scheduled_commands_associated_with_specified_process_manager()
        {
            // Arrange
            var serializer = new JsonMessageSerializer();

            var processManager      = new FakeProcessManager();
            var noiseProcessManager = new FakeProcessManager();

            const int noiseCommandCount = 3;

            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                var commands = new List <PendingScheduledCommand>(
                    from command in Enumerable.Repeat(new FakeCommand(), 3)
                    let scheduledEnvelope = new ScheduledEnvelope(new Envelope(command), DateTime.UtcNow)
                                            select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));

                commands.AddRange(
                    from command in Enumerable.Repeat(new FakeCommand(), noiseCommandCount)
                    let scheduledEnvelope = new ScheduledEnvelope(new Envelope(command), DateTime.UtcNow)
                                            select PendingScheduledCommand.FromScheduledEnvelope(noiseProcessManager, scheduledEnvelope, serializer));

                var random = new Random();
                db.PendingScheduledCommands.AddRange(
                    from command in commands
                    orderby random.Next()
                    select command);

                await db.SaveChangesAsync();
            }

            var sut = new SqlCommandPublisher(
                () => new ProcessManagerDbContext(_dbContextOptions),
                serializer,
                Mock.Of <IMessageBus>(),
                Mock.Of <IScheduledMessageBus>());

            // Act
            await sut.FlushCommands(processManager.Id, CancellationToken.None);

            // Assert
            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                (await db.PendingScheduledCommands.AnyAsync(c => c.ProcessManagerId == processManager.Id)).Should().BeFalse();
                (await db.PendingScheduledCommands.CountAsync(c => c.ProcessManagerId == noiseProcessManager.Id)).Should().Be(noiseCommandCount);
            }
        }
Beispiel #11
0
        private void InsertPendingScheduledCommands(T processManager, string operationId, Guid?correlationId, string contributor)
        {
            IEnumerable <PendingScheduledCommand> pendingScheduledCommands =
                from scheduledCommand in processManager.FlushPendingScheduledCommands()
                let scheduledEnvelope =
                    new ScheduledEnvelope(
                        new Envelope(
                            Guid.NewGuid(),
                            scheduledCommand.Command,
                            operationId,
                            correlationId,
                            contributor),
                        scheduledCommand.ScheduledTimeUtc)
                    select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, _serializer);

            _dbContext.PendingScheduledCommands.AddRange(pendingScheduledCommands);
        }
Beispiel #12
0
        public void FromScheduledEnvelope_sets_CommandJson_correctly()
        {
            FooCommand command           = new Fixture().Create <FooCommand>();
            var        scheduledEnvelope = new ScheduledEnvelope(
                new Envelope(command),
                DateTimeOffset.Now);
            var serializer = new JsonMessageSerializer();

            var actual = PendingScheduledCommand.FromScheduledEnvelope(
                new FooProcessManager(),
                scheduledEnvelope,
                serializer);

            serializer.Deserialize(actual.CommandJson).ShouldBeEquivalentTo(
                command,
                opts => opts.RespectingRuntimeTypes());
        }
        public async Task FlushCommands_sends_all_scheduled_commands_associated_with_specified_process_manager_sequentially()
        {
            // Arrange
            var serializer          = new JsonMessageSerializer();
            var processManager      = new FooProcessManager();
            var noiseProcessManager = new FooProcessManager();

            var fixture = new Fixture();

            var scheduledEnvelopes = new List <ScheduledEnvelope>(
                from command in fixture.CreateMany <FooCommand>()
                let envelope = new Envelope(Guid.NewGuid(), command, null, Guid.NewGuid(), null)
                               select new ScheduledEnvelope(envelope, fixture.Create <DateTimeOffset>()));

            using (var db = new FooProcessManagerDbContext())
            {
                db.PendingScheduledCommands.AddRange(
                    from scheduledEnvelope in scheduledEnvelopes
                    select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));

                db.PendingScheduledCommands.AddRange(
                    from scheduledEnvelope in fixture.CreateMany <ScheduledEnvelope>()
                    select PendingScheduledCommand.FromScheduledEnvelope(noiseProcessManager, scheduledEnvelope, serializer));

                await db.SaveChangesAsync();
            }

            var scheduledMessageBus = new ScheduledMessageBus();

            var sut = new SqlCommandPublisher(
                () => new FooProcessManagerDbContext(),
                serializer,
                Mock.Of <IMessageBus>(),
                scheduledMessageBus);

            // Act
            await sut.FlushCommands(processManager.Id, CancellationToken.None);

            // Assert
            scheduledMessageBus.Sent.ShouldAllBeEquivalentTo(scheduledEnvelopes, opts => opts.WithStrictOrdering().RespectingRuntimeTypes());
        }
        public async Task EnqueueAll_publishes_all_pending_scheduled_commands_asynchronously()
        {
            // Arrange
            var serializer = new JsonMessageSerializer();

            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                for (int i = 0; i < 3; i++)
                {
                    var processManager = new FakeProcessManager();
                    db.PendingScheduledCommands.AddRange(from command in Enumerable.Repeat(new FakeCommand(), 3)
                                                         let envelope = new Envelope(command)
                                                                        let scheduledEnvelope = new ScheduledEnvelope(envelope, DateTime.UtcNow)
                                                                                                select PendingScheduledCommand.FromScheduledEnvelope(processManager, scheduledEnvelope, serializer));
                }

                await db.SaveChangesAsync();
            }

            var sut = new SqlCommandPublisher(
                () => new ProcessManagerDbContext(_dbContextOptions),
                serializer,
                Mock.Of <IMessageBus>(),
                Mock.Of <IScheduledMessageBus>());

            // Act
            sut.EnqueueAll(CancellationToken.None);

            // Assert
            using (var db = new ProcessManagerDbContext(_dbContextOptions))
            {
                int maximumRetryCount = 5;
                var retryPolicy       = new RetryPolicy <bool>(
                    maximumRetryCount,
                    new DelegatingTransientFaultDetectionStrategy <bool>(any => any == true),
                    new ConstantRetryIntervalStrategy(TimeSpan.FromSeconds(1.0)));
                (await retryPolicy.Run(db.PendingScheduledCommands.AnyAsync, CancellationToken.None)).Should().BeFalse();
            }
        }