Ejemplo n.º 1
0
        public void Props_created_with_strategy_must_have_it_set()
        {
            var strategy = new OneForOneStrategy(_ => Directive.Stop);
            var props = Props.Create(() => new PropsTestActor(), strategy);

            Assert.Equal(strategy, props.SupervisorStrategy);
        }
 public void Serialization_must_serialize_and_deserialize_DaemonMsgCreate_with_Deploy_and_RouterConfig()
 {
     var supervisorStrategy = new OneForOneStrategy(3, TimeSpan.FromSeconds(10), exception => Directive.Escalate);
     var deploy1 = new Deploy("path1",
         ConfigurationFactory.ParseString("a=1"),
         new RoundRobinPool(5, null, supervisorStrategy, null),
         new RemoteScope(new Address("akka", "Test", "host1", 1921)),
         "mydispatcher");
     var deploy2 = new Deploy("path2",
         ConfigurationFactory.ParseString("a=2"),
         FromConfig.Instance,
         new RemoteScope(new Address("akka", "Test", "host2", 1922)),
         Deploy.NoDispatcherGiven);
     VerifySerialization(new DaemonMsgCreate(Props.Create<MyActor>().WithDispatcher("my-disp").WithDeploy(deploy1), deploy2, "foo", _supervisor));
 }
        public void Cluster_aware_routers_must_use_provided_supervisor_strategy()
        {
            var escalator = new OneForOneStrategy(
                exception =>
                {
                    TestActor.Tell("supervised");
                    return Directive.Stop;
                });

            var settings = new ClusterRouterPoolSettings(1, 1, true);

            var router =
                Sys.ActorOf(
                    new ClusterRouterPool(new RoundRobinPool(1, null, escalator, Dispatchers.DefaultDispatcherId),
                        settings)
                        .Props(Props.Create(() => new KillableActor(TestActor))), "therouter");

            router.Tell("go away");
            ExpectMsg("supervised");
        }
Ejemplo n.º 4
0
        public void An_actor_Must_process_stashed_messages_after_restart()
        {
            SupervisorStrategy strategy = new OneForOneStrategy(2, TimeSpan.FromSeconds(1), e => Directive.Restart);
            var boss = ActorOf(() => new Supervisor(strategy));
            var restartLatch = CreateTestLatch();
            var hasMsgLatch = CreateTestLatch();
            var slaveProps = Props.Create(() => new SlaveActor(restartLatch, hasMsgLatch, "stashme"));

            //Send the props to supervisor, which will create an actor and return the ActorRef
            var slave = boss.AskAndWait<IActorRef>(slaveProps, TestKitSettings.DefaultTimeout);

            //send a message that will be stashed
            slave.Tell("stashme");

            //this will crash the slave.
            slave.Tell("crash");

            //During preRestart restartLatch is opened
            //After that the cell will unstash "stashme", it should be received by the slave and open hasMsgLatch
            restartLatch.Ready(TimeSpan.FromSeconds(10));
            hasMsgLatch.Ready(TimeSpan.FromSeconds(10));
        }
Ejemplo n.º 5
0
        public void RemoteRouter_must_set_supplied_SupervisorStrategy()
        {
            var probe = CreateTestProbe(masterActorSystem);
            var escalator = new OneForOneStrategy(ex =>
            {
                probe.Ref.Tell(ex);
                return Directive.Escalate;
            });

            var router =
                masterActorSystem.ActorOf(
                    new RemoteRouterConfig(new RoundRobinPool(1, null, escalator, null),
                        new[] { new Address("akka.tcp", sysName, "localhost", port) })
                        .Props(Props.Empty), "blub3");

            router.Tell(new GetRoutees(), probe.Ref);
            // Need to be able to bind EventFilter to additional actor system (masterActorSystem in this case) before this code works
            // EventFilter.Exception<ActorKilledException>().ExpectOne(() => 
            probe.ExpectMsg<Routees>().Members.Head().Send(Kill.Instance, TestActor);
            //);
            probe.ExpectMsg<ActorKilledException>();
        }
        public void A_constructed_AllForOne_supervisor_strategy_with_nullable_timeouts_and_a_decider_has_the_expected_properties(TimeSpan? timeout, int expectedTimeoutMilliseconds)
        {
            var uut = new OneForOneStrategy(-1, timeout, Decider.From(Directive.Restart));

            Assert.Equal(uut.WithinTimeRangeMilliseconds, expectedTimeoutMilliseconds);
        }
        public void A_constructed_AllForOne_supervisor_strategy_with_nullable_retries_and_a_decider_has_the_expected_properties(int? retries, int expectedRetries)
        {
            var uut = new OneForOneStrategy(retries, null, Decider.From(Directive.Restart));

            Assert.Equal(uut.MaxNumberOfRetries, expectedRetries);
        }
Ejemplo n.º 8
0
 private RootGuardianActorRef CreateRootGuardian(ActorSystemImpl system)
 {
     var supervisor = new RootGuardianSupervisor(_rootPath, this, _terminationPromise, _log);
     var rootGuardianStrategy = new OneForOneStrategy(ex =>
     {
         _log.Error(ex, "Guardian failed. Shutting down system");
         return Directive.Stop;
     });
     var props = Props.Create<GuardianActor>(rootGuardianStrategy);
     var rootGuardian = new RootGuardianActorRef(system, props, DefaultDispatcher, _defaultMailbox, supervisor, _rootPath, _deadLetters, _extraNames);
     return rootGuardian;
 }
Ejemplo n.º 9
0
        public void An_actor_that_clears_the_stash_on_preRestart_Must_not_receive_previously_stashed_messages()
        {
            SupervisorStrategy strategy = new OneForOneStrategy(2, TimeSpan.FromSeconds(1), e => Directive.Restart);
            var boss = ActorOf(() => new Supervisor(strategy));
            var restartLatch = CreateTestLatch();
            var slaveProps = Props.Create(() => new ActorsThatClearsStashOnPreRestart(restartLatch));

            //Send the props to supervisor, which will create an actor and return the ActorRef
            var slave = boss.AskAndWait<IActorRef>(slaveProps, TestKitSettings.DefaultTimeout);

            //send messages that will be stashed
            slave.Tell("stashme 1");
            slave.Tell("stashme 2");
            slave.Tell("stashme 3");

            //this will crash the slave.
            slave.Tell("crash");

            //send a message that should not be stashed
            slave.Tell("this should bounce back");

            //During preRestart restartLatch is opened
            //After that the cell will clear the stash
            //So when the cell tries to unstash, it will not unstash messages. If it would TestActor
            //would receive all stashme messages instead of "this should bounce back"
            restartLatch.Ready(TimeSpan.FromSeconds(1110));
            ExpectMsg("this should bounce back");
        }
        /// <summary>
        /// Creates a ZipActor pool.
        /// </summary>
        /// <remarks>
        /// We use a pool to avoid a build up of messages in the <see cref="ZipActor"/>, zip being a long-running operation.
        /// </remarks>
        private void CreateZipActorPool()
        {
            SupervisorStrategy strategy = new OneForOneStrategy(
                maxNrOfRetries: 0,
                withinTimeMilliseconds: 0,
                decider: Decider.From(exception =>
                {
                    if (exception is IOException)
                    {
                        Logger.Warning("{0}{1} - {2}", LogMessageParts.SkippingFolder, Sender.Path.Name, exception.Message);

                        IncrementFolderCount();

                        return Directive.Resume;
                    }
                    else
                    {
                        Logger.Error("{0}{1} - {2}", LogMessageParts.ApplicationTerminating, Sender.Path.Name, exception.Message);

                        return Directive.Stop;
                    }
                }),
                loggingEnabled: false);

            _zipActor = Context
                .ActorOf(Props.Create<ZipActor>()
                .WithRouter((new RoundRobinPool(_numberOfFolders))
                .WithSupervisorStrategy(strategy)), "Zip");
        }
        public void A_supervisor_hierarchy_must_Restart_Manager_And_Workers_In_AllForOne()
        {
            var countDown = new CountdownEvent(4);
            SupervisorStrategy strategy = new OneForOneStrategy(_ => Directive.Restart);
            var boss = ActorOf(Props.Create(() => new Supervisor(strategy)), "boss");

            Func<Exception, Directive> decider = _ => { return Directive.Escalate; };
            var managerProps = new PropsWithName(Props.Create(() => new CountDownActor(countDown, new AllForOneStrategy(decider))), "manager");
            var manager = boss.Ask<IActorRef>(managerProps, TestKitSettings.DefaultTimeout).Result;

            var workerProps = Props.Create(() => new CountDownActor(countDown, SupervisorStrategy.DefaultStrategy));
            var worker1 = manager.Ask<IActorRef>(new PropsWithName(workerProps, "worker1"), TestKitSettings.DefaultTimeout).Result;
            var worker2 = manager.Ask<IActorRef>(new PropsWithName(workerProps, "worker2"), TestKitSettings.DefaultTimeout).Result;
            var worker3 = manager.Ask<IActorRef>(new PropsWithName(workerProps, "worker3"), TestKitSettings.DefaultTimeout).Result;

            EventFilter.Exception<ActorKilledException>().ExpectOne(() =>
            {
                worker1.Tell(Kill.Instance);
                // manager + all workers should be restarted by only killing a worker
                // manager doesn't trap exits, so boss will restart manager

                countDown.Wait(TimeSpan.FromSeconds(5)).ShouldBe(true);

            });

        }
Ejemplo n.º 12
0
        public void Routers_in_general_must_default_to_all_for_one_always_escalate_strategy()
        {
            var restarter = new OneForOneStrategy(e =>
            {
                TestActor.Tell(e);
                return Directive.Restart;
            });

            var supervisor = Sys.ActorOf(Props.Create(() => new Supervisor(restarter)));

            supervisor.Tell(new RoundRobinPool(3).Props(Props.Create(() => new RestartActor(TestActor))));

            var router = ExpectMsg<IActorRef>();
            EventFilter.Exception<ArgumentException>("die").ExpectOne(() =>
            {
                router.Tell("die");
            });
            ExpectMsg<ArgumentException>().Message.Should().Be("die");
            ExpectMsg("restarted");
            ExpectMsg("restarted");
            ExpectMsg("restarted");
        }
Ejemplo n.º 13
0
        public void Routers_in_general_must_set_supplied_supervisorStrategy_for_FromConfig()
        {
            var escalator = new OneForOneStrategy(e =>
            {
                TestActor.Tell(e);
                return Directive.Escalate;
            });

            var router = Sys.ActorOf(FromConfig.Instance.WithSupervisorStrategy(escalator).Props(Props.Create<BlackHoleActor>()), "router1");
            router.Tell(new GetRoutees());
            EventFilter.Exception<ActorKilledException>().ExpectOne(() =>
            {
                ExpectMsg<Routees>().Members.First().Send(Kill.Instance, TestActor);
            });
            ExpectMsg<ActorKilledException>();
        }
Ejemplo n.º 14
0
        public void Routers_in_general_must_set_supplied_supervisorStrategy()
        {
            var escalator = new OneForOneStrategy(e =>
            {
                TestActor.Tell(e);
                return Directive.Escalate;
            });

            var router = Sys.ActorOf(new RoundRobinPool(1, null, escalator, Dispatchers.DefaultDispatcherId).Props(Props.Create<BlackHoleActor>()));
            router.Tell(new GetRoutees());
            EventFilter.Exception<ActorKilledException>().ExpectOne(() =>
            {
                ExpectMsg<Routees>().Members.First().Send(Kill.Instance, TestActor);
            });
            ExpectMsg<ActorKilledException>();

            var router2 = Sys.ActorOf(new RoundRobinPool(1).WithSupervisorStrategy(escalator).Props(Props.Create<BlackHoleActor>()));
            router2.Tell(new GetRoutees());
            EventFilter.Exception<ActorKilledException>().ExpectOne(() =>
            {
                ExpectMsg<Routees>().Members.First().Send(Kill.Instance, TestActor);
            });

            ExpectMsg<ActorKilledException>();
        }