public async Task Given_default_policy() { var aggregateId = Guid.NewGuid().ToString(); var initialParameter = 1; var cmd = new InflateNewBallonCommand(initialParameter, aggregateId); await Node.Prepare(cmd) .Expect <BalloonCreated>() .Execute(); var changedParameter = 2; var changeSampleAggregateCommand = new WriteTitleCommand(changedParameter, aggregateId); await Node.Prepare(changeSampleAggregateCommand) .Expect <BalloonTitleChanged>() .Execute(); Thread.Sleep(1000); var aggregates = await AggregateSnapshotRepository.New(AutoTestNodeDbConfiguration.Default.JournalConnectionString, BalloonAggregateFactory.Default) .Load <Balloon>(aggregateId); //Snapshots_should_be_saved_two_times() Assert.Equal(2, aggregates.Length); //Restored_aggregates_should_have_same_ids() Assert.True(aggregates.All(s => s.Payload.Id == aggregateId)); //First_snapshot_should_have_parameters_from_first_command() Assert.Equal(initialParameter.ToString(), aggregates.First().Payload.Title); //Second_snapshot_should_have_parameters_from_second_command() Assert.Equal(changedParameter.ToString(), aggregates.Skip(1).First().Payload.Title); }
public async Task Given_persisted_snapshot_Aggregate_should_execute_command() { var aggregate = new Balloon(Guid.NewGuid().ToString(), "haha"); aggregate.WriteNewTitle(10); aggregate.ClearUncommitedEvents(); var repo = new AggregateSnapshotRepository(AutoTestNodeDbConfiguration.Default.JournalConnectionString, new BalloonAggregateFactory(), new BalloonAggregateFactory()); await repo.Add(aggregate); var cmd = new IncreaseTitleCommand(1, aggregate.Id); var res = await Node.Prepare(cmd) .Expect <BalloonTitleChanged>() .Execute(); var message = res.Received; //Values_should_be_equal() Assert.Equal("11", message.Value); //Ids_should_be_equal() Assert.Equal(aggregate.Id, message.SourceId); }
public void Test() { _aggregate = new SampleAggregate(Guid.NewGuid(), "test"); _aggregate.ChangeState(10); _aggregate.ClearEvents(); var repo = new AggregateSnapshotRepository(AkkaConf.Persistence.JournalConnectionString, GridNode.AggregateFromSnapshotsFactory); repo.Add(_aggregate); _restoredAggregate = LoadAggregate <SampleAggregate>(_aggregate.Id); }
public async Task Given_save_on_each_message_policy_and_keep_2_snapshots() { var startEvent = new GotTiredEvent(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); var res = await Node.NewDebugWaiter() .Expect <ProcessManagerCreated <SoftwareProgrammingState> >() .Create() .SendToProcessManagers(startEvent); var processId = res.Message <ProcessManagerCreated <SoftwareProgrammingState> >() .SourceId; var continueEventA = new CoffeMakeFailedEvent(Guid.NewGuid().ToString(), startEvent.PersonId, BusinessDateTime.UtcNow, processId); await Node.SendToProcessManagers(continueEventA); await Node.KillProcessManager <SoftwareProgrammingProcess, SoftwareProgrammingState>(processId); Version <ProcessStateAggregate <SoftwareProgrammingState> >[] snapshots = null; //Only_two_Snapshots_should_left() AwaitAssert(() => { snapshots = AggregateSnapshotRepository.New(AutoTestNodeDbConfiguration.Default.JournalConnectionString) .Load <ProcessStateAggregate <SoftwareProgrammingState> >(processId) .Result; Assert.Equal(2, snapshots.Length); // Restored_aggregates_should_have_same_ids() Assert.True(snapshots.All(s => s.Payload.Id == processId)); // First_Snapshots_should_have_coding_state_from_first_event() Assert.Equal(nameof(SoftwareProgrammingProcess.MakingCoffee), snapshots.First() .Payload.State.CurrentStateName); //Last_Snapshots_should_have_coding_state_from_last_event() Assert.Equal(nameof(SoftwareProgrammingProcess.Sleeping), snapshots.Last() .Payload.State.CurrentStateName); //All_snapshots_should_not_have_uncommited_events() Assert.Empty(snapshots.SelectMany(s => s.Payload.GetEvents())); }, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(1)); }
public void Test() { _sagaState = new SoftwareProgrammingSagaState(Guid.NewGuid(), SoftwareProgrammingSaga.States.MakingCoffee); _sagaState.RememberPerson(Guid.NewGuid()); _sagaState.RememberBadCoffeMachine(Guid.NewGuid()); _sagaState.ClearEvents(); var repo = new AggregateSnapshotRepository(AkkaConf.Persistence.JournalConnectionString, GridNode.AggregateFromSnapshotsFactory); repo.Add(_sagaState); _restoredState = LoadSagaState <SoftwareProgrammingSaga, SoftwareProgrammingSagaState>(_sagaState.Id); }
public void Test() { var saga = new SoftwareProgrammingSaga(); var state = new SoftwareProgrammingSagaData(saga.Coding.Name, Guid.NewGuid(), Guid.NewGuid()); _sagaState = new SagaDataAggregate <SoftwareProgrammingSagaData>(Guid.NewGuid(), state); _sagaState.RememberEvent(saga.CoffeReady, state, new object()); _sagaState.ClearEvents(); var repo = new AggregateSnapshotRepository(AkkaConf.Persistence.JournalConnectionString, GridNode.AggregateFromSnapshotsFactory); repo.Add(_sagaState); _restoredState = LoadInstanceSagaState <SoftwareProgrammingSaga, SoftwareProgrammingSagaData>(_sagaState.Id); }
public async Task Test() { var i = Node.Id; //just start node var process = new SoftwareProgrammingProcess(); var state = new SoftwareProgrammingState(Guid.NewGuid().ToString(), process.Coding.Name, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); var processStateAggregate = new ProcessStateAggregate <SoftwareProgrammingState>(state); processStateAggregate.ReceiveMessage(state, null); processStateAggregate.ClearUncommitedEvents(); var repo = new AggregateSnapshotRepository(AutoTestNodeDbConfiguration.Default.JournalConnectionString, AggregateFactory.Default, AggregateFactory.Default); await repo.Add(processStateAggregate); var restoredState = await this.LoadProcessByActor <SoftwareProgrammingState>(processStateAggregate.Id); //CoffeMachineId_should_be_equal() Assert.Equal(processStateAggregate.State.CoffeeMachineId, restoredState.CoffeeMachineId); // State_should_be_equal() Assert.Equal(processStateAggregate.State.CurrentStateName, restoredState.CurrentStateName); }
public async Task Given_default_policy() { var startEvent = new GotTiredEvent(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); var resTask = Node.NewDebugWaiter() .Expect <ProcessReceivedMessage <SoftwareProgrammingState> >() .Create() .SendToProcessManagers(startEvent, MessageMetadata.New(startEvent.Id, null, null)); var processId = (await resTask).Message <ProcessReceivedMessage <SoftwareProgrammingState> >().SourceId; var continueEvent = new CoffeMakeFailedEvent(processId, startEvent.PersonId, BusinessDateTime.UtcNow, processId); //to avoid racy state receiving expected message from processing GotTiredEvent await Node.NewDebugWaiter() .Expect <ProcessReceivedMessage <SoftwareProgrammingState> >(e => e.MessageId == continueEvent.Id) .Create() .SendToProcessManagers(continueEvent, processId); Log.Info("Testcase enforce additional snapshot save & delete, will kill process manager"); await Node.KillProcessManager <SoftwareProgrammingProcess, SoftwareProgrammingState>(processId); var snapshots = await AggregateSnapshotRepository.New(AutoTestNodeDbConfiguration.Default.JournalConnectionString, AggregateFactory.Default) .Load <ProcessStateAggregate <SoftwareProgrammingState> >(processId); //Snapshot_should_be_saved_one_time Assert.Single(snapshots); //Restored_process_state_should_have_correct_ids Assert.True(snapshots.All(s => s.Payload.Id == processId)); //Snapshot_should_have_parameters_from_first_event = created event Assert.Equal(nameof(SoftwareProgrammingProcess.Coding), snapshots.First().Payload.State.CurrentStateName); //All_snapshots_should_not_have_uncommited_events Assert.Empty(snapshots.SelectMany(s => s.Payload.GetEvents())); }
public async Task Given_default_policy() { var startEvent = new GotTiredEvent(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); var res = await Node.NewDebugWaiter() .Expect <ProcessManagerCreated <SoftwareProgrammingState> >() .Create() .SendToProcessManagers(startEvent); var processId = res.Message <ProcessManagerCreated <SoftwareProgrammingState> >() .SourceId; var continueEvent = new CoffeMakeFailedEvent(processId, startEvent.PersonId, BusinessDateTime.UtcNow, processId); await Node.NewDebugWaiter() .Expect <ProcessReceivedMessage <SoftwareProgrammingState> >() .Create() .SendToProcessManagers(continueEvent); var continueEventB = new Fault <GoSleepCommand>(new GoSleepCommand(startEvent.PersonId, startEvent.LovelySofaId), new Exception(), typeof(object), processId, BusinessDateTime.Now); await Node.NewDebugWaiter() .Expect <ProcessReceivedMessage <SoftwareProgrammingState> >() .Create() .SendToProcessManagers(continueEventB); await Node.KillProcessManager <SoftwareProgrammingProcess, SoftwareProgrammingState>(continueEvent.ProcessId); Version <ProcessStateAggregate <SoftwareProgrammingState> >[] snapshots = null; AwaitAssert(() => { snapshots = new AggregateSnapshotRepository(AutoTestNodeDbConfiguration.Default.JournalConnectionString, AggregateFactory.Default, AggregateFactory.Default) .Load <ProcessStateAggregate <SoftwareProgrammingState> >(processId) .Result; //saving on each message, maximum on each command //Snapshots_should_be_saved_two_times //4 events in total, two saves of snapshots due to policy saves on each two events //1 event and 3 Assert.Equal(2, snapshots.Length); //First_snapshot_should_have_state_from_first_event Assert.Equal(nameof(SoftwareProgrammingProcess.Coding), snapshots.First() .Payload.State.CurrentStateName); //Last_snapshot_should_have_parameters_from_last_command() Assert.Equal(nameof(SoftwareProgrammingProcess.Sleeping), snapshots.Last() .Payload.State.CurrentStateName); //Restored_process_state_should_have_correct_ids Assert.True(snapshots.All(s => s.Payload.Id == processId)); //All_snapshots_should_not_have_uncommited_events() Assert.Empty(snapshots.SelectMany(s => s.Payload.GetEvents())); }, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(1)); }