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);
        }
Example #2
0
        public void When_actor_Stops_Then_it_sends_ActorTerminated_message_to_supervisor()
        {
            var system = new TestActorSystem();

            system.Start();
            var parentMailbox = new TestMailbox(system.CreateDefaultMailbox());
            ParentWithFailingChildActor parentInstance = null;
            var props = new DelegateActorCreationProperties(() => parentInstance = new ParentWithFailingChildActor())
            {
                MailboxCreator = () => parentMailbox
            };
            var actor = system.CreateActor(props, "Parent");

            actor.Send("A trigger message that will cause actor to fail", null);
            var actorsTerminated = parentMailbox.GetStateChangesForEnquingSystemMessagesOfType <ActorTerminated>().ToList();

            actorsTerminated.Should().HaveCount(1).And.ContainSingle(s => ((ActorTerminated)s.GetLastEnqueuedSystemMessage().Message).TerminatedActor == parentInstance.FailingChild);
        }
Example #3
0
        public void When_an_actor_crashes_during_handling_message_Then_the_actors_mailbox_gets_suspended()
        {
            var system = new TestActorSystem();

            system.Start();

            var mailbox = new TestMailbox(system.CreateDefaultMailbox());
            var props   = new DelegateActorCreationProperties(() => AnonymousActor.Create <object>(_ => { throw new Exception(); }))
            {
                MailboxCreator = () => mailbox
            };

            var actor = system.CreateActor(props);

            actor.Send("A trigger message that will cause actor to fail", null);
            var suspendCalls = mailbox.GetStateChangesFor(TestMailbox.StateChange.Suspend);

            suspendCalls.Count.Should().Be(1);
        }
        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);
        }
Example #5
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.");
            }
        }
Example #6
0
        public void Given_an_actor_with_children_When_it_crashes_during_handling_message_Then_its_children_terminates_and_sends_ActorTerminated_to_their_parent()
        {
            //The children get created since their parent gets created
            var system = new TestActorSystem();

            system.Start();
            var mailbox = new TestMailbox(system.CreateDefaultMailbox());
            var props   = new DelegateActorCreationProperties(() => new ParentWhichFailsWithChildrenActor(2, childName: i => "Child" + i))
            {
                MailboxCreator = () => mailbox
            };

            var actor = system.CreateActor(props, "Parent");

            actor.Send("A trigger message that will cause actor to fail", null);
            var actorTerminatedMessages = mailbox.GetStateChangesForEnquingSystemMessagesOfType <ActorTerminated>();

            actorTerminatedMessages.Count.Should().Be(2);
            var terminatedActorNames = actorTerminatedMessages.Select(m => ((ActorTerminated)m.GetLastEnqueuedSystemMessage().Message).TerminatedActor.Name).OrderBy(n => n).ToList();

            terminatedActorNames.Should().ContainInOrder(new [] { "Child1", "Child2" });
        }
        public void When_a_child_actor_is_created_Then_it_sends_SuperviseActor_message_to_parent()
        {
            var system = new TestActorSystem();

            system.Start();

            var mailbox = new TestMailbox(system.CreateDefaultMailbox());
            ParentWithChildActor parent = null;
            var parentProps             = new DelegateActorCreationProperties(() =>
            {
                parent = new ParentWithChildActor();
                return(parent);
            })
            {
                MailboxCreator = () => mailbox
            };

            var parentRef    = system.CreateActor(parentProps, "Parent");
            var stateChanges = mailbox.GetStateChangesForEnquingSystemMessagesOfType <SuperviseActor>();

            stateChanges.Count.Should().Be(1);
            ((SuperviseActor)stateChanges.First().GetLastEnqueuedSystemMessage().Message).ActorToSupervise.Should().BeSameAs(parent.Child);
        }
Example #8
0
        public void Given_an_actor_with_children_When_it_crashes_during_handling_message_Then_its_children_are_stopped()
        {
            var system = new TestActorSystem();

            system.Start();
            var childrenMailboxes = Enumerable.Range(1, 10).Select(_ => new TestMailbox(system.CreateDefaultMailbox())).ToList();
            var mailbox           = new TestMailbox(system.CreateDefaultMailbox());
            var props             = new DelegateActorCreationProperties(() => new ParentWhichFailsWithChildrenActor(childrenMailboxes))
            {
                MailboxCreator = () => mailbox
            };

            var actor = system.CreateActor(props);

            actor.Send("A trigger message that will cause actor to fail", null);
            var childTerminations = childrenMailboxes.Select(m => m.GetStateChangesForEnquingSystemMessagesOfType <TerminateActor>()).ToList();

            childTerminations.Count.Should().Be(10);
            for (int i = 0; i < childTerminations.Count; i++)
            {
                var terminateCalls = childTerminations[i];
                terminateCalls.Count.Should().Be(1, "Mailbox for child " + i + " should have been terminated.");
            }
        }
Example #9
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.");
            }
        }