public void EntityList_CanAdd_Entity_WithOut_Throwing() { // Arrange var entity = new FirstSubProcess("test", 1, DateTime.Now); //Act Action addAction = () => _entityList.Add(entity); //Assert addAction.Should().NotThrow(); }
public static Action Should_Throw_Due_To_Bad_StreamId(ParamScenarioTest paramScenarioTest, Func <InputAggregate> aggregateFactory) { var aggregate = aggregateFactory(); var processElemCreation = new FirstSubProcess(paramScenarioTest.ProcessName, paramScenarioTest.ExpectedProcessId, paramScenarioTest.ExpectedDateCreated); aggregate.RaiseEvent(new InputAggregateCreated(paramScenarioTest.ExpectedStreamId + 1)); Action shouldThrow = () => aggregate.RaiseEvent(new ProcessElementEntityCreated(paramScenarioTest.ExpectedStreamId, processElemCreation)); return(shouldThrow); }
public static InputAggregate Start_Process(ParamScenarioTest paramScenarioTest, Func <InputAggregate> aggregateFactory) { var aggregate = aggregateFactory(); var processElemCreation = new FirstSubProcess(paramScenarioTest.ProcessName, paramScenarioTest.ExpectedProcessId, paramScenarioTest.ExpectedDateCreated); aggregate.RaiseEvent(new InputAggregateCreated(paramScenarioTest.ExpectedStreamId)); aggregate.RaiseEvent(new ProcessElementEntityCreated(paramScenarioTest.ExpectedStreamId, processElemCreation)); aggregate.RaiseEvent(new ProcessElemStarted(paramScenarioTest.ExpectedStreamId, paramScenarioTest.ExpectedProcessId, paramScenarioTest.ExpectedRunningService, paramScenarioTest.ExpectedDateStarted)); return(aggregate); }
public async Task Repo_Can_Retrieve_Agregate_Using_GetByIdAsync() { // Arrange var param = GetArg(1); var aggregate = _repo.GetNewAggregate(); var processElemCreation = new FirstSubProcess(param.ProcessName, param.ExpectedProcessId, param.ExpectedDateCreated); aggregate.RaiseEvent(new InputAggregateCreated(param.ExpectedStreamId)); aggregate.RaiseEvent(new ProcessElementEntityCreated(param.ExpectedStreamId, processElemCreation)); aggregate.RaiseEvent(new ProcessElemStarted(param.ExpectedStreamId, param.ExpectedProcessId, param.ExpectedRunningService, param.ExpectedDateStarted)); aggregate.RaiseEvent(new ProcessElemStoped(param.ExpectedStreamId, param.ExpectedProcessId, param.ExpectedDateStoped)); _repo.Save(aggregate); // Act var aggregate2 = await _repo.GetByIdAsync(param.ExpectedStreamId); // Assert aggregate2.GetProcessElementById(param.ExpectedProcessId).ShouldBeAsExpected(param); }
public void Repo_Allow_To_Add_Event_On_Reloaded_Aggregate() { // Arrange var param = GetArg(1); var aggregate = _repo.GetNewAggregate(); var processElemCreation = new FirstSubProcess(param.ProcessName, param.ExpectedProcessId, param.ExpectedDateCreated); // Act aggregate.RaiseEvent(new InputAggregateCreated(param.ExpectedStreamId)); _repo.Save(aggregate); var aggregate2 = _repo.GetById(param.ExpectedStreamId); aggregate2.RaiseEvent(new ProcessElementEntityCreated(param.ExpectedStreamId, processElemCreation)); aggregate2.RaiseEvent(new ProcessElemStarted(param.ExpectedStreamId, param.ExpectedProcessId, param.ExpectedRunningService, param.ExpectedDateStarted)); aggregate2.RaiseEvent(new ProcessElemStoped(param.ExpectedStreamId, param.ExpectedProcessId, param.ExpectedDateStoped)); _repo.Save(aggregate2); // Assert ((IEventSourced <int>)aggregate2).UncommittedEvents.Count().Should().Be(0); _store.ReadEvents(1).Count().Should().Be(4); aggregate2.GetProcessElementById(param.ExpectedProcessId).ShouldBeAsExpected(param); }
public ProcessElementEntityCreated(int streamId, FirstSubProcess process) : base(streamId) => Process = process;
public void Repo_Should_Failed_To_Updated_Aggregate_When_Used_NonSequentialy_WithOut_Protection_By_Two_Thread() { /// Arrange var param1 = GetArg(1); var param2 = GetArg(2); var aggregate = _repo.GetNewAggregate(); var processElemCreation1 = new FirstSubProcess(param1.ProcessName, param1.ExpectedProcessId, param1.ExpectedDateCreated); var processElemCreation2 = new FirstSubProcess(param2.ProcessName, param2.ExpectedProcessId, param2.ExpectedDateCreated); var pingPong = new PingPong(); var mre3 = new ManualResetEvent(false); void DoNothing() { return; } Action finalAction = DoNothing; void Thread1Work() { Thread.CurrentThread.Name = "Thread1Work"; var _aggregate1 = _repo.GetById(param1.ExpectedStreamId); pingPong.WaitPing(); _aggregate1.RaiseEvent(new ProcessElementEntityCreated(param1.ExpectedStreamId, processElemCreation1)); _aggregate1.RaiseEvent(new ProcessElemStarted(param1.ExpectedStreamId, param1.ExpectedProcessId, param1.ExpectedRunningService, param1.ExpectedDateStarted)); _repo.Save(_aggregate1); pingPong.Ping(); _aggregate1.RaiseEvent(new ProcessElemStoped(param1.ExpectedStreamId, param1.ExpectedProcessId, param1.ExpectedDateStoped)); _repo.Save(_aggregate1); pingPong.SetOnlyPong(); } void Thread2Work() { Thread.CurrentThread.Name = "Thread2Work"; var _aggregate2 = _repo.GetById(param1.ExpectedStreamId); pingPong.WaitPong(); _aggregate2.RaiseEvent(new ProcessElementEntityCreated(param2.ExpectedStreamId, processElemCreation2)); _aggregate2.RaiseEvent(new ProcessElemStarted(param2.ExpectedStreamId, param2.ExpectedProcessId, param2.ExpectedRunningService, param2.ExpectedDateStarted)); pingPong.Pong(); _aggregate2.RaiseEvent(new ProcessElemStoped(param2.ExpectedStreamId, param2.ExpectedProcessId, param2.ExpectedDateStoped)); _repo.Save(_aggregate2); } void finalTask(Task t) { if (t.IsFaulted) { var ex = t.Exception; finalAction = () => throw ex; } mre3.Set(); } // Act aggregate.RaiseEvent(new InputAggregateCreated(param1.ExpectedStreamId)); _repo.Save(aggregate); var t1 = Task.Factory.StartNew(Thread1Work); var t2 = Task.Factory.StartNew(Thread2Work); var tasks = new[] { t1, t2 }; Task.WhenAll(tasks).ContinueWith(finalTask); mre3.WaitOne(); /// Assert var _aggregate = _repo.GetById(param1.ExpectedStreamId); _aggregate.GetProcessElementById(param1.ExpectedProcessId).ShouldBeAsExpected(param1); finalAction.Should().Throw <AggregateException>(); }
public void Repo_Should_Rollback_Save_If_Not_on_The_Rigth_Version() { /// Arrange var param1 = GetArg(1); var param2 = GetArg(2); var aggregate = _repo.GetNewAggregate(); var processElemCreation1 = new FirstSubProcess(param1.ProcessName, param1.ExpectedProcessId, param1.ExpectedDateCreated); var processElemCreation2 = new FirstSubProcess(param2.ProcessName, param2.ExpectedProcessId, param2.ExpectedDateCreated); var pingPong = new PingPong(); var mre3 = new ManualResetEvent(false); var firstThreadFirstTaskHaveBeenRollbacked = false; var firstThreadSecondTaskHaveBeenRollbacked = false; var secondThreadFirstTaskHaveBeenRollbacked = false; void DoNothing() { return; } Action finalAction = DoNothing; void Thread1Work() { Thread.CurrentThread.Name = "Thread1Work"; var _aggregate1 = _repo.GetById(param1.ExpectedStreamId); using (var uow1 = new BasicUnitOfWork <InputAggregate, int>()) { uow1.OnRollback += () => firstThreadFirstTaskHaveBeenRollbacked = true; pingPong.WaitPing(); _aggregate1.RaiseEvent(new ProcessElementEntityCreated(param1.ExpectedStreamId, processElemCreation1)); _aggregate1.RaiseEvent(new ProcessElemStarted(param1.ExpectedStreamId, param1.ExpectedProcessId, param1.ExpectedRunningService, param1.ExpectedDateStarted)); _aggregate1 = _saver.Save(_aggregate1, uow1); } pingPong.Ping(); using (var uow2 = new BasicUnitOfWork <InputAggregate, int>()) { uow2.OnRollback += () => firstThreadSecondTaskHaveBeenRollbacked = true; _aggregate1.RaiseEvent(new ProcessElemStoped(param1.ExpectedStreamId, param1.ExpectedProcessId, param1.ExpectedDateStoped)); _aggregate1 = _saver.Save(_aggregate1, uow2); } pingPong.SetOnlyPong(); } void Thread2Work() { Thread.CurrentThread.Name = "Thread2Work"; var _aggregate2 = _repo.GetById(param1.ExpectedStreamId); using (var uow1 = new BasicUnitOfWork <InputAggregate, int>()) { uow1.OnRollback += () => secondThreadFirstTaskHaveBeenRollbacked = true; pingPong.WaitPong(); _aggregate2.RaiseEvent(new ProcessElementEntityCreated(param2.ExpectedStreamId, processElemCreation2)); _aggregate2.RaiseEvent(new ProcessElemStarted(param2.ExpectedStreamId, param2.ExpectedProcessId, param2.ExpectedRunningService, param2.ExpectedDateStarted)); pingPong.Pong(); _aggregate2.RaiseEvent(new ProcessElemStoped(param2.ExpectedStreamId, param2.ExpectedProcessId, param2.ExpectedDateStoped)); _aggregate2 = _saver.Save(_aggregate2, uow1); } } void finalTask(Task t) { if (t.IsFaulted) { var ex = t.Exception; finalAction = () => throw ex; } mre3.Set(); } // Act aggregate.RaiseEvent(new InputAggregateCreated(param1.ExpectedStreamId)); _repo.Save(aggregate); var t1 = Task.Factory.StartNew(Thread1Work); var t2 = Task.Factory.StartNew(Thread2Work); var tasks = new[] { t1, t2 }; Task.WhenAll(tasks).ContinueWith(finalTask); mre3.WaitOne(); /// Assert var _aggregate = _repo.GetById(param1.ExpectedStreamId); _aggregate.GetProcessElementById(param1.ExpectedProcessId).ShouldBeAsExpected(param1); _aggregate.GetProcessElementById(param2.ExpectedProcessId).Should().BeNull(); finalAction.Should().NotThrow(); firstThreadFirstTaskHaveBeenRollbacked.Should().BeFalse(); firstThreadSecondTaskHaveBeenRollbacked.Should().BeFalse(); secondThreadFirstTaskHaveBeenRollbacked.Should().BeTrue(); }