Exemple #1
0
 // ReSharper disable once VirtualMemberNeverOverriden.Global   Init is virtual in order to be mockable
 internal virtual void Init(LocalActorRef self)
 {
     //This might be called directly after the constructor, or when the same actor instance has been returned
     //during recreate. In the second case, _self has been set to DeadLetter so we need to set it again.
     _self = self;
     if (!_hasBeenInitialized)                   //Do not perform this when "recreating" the same instance
     {
         _defaultMessageHandler = BuildNewHandler();
         _constructorMessageHandlerConfigurators.Pop();
         _hasBeenInitialized = true;
     }
 }
Exemple #2
0
        public void When_handling_CreateActor_message_Then_Init_is_called_on_the_new_actor()
        {
            var   mailbox           = A.Fake <Mailbox>();
            Actor actor             = null;
            var   actorInstantiator = A.Fake <ActorInstantiator>();

            //Note: NEVER do this in actual code (returning a premade instance). Always create new instances.
            A.CallTo(() => actorInstantiator.CreateNewActor()).ReturnsLazily(() => { actor = A.Fake <Actor>(); return(actor); });
            var actorRef = new LocalActorRef(new TestActorSystem(), actorInstantiator, new RootActorPath("test"), mailbox, A.Dummy <InternalActorRef>());

            actorRef.HandleSystemMessage(new SystemMessageEnvelope(actorRef, new CreateActor(), A.Fake <ActorRef>()));
            A.CallTo(() => actor.Init(actorRef)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemple #3
0
        public void When_sending_Then_the_message_is_wrapped_in_an_envelope_and_enqueued_on_the_mailbox()
        {
            var mailbox  = A.Fake <Mailbox>();
            var messages = new List <Envelope>();

            A.CallTo(() => mailbox.Enqueue(A <Envelope> .Ignored)).Invokes(x => messages.Add(x.GetArgument <Envelope>(0)));

            var actorRef = new LocalActorRef(new TestActorSystem(), A.Fake <ActorCreationProperties>(), new RootActorPath("test"), mailbox, A.Dummy <InternalActorRef>());

            ((InternalActorRef)actorRef).Start();
            actorRef.Send("MyTestMessage", null);
            messages.Should().Contain(e => e.Message is string && ((string)e.Message) == "MyTestMessage");
        }
Exemple #4
0
 /// <summary>
 /// This one is used for internal testing only
 /// </summary>
 internal Actor(LocalActorRef actorRef, ActorSystem system, LocalActorRefFactory localActorRefFactory)
 {
     if (actorRef == null)
     {
         if (!LocalActorRefStack.TryGetActorRefFromStack(out actorRef))
         {
             throw new InvalidOperationException(StringFormat.SafeFormat("Cannot create a new instance of type {0} directly using new(). An actor can only be created via the CreateActor methods.", GetType().FullName));
         }
         LocalActorRefStack.MarkActorRefConsumedInStack();
     }
     _system = system;
     _self   = actorRef;
     PrepareForConfiguringMessageHandler();
     _localActorRefFactory = localActorRefFactory;
 }
        public async Task Setup()
        {
            _timeout       = TimeSpan.FromMinutes(1);
            _system        = ActorSystem.Create("system");
            _parentActor   = _system.ActorOf(Props.Create(() => new ActorWithChild()), "parent");
            _localActorRef = (LocalActorRef)await _parentActor.Ask <IActorRef>(_createMessage, _timeout);

            _cell = _parentActor.As <ActorRefWithCell>().Underlying.As <ActorCell>();
            _repointableActorRef = (RepointableActorRef)_parentActor;

            var exp = _system.As <ExtendedActorSystem>();

            var vPath = exp.Guardian.Path / "testTemp";

            _virtualPathContainer =
                new VirtualPathContainer(exp.Provider, vPath, exp.Guardian, exp.Log);

            _virtualPathContainer.AddChild("foo",
                                           new EmptyLocalActorRef(exp.Provider, vPath / "foo", exp.EventStream));
        }
Exemple #6
0
        public void When_handling_CreateActor_message_Then_the_LocalActorRef_is_pushed_to_stack_and_afterwards_removed()
        {
            var mailbox           = A.Fake <Mailbox>();
            var actorInstantiator = A.Fake <ActorInstantiator>();
            ImmutableStack <LocalActorRef> stackDuringActorCreation = null;

            A.CallTo(() => actorInstantiator.CreateNewActor()).Invokes(() =>
            {
                stackDuringActorCreation = ActorHelper.GetActorRefStack();
            }).ReturnsLazily(() => new TestActor());
            var actorRef = new LocalActorRef(new TestActorSystem(), actorInstantiator, new RootActorPath("test"), mailbox, A.Dummy <InternalActorRef>());

            actorRef.HandleSystemMessage(new SystemMessageEnvelope(actorRef, new CreateActor(), A.Fake <ActorRef>()));
            var stackAfterActorCreation = ActorHelper.GetActorRefStack();

            stackDuringActorCreation.IsEmpty.Should().BeFalse("The stack should contain one item");
            stackDuringActorCreation.Peek().Should().BeSameAs(actorRef, "The item on stack should be the LocalActorRef that creates the actor");
            stackDuringActorCreation.Count().Should().Be(1, "The stack should only contain one item.");
            (stackAfterActorCreation == null || stackAfterActorCreation.IsEmpty).Should().BeTrue("The stack should be empty after creation");
        }
Exemple #7
0
        public void When_handling_message_Then_it_is_forwarded_to_the_actor_and_sender_is_set()
        {
            var mailbox           = A.Fake <Mailbox>();
            var actor             = ActorHelper.CreateActorDirectly <TestActor>();
            var actorInstantiator = A.Fake <ActorInstantiator>();

            //Note: NEVER do this in actual code (returning a premade instance). Always create new instances.
            A.CallTo(() => actorInstantiator.CreateNewActor()).Returns(actor);
            var actorRef = new LocalActorRef(new TestActorSystem(), actorInstantiator, new RootActorPath("test"), mailbox, A.Dummy <InternalActorRef>());
            var message  = new object();
            var sender   = A.Fake <ActorRef>();

            A.CallTo(() => sender.Name).Returns("SenderActor");

            //Send Create message so that the instance is created
            actorRef.HandleSystemMessage(new SystemMessageEnvelope(actorRef, new CreateActor(), A.Fake <ActorRef>()));


            actorRef.HandleMessage(new Envelope(actorRef, message, sender));

            actor.ReceivedMessages.Should().HaveCount(1);
            actor.ReceivedMessages[0].Item2.Should().BeSameAs(message);
            actor.ReceivedMessages[0].Item1.Name.Should().Be("SenderActor");
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="StopChild" /> class.
 /// </summary>
 /// <param name="child">The child.</param>
 public StopChild(LocalActorRef child)
 {
     Child = child;
 }
Exemple #9
0
 /// <summary>
 /// This will create an actor.
 /// <remarks>NOTE! Only use this in tests</remarks>
 /// </summary>
 public static T CreateActorDirectly <T>(out LocalActorRef localActorRef) where T : Actor, new()
 {
     return((T)CreateActorDirectly(() => new T(), out localActorRef));
 }