Ejemplo n.º 1
0
        public void When_actor_forwards_messages_of_specific_types_Then_it_calls_send_on_receiving_actor()
        {
            var testActorSystem = new TestActorSystem();

            testActorSystem.Start();
            TestActor recipientActor  = null;
            var       recipient       = testActorSystem.CreateActor(ActorCreationProperties.Create(() => { recipientActor = new TestActor(); return(recipientActor); }));
            var       receivedObjects = new List <object>();
            var       sut             = testActorSystem.CreateActor(ActorCreationProperties.Create(() => AnonymousActor.Create(c =>
            {
                // ReSharper disable ConvertClosureToMethodGroup
                c.ReceiveAndForward <int>(recipient);
                c.ReceiveAndForward <float>(recipient);
                c.ReceiveAny(o => receivedObjects.Add(o));
                // ReSharper restore ConvertClosureToMethodGroup
            })));

            var senderActor = testActorSystem.CreateActor(ActorCreationProperties.Create(() => new SendingActor(sut, 1, "2", 3.0f)));

            senderActor.Send("Send 1 2 and 3.0", null);
            recipientActor.ReceivedMessages.Should().HaveCount(2);
            recipientActor.ReceivedMessages[0].Item1.Should().BeSameAs(senderActor);
            recipientActor.ReceivedMessages[0].Item2.Should().Be(1);
            recipientActor.ReceivedMessages[1].Item1.Should().BeSameAs(senderActor);
            recipientActor.ReceivedMessages[1].Item2.Should().Be(3.0f);
            receivedObjects.Should().BeEquivalentTo(new object[] { "2" });
        }
Ejemplo n.º 2
0
        public ActorRefWithActor <TActor> CreateActor <TActor>(string name = null) where TActor : Actor, new()
        {
            TActor actor    = null;
            var    actorRef = CreateActor(ActorCreationProperties.Create(() => { actor = new TActor(); return(actor); }), name);

            return(new ActorRefWithActor <TActor>(actorRef, actor));
        }
Ejemplo n.º 3
0
            public ParentWithFailingChildActor(SupervisorStrategy supervisorStrategy = null)
            {
                _supervisorStrategy = supervisorStrategy;

                _failingChild = CreateActor(ActorCreationProperties.Create <StoppingActor>(), "FailingChild");
                ReceiveAnyAndForward(_failingChild);
            }
Ejemplo n.º 4
0
        public void When_a_dead_actor_is_watched_Then_an_WatchedActorTerminated_message_is_sent_to_watcher()
        {
            var system = new TestActorSystem();

            system.Start();

            var mailbox      = new TestMailbox(system.CreateDefaultMailbox());
            var watchedActor = system.CreateActor(ActorCreationProperties.Create <StoppingActor>(), "WatchedActor");
            var watcherProps = new DelegateActorCreationProperties(() => new WatchingActor(watchedActor))
            {
                MailboxCreator = () => mailbox
            };

            watchedActor.Send("stop", null);

            var watcher = system.CreateActor(watcherProps, "Watcher");

            mailbox.ClearEnqueuedSystemMessages();
            mailbox.ClearEnqueuedMessages();

            watcher.Send("watch", null);

            var watchSystemMessages = mailbox.GetEnquedSystemMessagesOfType <ActorTerminated>();

            watchSystemMessages.Should().HaveCount(1);
            watchSystemMessages[0].TerminatedActor.Should().BeSameAs(watchedActor);
            var watchMessages = mailbox.GetEnquedMessagesOfType <WatchedActorTerminated>();

            watchMessages.Should().HaveCount(1);
            watchMessages[0].TerminatedActor.Should().BeSameAs(watchedActor);
        }
Ejemplo n.º 5
0
        public void Given_an_actor_When_creating_another_actor_with_the_same_name_Then_it_fails()
        {
            var tuple        = GetActorCreator();
            var actorCreator = tuple.Item1;

            actorCreator.CreateActor(ActorCreationProperties.Create <TestActor>(), "NamedActor");
            Assert.Throws <InvalidActorNameException>(() => actorCreator.CreateActor(ActorCreationProperties.Create <TestActor>(), "NamedActor"));
        }
Ejemplo n.º 6
0
        public void When_an_actor_crashes_during_handling_message_Then_Failed_message_is_sent_to_parent()
        {
            var supervisor = A.Fake <InternalActorRef>();
            var child      = new LocalActorRef(new TestActorSystem(), ActorCreationProperties.CreateAnonymous(c => c.ReceiveAny(_ => { throw new Exception("Child failed"); })), new RootActorPath("child"), new UnboundedMailbox(new SynchronousScheduler()), supervisor);

            ((InternalActorRef)child).Start();
            child.Send("A trigger message that will cause Child to fail", null);
            A.CallTo(() => supervisor.SendSystemMessage(A <SystemMessage> .That.Matches(m => m is ActorFailed), child)).MustHaveHappened();
        }
Ejemplo n.º 7
0
        public void When_creating_actors_in_system_Then_they_should_be_created_in_UserGuardian()
        {
            var system = new ActorSys();

            system.Start();
            system.CreateActor(ActorCreationProperties.Create <TestActor>(), "Actor1");
            system.CreateActor(ActorCreationProperties.Create <TestActor>(), "Actor2");
            system.CreateActor(ActorCreationProperties.Create <TestActor>(), "Actor3");
            system.Children.Should().ContainInOrder(new object[] { "Actor1", "Actor2", "Actor3" });
        }
Ejemplo n.º 8
0
        public void Given_an_actor_Then_it_should_be_possible_to_create_a_child_actor_and_forward_messages_to_it()
        {
            var tuple        = GetActorCreator();
            var actorCreator = tuple.Item1;

            CreateChildTestActor actor = null;
            var actorref = actorCreator.CreateActor(ActorCreationProperties.Create(() => { actor = new CreateChildTestActor(); return(actor); }));

            actorref.Send("123", null);
            actor.ChildReceivedMessages.Should().ContainInOrder(new object[] { "123" });
        }
Ejemplo n.º 9
0
 public virtual ActorRef CreateActor(ActorCreationProperties actorCreationProperties, string name = null)
 {
     if (name != null)
     {
         ActorNameValidator.EnsureNameIsValid(name);
     }
     else
     {
         name = _system.UniqueNameCreator.GetNextRandomName();
     }
     return(CreateLocalActorReference(actorCreationProperties, name));
 }
        public void When_an_actor_restarts_Then_its_lifecycle_events_are_called()
        {
            var system = new TestActorSystem();

            system.Start();
            var actors = new List <TestActor>();
            var props  = ActorCreationProperties.Create(() => { actors.Add(new TestActor()); return(actors[actors.Count - 1]); });
            var actor  = system.CreateActor(props);

            actors[0].Calls.Clear();
            actor.Send("A trigger message that will cause actor to fail", null);
            actors.Should().HaveCount(2);
            actors[0].Calls.Should().ContainInOrder(new[] { "PreRestart", "PostStop" });
            actors[1].Calls.Should().ContainInOrder(new[] { "PreStart", "PostRestart" });
        }
        public void When_an_actor_is_created_Then_its_prestart_is_called_before_messages_are_processed()
        {
            var system = new TestActorSystem();

            system.Start();
            PrestartActor prestartActor = null;
            var           child         = system.CreateActor(ActorCreationProperties.Create(() =>
            {
                prestartActor = new PrestartActor();
                return(prestartActor);
            }));

            child.Send("A message", null);
            prestartActor.PrestartCalledFirst.Should().BeTrue();
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            var system = Bootstrapper.Instance.CreateSystemWithSettings(settings =>
            {
                settings.StandardOutLoggerSettings.LogLevel = LogLevel.Debug;
            });

            system.Start();
            var actor = system.CreateActor(ActorCreationProperties.Create <HelloWorldActor>(), "HelloWorld");

            actor.Send("World", null);

            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
Ejemplo n.º 13
0
        private InternalActorRef CreateLocalActorReference(ActorCreationProperties actorCreationProperties, string name, InternalActorRef supervisor)
        {
            InternalActorRef actorRef;

            try
            {
                ReserveChild(name);
                var instanceId = CreateInstanceId();
                var path       = new ChildActorPath(_path, name, instanceId);
                actorRef = _system.LocalActorRefFactory.CreateActor(_system, actorCreationProperties, supervisor, path);
            }
            catch
            {
                ReleaseChild(name);
                throw;
            }
            actorRef.Start();
            return(actorRef);
        }
Ejemplo n.º 14
0
        public void When_watching_another_actor_Then_a_Watch_message_is_sent_to_that_actor()
        {
            var system = new TestActorSystem();

            system.Start();

            var mailbox           = new TestMailbox(system.CreateDefaultMailbox());
            var watchedActorProps = new DelegateActorCreationProperties(() => AnonymousActor.Create <object>(_ => { }))
            {
                MailboxCreator = () => mailbox
            };
            var watchedActor = system.CreateActor(watchedActorProps, "WatchedActor");
            var watcher      = system.CreateActor(ActorCreationProperties.Create(() => new WatchingActor(watchedActor)), "Watcher");

            watcher.Send("watch", null);
            var watchMessages = mailbox.GetEnquedSystemMessagesOfType <WatchActor>();

            watchMessages.Should().HaveCount(1);
            watchMessages[0].Watcher.Should().BeSameAs(watcher);
        }
Ejemplo n.º 15
0
        public void When_an_actor_crashes_during_handling_message_Then_its_children_gets_suspended()
        {
            var system = new TestActorSystem();

            system.Start();

            var childrenMailboxes = Enumerable.Range(1, 10).Select(_ => new TestMailbox(system.CreateDefaultMailbox())).ToList();


            var parent = system.CreateActor(ActorCreationProperties.Create(() => new ParentWhichFailsWithChildrenActor(childrenMailboxes)));

            parent.Send("A trigger message that will cause the parent actor to fail", null);
            var childSuspends = childrenMailboxes.Select(m => m.GetStateChangesFor(TestMailbox.StateChange.Suspend)).ToList();

            childSuspends.Count.Should().Be(10);
            for (int i = 0; i < childSuspends.Count; i++)
            {
                var suspendCalls = childSuspends[i];
                suspendCalls.Count.Should().Be(1, "Mailbox for child " + i + " should have been suspended.");
            }
        }
Ejemplo n.º 16
0
        public void Given_supervisor_that_restarts_all_children_When_child_crashes_Then_all_its_siblings_are_restarted()
        {
            var system = new TestActorSystem();

            system.Start();
            var failingChildMailbox = new TestMailbox(system.CreateDefaultMailbox());
            var childrenMailboxes   = Enumerable.Range(1, 10).Select(_ => new TestMailbox(system.CreateDefaultMailbox())).ToList();


            var parent = system.CreateActor(ActorCreationProperties.Create(() => new ParentWithFailingChildActor(failingChildMailbox, childrenMailboxes, AllForOneSupervisorStrategy.DefaultAllForOne)));

            parent.Send("A trigger message that will cause the child actor to fail", null);
            var childRestarts = childrenMailboxes.Select(m => m.GetStateChangesForEnquingSystemMessagesOfType <RecreateActor>()).ToList();

            childRestarts.Count.Should().Be(10);
            for (int i = 0; i < childRestarts.Count; i++)
            {
                var suspendCalls = childRestarts[i];
                suspendCalls.Count.Should().Be(1, "Mailbox for child " + i + " should have been restarted.");
            }
        }
Ejemplo n.º 17
0
        protected override Tuple <IActorCreator, ActorSystem> GetActorCreator(LocalActorRefFactory localActorRefFactory, IBootstrapper bootstrapper = null)
        {
            if (bootstrapper == null)
            {
                var testBootstrapper = new TestBootstrapper();
                if (localActorRefFactory != null)
                {
                    testBootstrapper.LocalActorRefFactory = localActorRefFactory;
                }
                bootstrapper = testBootstrapper;
            }

            var system = new InternalActorSystem("default", bootstrapper);

            system.Start();
            Actor actor = null;

            system.CreateActor(ActorCreationProperties.Create(() =>
            {
                actor = new ParentActor();
                return(actor);
            }), "Parent");
            return(new Tuple <IActorCreator, ActorSystem>(actor, system));
        }
Ejemplo n.º 18
0
            public CreateChildTestActor()
            {
                var child = CreateActor(ActorCreationProperties.CreateAnonymous <object>(msg => ChildReceivedMessages.Add(msg)));

                ReceiveAny(m => child.Send(m, Self));
            }
Ejemplo n.º 19
0
 public abstract InternalActorRef CreateActor(ActorSystem system, ActorCreationProperties actorCreationProperties, InternalActorRef supervisor, ActorPath path);
Ejemplo n.º 20
0
 protected InternalActorRef CreateLocalActorReference(ActorCreationProperties actorCreationProperties, string name)
 {
     return(CreateLocalActorReference(actorCreationProperties, name, this));
 }
Ejemplo n.º 21
0
        public override InternalActorRef CreateActor(ActorSystem system, ActorCreationProperties actorCreationProperties, InternalActorRef supervisor, ActorPath path)
        {
            var mailbox = actorCreationProperties.CreateMailbox() ?? system.CreateDefaultMailbox();

            return(new LocalActorRef(system, actorCreationProperties, path, mailbox, supervisor));
        }
Ejemplo n.º 22
0
        public void Given_a_system_that_has_not_been_started_When_creating_an_actor_Then_it_should_fail()
        {
            var system = new TestActorSystem();

            Assert.Throws <InvalidOperationException>(() => system.CreateActor(ActorCreationProperties.CreateAnonymous(c => { })));
        }