Exemplo 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" });
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void When_started_Then_Guardians_should_have_been_created()
        {
            var system = new TestActorSystem();

            system.Start();
            system.RootGuardian.Should().NotBeNull();
            system.UserGuardian.Should().NotBeNull();
            system.SystemGuardian.Should().NotBeNull();
        }
Exemplo n.º 4
0
        public void When_asking_Then_response_is_returned_in_task()
        {
            var system = new TestActorSystem();

            system.Start();
            var actor    = system.CreateActor <EchoActor>().InnerActorRef;
            var response = actor.Ask("message", null);

            response.Result.Should().Be(":message:");
        }
Exemplo n.º 5
0
        public void When_actor_receives_StopActor_message_Then_it_terminates()
        {
            var system = new TestActorSystem();

            system.Start();
            var actor = system.CreateActor(() => new TestActor(system), "Parent");

            actor.Send("stopChild", null);
            actor.Actor.ReceivedTerminate.Should().BeTrue();
        }
        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" });
        }
Exemplo n.º 7
0
        public void When_an_actor_crashes_during_handling_message_Then_it_gets_recreated()
        {
            var system = new TestActorSystem();

            system.Start();

            var numberOfCreateCalls = 0;
            var props = new DelegateActorCreationProperties(() => { numberOfCreateCalls++; return(AnonymousActor.Create <object>(_ => { throw new Exception(); })); });

            var actor = system.CreateActor(props);

            numberOfCreateCalls.Should().Be(1);
            actor.Send("A trigger message that will cause actor to fail", null);
            numberOfCreateCalls.Should().Be(2);
        }
        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();
        }
Exemplo n.º 9
0
        public void When_actor_receives_StopActor_it_process_messages_prior_to_StopActor_message()
        {
            var system = new TestActorSystem();

            system.Start();
            var actor = system.CreateActor(() => new TestActor(system, childMailboxShouldBeOpen: false), "Parent");

            actor.Send("1", null);
            actor.Send("2", null);
            actor.Send("3", null);
            actor.Send("stopChild", null);
            actor.Send("4", null);
            actor.Send("openChildMailbox", null);

            actor.Actor.Replies.Should().ContainInOrder(new[] { "1", "2", "3" });
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public void Given_an_actor_with_children_When_it_crashes_during_handling_message_Then_its_children_are_recreated()
        {
            //The children get created since their parent gets created
            var system = new TestActorSystem();

            system.Start();
            var children = new List <NoopActor>();
            var props    = new DelegateActorCreationProperties(() => new ParentWhichFailsWithChildrenActor(2, _ =>
            {
                var c = new NoopActor();
                children.Add(c);
                return(c);
            }));

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

            children.Clear();
            actor.Send("A trigger message that will cause actor to fail", null);
            children.Should().HaveCount(2);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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.");
            }
        }
Exemplo 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.");
            }
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 18
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.");
            }
        }
Exemplo n.º 19
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 => { })));
        }