Ejemplo n.º 1
0
        public void Easy_syntax_love()
        {
            ServiceController controller = null;

            ActorFactory <ServiceController> factory =
                StateMachineActorFactory.Create <ServiceWorkflow, ServiceController>(inbox =>
            {
                controller = new ServiceController(inbox);
                return(controller);
            }, x =>
            {
                x.AccessCurrentState(s => s.CurrentState);

                x.Initially()
                .When(e => e.Started)
                .Then(i => i.OnStart())
                .TransitionTo(s => s.Running);

                x.During(s => s.Running)
                .When(e => e.Stopped)
                .Then(i => i.OnStop())
                .Finalize()
                .When(e => e.Paused)
                .Then(i => i.OnPause)
                .TransitionTo(s => s.Idle);

                x.During(s => s.Idle)
                .When(e => e.Started)
                .TransitionTo(s => s.Running);
            });

            ActorRef service = factory.GetActor();

            service.Send(new Pause());
            service.Send(new Start());
            service.Send(new Start());
            service.Send(new Stop());

            try
            {
                controller.Started.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received start");
                controller.Paused.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received pause");
                controller.Stopped.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received stop");
            }
            finally
            {
                service.Exit();
            }
        }
Ejemplo n.º 2
0
        public void Easy_syntax_love()
        {
            ActorFactory <QuoteService> factory =
                StateMachineActorFactory.Create <QuoteServiceWorkflow, QuoteService>(inbox => new QuoteService(inbox), x =>
            {
                x.AccessCurrentState(s => s.CurrentState);

                x.Initially()
                .When(e => e.GetQuoteRequest)
                .Then(i => i.SendQuoteRequest)
                .TransitionTo(s => s.WaitingForResponse);

                x.During(s => s.WaitingForResponse)
                .When(e => e.CancelRequest)
                .Then(i => i.Cancel)
                .Finalize();
            });

            ActorRef service = factory.GetActor();


            var response = new Future <Response <RequestSent> >();

            var actor = AnonymousActor.New(inbox =>
            {
                service.Request(new GetQuote
                {
                    Symbol = "MSFT"
                }, inbox)
                .Receive <Response <RequestSent> >(r =>
                {
                    service.Request(new GetQuote
                    {
                        Symbol = "AAPL"
                    }, inbox)
                    .Receive <Response <RequestSent> >(rr =>
                    {
                        response.Complete(r);
                    });
                });
            });

            response.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            service.Exit();
        }