Ejemplo n.º 1
0
        public void SendingTest_FromTestAggregate_CompletesSaga()
        {
            var eventProbe = CreateTestProbe("event-probe");

            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaCompletedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaTransactionCompletedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaTimeoutOccurred>));

            var aggregateManager = Sys.ActorOf(Props.Create(() => new TestAggregateManager()), "test-aggregatemanager");

            Sys.ActorOf(Props.Create(() => new TestSagaManager(() => new TestSaga(aggregateManager))), "test-sagaaggregatemanager");

            var senderAggregateId            = TestAggregateId.New;
            var senderCreateAggregateCommand = new CreateTestCommand(senderAggregateId, CommandId.New);

            aggregateManager.Tell(senderCreateAggregateCommand);

            var receiverAggregateId            = TestAggregateId.New;
            var receiverCreateAggregateCommand = new CreateTestCommand(receiverAggregateId, CommandId.New);

            aggregateManager.Tell(receiverCreateAggregateCommand);

            var senderTestId         = TestId.New;
            var senderTest           = new Test(senderTestId);
            var nextAggregateCommand = new AddTestCommand(senderAggregateId, CommandId.New, senderTest);

            aggregateManager.Tell(nextAggregateCommand);

            var sagaStartingCommand = new GiveTestCommand(senderAggregateId, CommandId.New, receiverAggregateId, senderTest);

            aggregateManager.Tell(sagaStartingCommand);

            eventProbe.
            ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent> >(
                x => x.AggregateEvent.Sender.Equals(senderAggregateId) &&
                x.AggregateEvent.Receiver.Equals(receiverAggregateId) &&
                x.AggregateEvent.SentTest.Equals(senderTest), new TimeSpan(0, 0, 20));

            eventProbe.
            ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaTransactionCompletedEvent> >(new TimeSpan(0, 0, 20));

            eventProbe.
            ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaCompletedEvent> >(new TimeSpan(0, 0, 20));

            eventProbe.ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaTimeoutOccurred> >(
                timeoutMsg => timeoutMsg.AggregateEvent.TimeoutMessage.Equals("First timeout test"),
                TimeSpan.FromSeconds(15));

            eventProbe.ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaTimeoutOccurred> >(
                timeoutMsg => timeoutMsg.AggregateEvent.TimeoutMessage.StartsWith("Second timeout test"),
                TimeSpan.FromSeconds(15));
        }
Ejemplo n.º 2
0
        private bool Execute(GiveTestCommand command)
        {
            if (!IsNew)
            {
                if (State.TestCollection.Any(x => x.Id == command.TestToGive.Id))
                {
                    Emit(new TestSentEvent(command.TestToGive, command.ReceiverAggregateId));
                }
            }
            else
            {
                TestErrors++;
                Throw(new TestedErrorEvent(TestErrors));
            }

            return(true);
        }
        public void SendingTest_FromTestAggregate_CompletesSagaAsync()
        {
            var eventProbe = CreateTestProbe("event-probe");

            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaStartedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaCompletedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaTransactionCompletedEvent>));
            var aggregateManager = Sys.ActorOf(Props.Create(() => new TestAggregateManager()), "test-aggregatemanager");

            Sys.ActorOf(Props.Create(() => new TestAsyncSagaManager(() => new TestAsyncSaga(aggregateManager))), "test-sagaaggregatemanager");

            var senderAggregateId            = TestAggregateId.New;
            var senderCreateAggregateCommand = new CreateTestCommand(senderAggregateId, CommandId.New);

            aggregateManager.Tell(senderCreateAggregateCommand);

            var receiverAggregateId            = TestAggregateId.New;
            var receiverCreateAggregateCommand = new CreateTestCommand(receiverAggregateId, CommandId.New);

            aggregateManager.Tell(receiverCreateAggregateCommand);

            var senderTestId         = TestId.New;
            var senderTest           = new Test(senderTestId);
            var nextAggregateCommand = new AddTestCommand(senderAggregateId, CommandId.New, senderTest);

            aggregateManager.Tell(nextAggregateCommand);

            var sagaStartingCommand = new GiveTestCommand(senderAggregateId, CommandId.New, receiverAggregateId, senderTest);

            aggregateManager.Tell(sagaStartingCommand);



            eventProbe.
            ExpectMsg <DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaStartedEvent> >(
                x => x.AggregateEvent.Sender.Equals(senderAggregateId) &&
                x.AggregateEvent.Receiver.Equals(receiverAggregateId) &&
                x.AggregateEvent.SentTest.Equals(senderTest) &&
                x.Metadata.ContainsKey("some-key"));

            eventProbe.
            ExpectMsg <DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaTransactionCompletedEvent> >();

            eventProbe.
            ExpectMsg <DomainEvent <TestAsyncSaga, TestAsyncSagaId, TestAsyncSagaCompletedEvent> >();
        }
Ejemplo n.º 4
0
        public async Task <Result> Do(GiveTestCommand command)
        {
            if (!IsNew)
            {
                if (State.TestCollection.Any(x => x.Id == command.TestToGive.Id))
                {
                    await Emit(new TestSentEvent(Id, command.ReceiverAggregateId, command.TestToGive));

                    return(Result.Success);
                }
            }

            TestErrors++;
            await Emit(new TestedErrorEvent(Id, TestErrors));

            return(Result.Fail("TestedErrorEvent"));
        }
Ejemplo n.º 5
0
        private bool Execute(GiveTestCommand command)
        {
            if (!IsNew)
            {
                if (State.TestCollection.Any(x => x.Id == command.TestToGive.Id))
                {
                    Emit(new TestSentEvent(command.TestToGive, command.ReceiverAggregateId));
                    Reply(TestExecutionResult.SucceededWith(command.SourceId));
                }
            }
            else
            {
                TestErrors++;
                Throw(new TestedErrorEvent(TestErrors));
                ReplyFailure(TestExecutionResult.FailedWith(command.SourceId));
            }

            return(true);
        }
Ejemplo n.º 6
0
        public Task <IExecutionResult> Execute(GiveTestCommand command)
        {
            if (!IsNew)
            {
                if (State.TestCollection.Any(x => x.Id == command.TestToGive.Id))
                {
                    Emit(new TestSentEvent(command.TestToGive, command.ReceiverAggregateId));
                    Reply(TestExecutionResult.SucceededWith(command.Metadata.SourceId));
                }
            }
            else
            {
                TestErrors++;
                Throw(new TestedErrorEvent(TestErrors));
                ReplyFailure(TestExecutionResult.FailedWith(command.Metadata.SourceId));
            }

            return(Task.FromResult(ExecutionResult.Success()));
        }
Ejemplo n.º 7
0
        public void SendingTest_FromTestAggregate_CompletesSaga()
        {
            var eventProbe = CreateTestProbe("event-probe");

            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSagaId, TestSagaStartedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSagaId, TestSagaCompletedEvent>));
            Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSagaId, TestSagaTransactionCompletedEvent>));
            var bus = Sys.GetExtension <ServiceProviderHolder>().ServiceProvider.GetRequiredService <ICommandBus>();


            var senderAggregateId            = TestAggregateId.New;
            var senderCreateAggregateCommand = new CreateTestCommand(senderAggregateId, CommandId.New);

            bus.Publish(senderCreateAggregateCommand).GetAwaiter().GetResult();

            var receiverAggregateId            = TestAggregateId.New;
            var receiverCreateAggregateCommand = new CreateTestCommand(receiverAggregateId, CommandId.New);

            bus.Publish(receiverCreateAggregateCommand).GetAwaiter().GetResult();

            var senderTestId         = TestId.New;
            var senderTest           = new Test(senderTestId);
            var nextAggregateCommand = new AddTestCommand(senderAggregateId, CommandId.New, senderTest);

            bus.Publish(nextAggregateCommand).GetAwaiter().GetResult();

            var sagaStartingCommand = new GiveTestCommand(senderAggregateId, CommandId.New, receiverAggregateId, senderTest);

            bus.Publish(sagaStartingCommand).GetAwaiter().GetResult();

            eventProbe.ExpectMsg <DomainEvent <TestSagaId, TestSagaStartedEvent> >(
                x => x.AggregateEvent.Sender.Equals(senderAggregateId) &&
                x.AggregateEvent.Receiver.Equals(receiverAggregateId) &&
                x.AggregateEvent.SentTest.Equals(senderTest));

            eventProbe.ExpectMsg <DomainEvent <TestSagaId, TestSagaTransactionCompletedEvent> >();

            eventProbe.ExpectMsg <DomainEvent <TestSagaId, TestSagaCompletedEvent> >();
        }
Ejemplo n.º 8
0
        public void SendingTest_FromTestAggregate_CompletesSaga()
        {
            var probe = CreateTestActor("probeActor");

            Sys.EventStream.Subscribe(probe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent>));
            var aggregateManager = Sys.ActorOf(Props.Create(() => new TestAggregateManager()), "test-aggregatemanager");

            Sys.ActorOf(Props.Create(() => new TestSagaManager(() => new TestSaga(aggregateManager))), "test-sagaaggregatemanager");

            var senderAggregateId            = TestAggregateId.New;
            var senderCreateAggregateCommand = new CreateTestCommand(senderAggregateId, probe);

            aggregateManager.Tell(senderCreateAggregateCommand);

            var receiverAggregateId            = TestAggregateId.New;
            var receiverCreateAggregateCommand = new CreateTestCommand(receiverAggregateId, probe);

            aggregateManager.Tell(receiverCreateAggregateCommand);

            var senderTestId         = TestId.New;
            var senderTest           = new Test(senderTestId);
            var nextAggregateCommand = new AddTestCommand(senderAggregateId, senderTest);

            aggregateManager.Tell(nextAggregateCommand);



            var sagaStartingCommand = new GiveTestCommand(senderAggregateId, receiverAggregateId, senderTest);

            aggregateManager.Tell(sagaStartingCommand);



            ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent> >(
                x => x.AggregateEvent.Sender.Equals(senderAggregateId) &&
                x.AggregateEvent.Receiver.Equals(receiverAggregateId) &&
                x.AggregateEvent.SentTest.Equals(senderTest));
        }
Ejemplo n.º 9
0
        public void Aggregates_can_be_hydrated()
        {
            var senderTestId = TestId.NewDeterministic(TestIdNamespace, (DateTime.UtcNow.Day - 1).ToString());
            var senderTest   = new Test(senderTestId);
            var receiver     = TestAggregateId.NewDeterministic(TestIdNamespace, (DateTime.UtcNow.Day + 1).ToString());
            var sender       = TestAggregateId.NewDeterministic(TestIdNamespace, DateTime.UtcNow.Day.ToString());

            RunOn(() =>
            {
                //set up event probe
                // set up sender
                var senderProbe = CreateTestProbe("sender-probe");
                var commandId   = CommandId.New;
                _aggregateProxy.Value.Tell(new CreateTestCommand(sender, commandId), senderProbe);

                senderProbe.ExpectMsg <TestExecutionResult>(
                    x => x.Result.IsSuccess &&
                    x.SourceId.Value == commandId.Value, TimeSpan.FromSeconds(10));


                var nextAggregateCommand = new AddTestCommand(sender, CommandId.New, senderTest);
                _aggregateProxy.Value.Tell(nextAggregateCommand, senderProbe);

                senderProbe.ExpectMsg <TestExecutionResult>(
                    x => x.Result.IsSuccess, TimeSpan.FromSeconds(10));

                // set up receiver
                var receiverProbe = CreateTestProbe("receiver-probe");
                var commandId2    = CommandId.New;
                _aggregateProxy.Value.Tell(new CreateTestCommand(receiver, commandId2), receiverProbe);

                receiverProbe.ExpectMsg <TestExecutionResult>(
                    x => x.Result.IsSuccess &&
                    x.SourceId.Value == commandId2.Value, TimeSpan.FromSeconds(10));
            }, _config.Client);


            EnterBarrier("aggregates-hydrated");

            var eventProbe = CreateTestProbe("event-probe");

            RunOn(() =>
            {
                Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent>));
                Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaCompletedEvent>));
                Sys.EventStream.Subscribe(eventProbe, typeof(DomainEvent <TestSaga, TestSagaId, TestSagaTransactionCompletedEvent>));
            }, _config.Worker);

            RunOn(() =>
            {
                var senderProbe = CreateTestProbe("saga-starting-probe");

                var nextAggregateCommand = new GiveTestCommand(sender, CommandId.New, receiver, senderTest);
                _aggregateProxy.Value.Tell(nextAggregateCommand, senderProbe);

                senderProbe.ExpectMsg <TestExecutionResult>(
                    x => x.Result.IsSuccess, TimeSpan.FromSeconds(10));
            }, _config.Client);

            RunOn(() =>
            {
                eventProbe.
                ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaStartedEvent> >(
                    x => x.AggregateEvent.Sender.Equals(sender) &&
                    x.AggregateEvent.Receiver.Equals(receiver) &&
                    x.AggregateEvent.SentTest.Equals(senderTest), TimeSpan.FromSeconds(20));

                eventProbe.
                ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaTransactionCompletedEvent> >(TimeSpan.FromSeconds(20));

                eventProbe.
                ExpectMsg <DomainEvent <TestSaga, TestSagaId, TestSagaCompletedEvent> >(TimeSpan.FromSeconds(20));
            }, _config.Worker);


            EnterBarrier("saga-finished");
        }