Пример #1
0
        public override void Handle(CommandScheduled @event)
        {
            // An entry is considered committed once it has been written to persistant storage and replicated.
            _nodePublisher.PublishEvent(new NodeCommandScheduled
            {
                Command = new CommitEntry
                {
                    EntryIdx  = @event.LogEntry.Index,
                    EntryTerm = @event.LogEntry.Term
                }
            }).Wait();

            @event.Command.Execute(_serviceLocator);
            _nodePublisher.PublishEvent(new NodeCommandScheduled
            {
                Command = new ApplyEntry
                {
                    EntryIdx = @event.LogEntry.Index
                }
            }).Wait();

            if (@event.CompletionSource != null)
            {
                @event.CompletionSource.SetResult(new CommandExecutionResult(true));
            }
        }
Пример #2
0
        public async Task When_a_non_scheduler_assigned_negative_SequenceNumber_collides_then_it_is_ignored()
        {
            // arrange
            var commandScheduler = Configuration.Current.CommandScheduler <Order>();

            var initialSequenceNumber = Any.PositiveInt();

            var orderId = Any.Guid();

            await commandScheduler.Schedule(new CommandScheduled <Order>
            {
                AggregateId    = orderId,
                Command        = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime        = Clock.Now().AddYears(1)
            });

            var secondCommand = new CommandScheduled <Order>
            {
                AggregateId    = orderId,
                Command        = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime        = Clock.Now().AddYears(1)
            };

            // act
            Action again = () => commandScheduler.Schedule(secondCommand).Wait();

            // assert
            again.ShouldNotThrow <DbUpdateException>();
        }
Пример #3
0
        public async Task When_a_scheduler_assigned_negative_SequenceNumber_collides_then_it_retries_scheduling_with_a_new_SequenceNumber()
        {
            // arrange
            var commandScheduler = Configuration.Current.CommandScheduler <Order>();

            var initialSequenceNumber = -Any.PositiveInt();

            var orderId = Any.Guid();

            await commandScheduler.Schedule(new CommandScheduled <Order>
            {
                AggregateId    = orderId,
                Command        = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime        = Clock.Now().AddYears(1)
            });

            var secondCommand = new CommandScheduled <Order>
            {
                AggregateId    = orderId,
                Command        = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime        = Clock.Now().AddYears(1)
            };

            await commandScheduler.Schedule(secondCommand);

            using (var db = CommandSchedulerDbContext())
            {
                db.ScheduledCommands
                .Count(c => c.AggregateId == orderId)
                .Should()
                .Be(2);
            }
        }
Пример #4
0
        public override void Handle(CommandScheduled @event)
        {
            if (@event.LogEntry == null || @event.EncodedEntry == null)
            {
                throw new InvalidOperationException("Must set EncodedEntry on event before executing this step.");
            }

            _writeDataBlocks.WriteBlock(@event.EncodedEntry);
        }
 public void Handle(CommandScheduled e)
 {
     AggRoot          = e.CommandToExecute;
     CommandSchedules = new List <ScheduleInstruction> {
         e.ScheduleInstructions
     };
     Clients = new List <int> {
         e.ClientId
     };
 }
Пример #6
0
        public async Task A_scheduled_command_is_not_due_if_it_has_already_been_delivered_and_succeeded()
        {
            var command = new CommandScheduled <Order>
            {
                Command = new AddItem()
            };

            command.Result = new CommandSucceeded(command);

            command.IsDue().Should().BeFalse();
        }
Пример #7
0
        public async Task A_scheduled_command_is_due_if_no_due_time_is_specified()
        {
            var command = new CommandScheduled <Order>
            {
                Command = new AddItem()
            };

            command.IsDue()
            .Should()
            .BeTrue();
        }
Пример #8
0
        public async Task A_scheduled_command_is_not_due_if_a_due_time_is_specified_that_is_later_than_the_current_domain_clock()
        {
            var command = new CommandScheduled <Order>
            {
                Command = new AddItem(),
                DueTime = Clock.Now().Add(TimeSpan.FromSeconds(1))
            };

            command.IsDue()
            .Should()
            .BeFalse();
        }
Пример #9
0
        public async Task A_scheduled_command_is_due_if_a_due_time_is_specified_that_is_earlier_than_the_specified_clock()
        {
            var command = new CommandScheduled <Order>
            {
                Command = new AddItem(),
                DueTime = Clock.Now().Add(TimeSpan.FromDays(1))
            };

            command.IsDue(Clock.Create(() => Clock.Now().Add(TimeSpan.FromDays(2))))
            .Should()
            .BeTrue();
        }
Пример #10
0
        public static CommandScheduled GetCommandEvent(long logIdx, byte[] data, Action executeAction)
        {
            var @event = new CommandScheduled
            {
                Command = new TestExecutableCommand(executeAction)
            }.Translate(new CommandScheduled(), 1L);

            @event.SetLogEntry(new LogEntry {
                Index = logIdx
            }, data);

            return(@event);
        }
Пример #11
0
        public override void Handle(CommandScheduled @event)
        {
            var request = new AppendEntriesRequest {
                Entries = new[] {
                    @event.EncodedEntry
                }
            };

            foreach (var peerNode in _peers)
            {
                GetChannel(peerNode).AppendEntries(request);
            }
        }
Пример #12
0
        // TODO: Should add checksum for validation when sourcing from log... http://stackoverflow.com/questions/10335203/is-there-any-very-rapid-checksum-generation-algorithm
        public override void Handle(CommandScheduled @event)
        {
            var logEntry = new LogEntry {
                Term        = _node.Data.CurrentTerm,
                Index       = _lastLogId + 1,
                CommandType = @event.Command.GetType().AssemblyQualifiedName,
                Command     = @event.Command
            };

            using (var ms = new MemoryStream())
            {
                Serializer.SerializeWithLengthPrefix(ms, logEntry, PrefixStyle.Base128);
                @event.SetLogEntry(logEntry, ms.ToArray());
            }

            _lastLogId++;
        }
        public void Event_ETag_cannot_be_the_same_as_the_command_etag()
        {
            var etag1 = Any.Word().ToETag();

            CommandScheduled <Order> scheduled;

            using (CommandContext.Establish(new TestCommand(etag1)))
            {
                scheduled = new CommandScheduled <Order>
                {
                    AggregateId = Any.Guid(),
                    Command     = new AddItem()
                };
            }

            scheduled.Command.ETag.Should().NotBeEmpty();
            scheduled.ETag.Should().NotBeEmpty();
            scheduled.ETag.Should().NotBe(scheduled.Command.ETag);
        }
        public async Task When_a_scheduler_assigned_negative_SequenceNumber_collides_then_it_retries_scheduling_with_a_new_SequenceNumber()
        {
            // arrange
            var container = Configuration.Current.Container;
            var commandScheduler = container.Resolve<ICommandScheduler<Order>>();

            var initialSequenceNumber = -Any.PositiveInt();

            var orderId = Any.Guid();

            await commandScheduler.Schedule(new CommandScheduled<Order>
            {
                AggregateId = orderId,
                Command = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime = Clock.Now().AddYears(1)
            });

            var secondCommand = new CommandScheduled<Order>
            {
                AggregateId = orderId,
                Command = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime = Clock.Now().AddYears(1)
            };

            await commandScheduler.Schedule(secondCommand);

            using (var db = new CommandSchedulerDbContext())
            {
                db.ScheduledCommands
                  .Count(c => c.AggregateId == orderId)
                  .Should()
                  .Be(2);
            }
        }
        public async Task When_a_non_scheduler_assigned_negative_SequenceNumber_collides_then_it_is_ignores()
        {
            // arrange
            var container = Configuration.Current.Container;
            var commandScheduler = container.Resolve<ICommandScheduler<Order>>();

            var db = new CommandSchedulerDbContext();

            disposables.Add(db);

            var initialSequenceNumber = Any.PositiveInt();

            var orderId = Any.Guid();

            await commandScheduler.Schedule(new CommandScheduled<Order>
            {
                AggregateId = orderId,
                Command = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime = Clock.Now().AddYears(1)
            });

            var secondCommand = new CommandScheduled<Order>
            {
                AggregateId = orderId,
                Command = new Cancel(),
                SequenceNumber = initialSequenceNumber,
                DueTime = Clock.Now().AddYears(1)
            };

            Action again = () => commandScheduler.Schedule(secondCommand).Wait();

            again.ShouldNotThrow<DbUpdateException>();
        }
        public async Task A_scheduled_command_is_not_due_if_it_has_already_been_delivered_and_succeeded()
        {
            var command = new CommandScheduled<Order>
            {
                Command = new AddItem()
            };
            command.Result = new CommandSucceeded(command);

            command.IsDue().Should().BeFalse();
        }
        public async Task A_scheduled_command_is_not_due_if_a_due_time_is_specified_that_is_later_than_the_current_domain_clock()
        {
            var command = new CommandScheduled<Order>
            {
                Command = new AddItem(),
                DueTime = Clock.Now().Add(TimeSpan.FromSeconds(1))
            };

            command.IsDue()
                   .Should()
                   .BeFalse();
        }
        public async Task A_scheduled_command_is_due_if_a_due_time_is_specified_that_is_earlier_than_the_specified_clock()
        {
            var command = new CommandScheduled<Order>
            {
                Command = new AddItem(),
                DueTime = Clock.Now().Add(TimeSpan.FromDays(1))
            };

            command.IsDue(Clock.Create(() => Clock.Now().Add(TimeSpan.FromDays(2))))
                   .Should()
                   .BeTrue();
        }
        public async Task A_scheduled_command_is_due_if_no_due_time_is_specified()
        {
            var command = new CommandScheduled<Order>
            {
                Command = new AddItem()
            };

            command.IsDue()
                   .Should()
                   .BeTrue();
        }