Esempio n. 1
0
        public void A_dispatcher_must_handle_waves_of_actors()
        {
            var dispatcher = InterceptedDispatcher();
            var props      = Props.Create(() => new DispatcherActor()).WithDispatcher(dispatcher.Id);

            Action <int> flood = num =>
            {
                var cachedMessage            = new CountDownNStop(new CountdownEvent(num));
                var stopLatch                = new CountdownEvent(num);
                var keepAliveLatch           = new CountdownEvent(1);
                var waitTime                 = (int)Dilated(TimeSpan.FromSeconds(20)).TotalMilliseconds;
                Action <IActorDsl> bossActor = c =>
                {
                    c.Receive <string>(str => str.Equals("run"), (s, context) =>
                    {
                        for (var i = 1; i <= num; i++)
                        {
                            context.Watch(context.ActorOf(props)).Tell(cachedMessage);
                        }
                    });

                    c.Receive <Terminated>((terminated, context) =>
                    {
                        stopLatch.Signal();
                    });
                };
                var boss = Sys.ActorOf(Props.Create(() => new Act(bossActor)).WithDispatcher("boss"));

                try
                {
                    // this future is meant to keep the dispatcher alive until the end of the test run even if
                    // the boss doesn't create children fast enough to keep the dispatcher from becoming empty
                    // and it needs to be on a separate thread to not deadlock the calling thread dispatcher
                    dispatcher.Schedule(() =>
                    {
                        keepAliveLatch.Wait(waitTime);
                    });
                    boss.Tell("run");
                    try
                    {
                        AssertCountdown(cachedMessage.Latch, waitTime, "Counting down from " + num);
                    }
                    catch (Exception ex)
                    {
                        // TODO balancing dispatcher
                        throw;
                    }
                    AssertCountdown(stopLatch, waitTime, "Expected all children to stop.");
                }
                finally
                {
                    keepAliveLatch.Signal();
                    Sys.Stop(boss);
                }
            };

            for (var i = 1; i <= 3; i++)
            {
                flood(50000);
                AssertDispatcher(dispatcher, i);
            }
        }
Esempio n. 2
0
        public void A_dispatcher_must_handle_waves_of_actors()
        {
            var dispatcher = InterceptedDispatcher();
            var props = Props.Create(() => new DispatcherActor()).WithDispatcher(dispatcher.Id);

            Action<int> flood = num =>
            {
                var cachedMessage = new CountDownNStop(new CountdownEvent(num));
                var stopLatch = new CountdownEvent(num);
                var keepAliveLatch = new CountdownEvent(1);
                var waitTime = (int)Dilated(TimeSpan.FromSeconds(20)).TotalMilliseconds;
                Action<IActorDsl> bossActor = c =>
                {
                    c.Receive<string>(str => str.Equals("run"), (s, context) =>
                    {
                        for (var i = 1; i <= num; i++)
                        {
                            context.Watch(context.ActorOf(props)).Tell(cachedMessage);
                        }
                    });

                    c.Receive<Terminated>((terminated, context) =>
                    {
                        stopLatch.Signal();
                    });
                };
                var boss = Sys.ActorOf(Props.Create(() => new Act(bossActor)).WithDispatcher("boss"));

                try
                {
                    // this future is meant to keep the dispatcher alive until the end of the test run even if
                    // the boss doesn't create children fast enough to keep the dispatcher from becoming empty
                    // and it needs to be on a separate thread to not deadlock the calling thread dispatcher
                    dispatcher.Schedule(() =>
                    {
                        keepAliveLatch.Wait(waitTime);
                    });
                    boss.Tell("run");
                    try
                    {
                        AssertCountdown(cachedMessage.Latch, waitTime, "Counting down from " + num);
                    }
                    catch (Exception ex)
                    {
                        // TODO balancing dispatcher
                        throw;
                    }
                    AssertCountdown(stopLatch, waitTime, "Expected all children to stop.");
                }
                finally
                {
                    keepAliveLatch.Signal();
                    Sys.Stop(boss);
                }
            };

            for (var i = 1; i <= 3; i++)
            {
                flood(50000);
                AssertDispatcher(dispatcher, i);
            }
        }