public InternalActorRef CreateGuardian(Func <Actor> actorFactory, string name) { var props = new DelegateActorCreationProperties(actorFactory); var guardian = CreateLocalActorReference(props, name); return(guardian); }
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); }
public void Given_an_ActorCreator_When_creating_actors_with_valid_name_Then_it_succeeds(string name) { var delegateActorFactory = new DelegateActorCreationProperties(() => new FakeActor()); var tuple = GetActorCreator(); var actorCreator = tuple.Item1; actorCreator.CreateActor(delegateActorFactory, name: name).Should().NotBeNull(); }
public void Given_an_ActorCreator_When_creating_actors_with_invalid_name_Then_it_fails(string name) { var delegateActorFactory = new DelegateActorCreationProperties(() => new FakeActor()); var tuple = GetActorCreator(); var actorCreator = tuple.Item1; Assert.Throws <InvalidActorNameException>(() => actorCreator.CreateActor(delegateActorFactory, name: name)); }
public void Given_a_DelegateActorFactory_When_creating_a_new_actor_Then_the_supplied_function_is_called() { var functionWasCalled = false; var factory = new DelegateActorCreationProperties(() => { functionWasCalled = true; return(null); }); factory.CreateNewActor(); functionWasCalled.Should().BeTrue(); }
public void Given_a_factory_created_by_ActorFactory_When_creating_actors_Then_they_are_different() { var factory = new DelegateActorCreationProperties(ActorHelper.CreateActorDirectly <FakeActor>); var actor1 = factory.CreateNewActor(); var actor2 = factory.CreateNewActor(); actor1.Should().NotBeSameAs(actor2); }
public void Given_an_ActorCreator_When_creating_actors_with_no_names_Then_they_are_assigned_random_different_names() { var delegateActorFactory = new DelegateActorCreationProperties(() => new FakeActor()); var tuple = GetActorCreator(); var actorCreator = tuple.Item1; var actorRef1 = actorCreator.CreateActor(delegateActorFactory, name: null); var actorRef2 = actorCreator.CreateActor(delegateActorFactory, name: null); actorRef1.Name.Should().NotBe(actorRef2.Name); }
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 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); }
public void When_created_actor_Then_start_is_called_on_LocalActorRef() { var fakeLocalActorRefFactory = A.Fake <DefaultLocalActorRefFactory>(); A.CallTo(fakeLocalActorRefFactory).CallsBaseMethod(); var fakeActorRef = A.Fake <InternalActorRef>(); var tuple = GetActorCreator(fakeLocalActorRefFactory); var actorSystem = tuple.Item2; var actorCreator = tuple.Item1; var delegateActorCreationProperties = new DelegateActorCreationProperties(() => new FakeActor()); A.CallTo(() => fakeLocalActorRefFactory.CreateActor(actorSystem, A <ActorCreationProperties> .That.IsSameAs(delegateActorCreationProperties), A <InternalActorRef> .Ignored, A <ActorPath> .Ignored)).ReturnsLazily(() => { return(fakeActorRef); }); var actorRef = actorCreator.CreateActor(delegateActorCreationProperties); A.CallTo(() => fakeActorRef.Start()).MustHaveHappened(); }
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); }
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 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); }
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); }
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); }
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."); } }