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)); } }
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>(); }
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); } }
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 }; }
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_due_if_no_due_time_is_specified() { var command = new CommandScheduled <Order> { Command = new AddItem() }; command.IsDue() .Should() .BeTrue(); }
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 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); }
public override void Handle(CommandScheduled @event) { var request = new AppendEntriesRequest { Entries = new[] { @event.EncodedEntry } }; foreach (var peerNode in _peers) { GetChannel(peerNode).AppendEntries(request); } }
// 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(); }