Exemple #1
0
        public void When_actor_handles_messages_Then_it_calls_the_correct_handler()
        {
            var          receivedStrings     = new List <string>();
            var          receivedInts        = new List <int>();
            var          receivedFloats      = new List <float>();
            var          receivedObjects     = new List <object>();
            var          allRecievedMessages = new List <object>();
            Func <Actor> actorFactory        = () => AnonymousActor.Create(c =>
            {
                // ReSharper disable ConvertClosureToMethodGroup
                c.AddReceiver(typeof(object), msg => { allRecievedMessages.Add(msg); return(false); });
                c.Receive <float>(f => receivedFloats.Add(f));
                c.Receive <int>(i => receivedInts.Add(i));
                c.ReceiveAny(o => receivedObjects.Add(o));
                c.Receive <string>(s => receivedStrings.Add(s));               //Since we have a Catch-all above then no floats should end up here
                // ReSharper restore ConvertClosureToMethodGroup
            });
            var actor = ActorHelper.CreateInitializedActorDirectly(actorFactory);

            actor.HandleMessage(1.0f);
            actor.HandleMessage(2);
            actor.HandleMessage(true);
            actor.HandleMessage("4");

            receivedFloats.Should().BeEquivalentTo(1.0f);
            receivedInts.Should().BeEquivalentTo(2);
            receivedObjects.Should().BeEquivalentTo(true, "4");
            receivedStrings.Should().BeEmpty();
            allRecievedMessages.Should().BeEquivalentTo(1.0f, 2, true, "4");
        }
Exemple #2
0
            public ParentWithFailingChildActor(Mailbox failingChildMailbox, IEnumerable <Mailbox> siblingMailboxes, SupervisorStrategy supervisorStrategy = null)
            {
                _supervisorStrategy = supervisorStrategy;
                var failingChildProps = new DelegateActorCreationProperties(() => AnonymousActor.Create <object>(_ => { throw new Exception(); }))
                {
                    MailboxCreator = () => failingChildMailbox
                };
                Func <Mailbox, ActorCreationProperties> createSibling = m => new DelegateActorCreationProperties(() => new NoopActor())
                {
                    MailboxCreator = () => m
                };

                var failingChild = CreateActor(failingChildProps, "FailingChild");

                siblingMailboxes.ForEach((m, i) => CreateActor(createSibling(m), "Sibling" + i));
                ReceiveAnyAndForward(failingChild);
            }
Exemple #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);
        }
Exemple #5
0
            public TestActor(ActorSystem system, bool childMailboxShouldBeOpen = true)
            {
                Replies = new List <string>();
                var childMailbox = new DelayedTestMailbox(system.CreateDefaultMailbox());
                var child        = CreateActor(new DelegateActorCreationProperties(() => AnonymousActor.Create(c => c.ReceiveAny((msg, sender) => sender.Reply(msg))))
                {
                    MailboxCreator = () => childMailbox
                }, "Child");

                if (childMailboxShouldBeOpen)
                {
                    childMailbox.Open();
                }

                Watch(child);

                Receive <WatchedActorTerminated>(terminated => ReceivedTerminate = true, m => m.TerminatedActor == child); //Record that child terminated
                Receive <string>(_ => child.Send(StopActor.Instance, Self), s => s == "stopChild");                        //Send Stop to child when any other message is received
                Receive <string>(_ => childMailbox.Open(), s => s == "openChildMailbox");
                Receive <string>(s => Replies.Add(s), _ => Sender.Path == child.Path);                                     //Record replies from child
                ReceiveAny(m => child.Send(m, Self));                                                                      //Forward all other messages
            }
Exemple #6
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" });
        }
Exemple #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);
        }