public ListenerActor(TestLatch fooLatch, TestLatch barLatch, AtomicCounter barCount)
 {
     _fooLatch = fooLatch;
     _barLatch = barLatch;
     _barCount = barCount;
     Listeners = new ListenerSupport();
 }
            public Watcher(TestLatch exitingLatch, TestLatch removedLatch, Address secondAddress)
            {
                _exitingLatch = exitingLatch;
                _removedLatch = removedLatch;
                _secondAddress = secondAddress;

                Receive<ClusterEvent.CurrentClusterState>(state =>
                {
                    if (state.Members.Any(m => m.Address == _secondAddress && m.Status == MemberStatus.Exiting))
                        _exitingLatch.CountDown();
                });
                Receive<ClusterEvent.MemberExited>(m =>
                {
                    if (m.Member.Address == secondAddress)
                    {
                        exitingLatch.CountDown();
                    }
                });
                Receive<ClusterEvent.MemberRemoved>(m =>
                {
                    if (m.Member.Address == secondAddress)
                    {
                        _removedLatch.CountDown();
                    }
                });
            }
Exemple #3
0
            public RoundRobinPoolBroadcastActor(TestLatch helloLatch, TestLatch stopLatch)
            {
                _helloLatch = helloLatch;
                _stopLatch = stopLatch;

                Receive<string>(s => s == "hello", c => _helloLatch.CountDown());
            }
        public void Smallest_mailbox_pool_must_deliver_messages_to_idle_actor()
        {
            var usedActors = new ConcurrentDictionary<int, string>();
            var router = Sys.ActorOf(new SmallestMailboxPool(3).Props(Props.Create(() => new SmallestMailboxActor(usedActors))));

            var busy = new TestLatch(1);
            var received0 = new TestLatch(1);
            router.Tell(Tuple.Create(busy, received0));
            received0.Ready(TestKitSettings.DefaultTimeout);

            var received1 = new TestLatch(1);
            router.Tell(Tuple.Create(1, received1));
            received1.Ready(TestKitSettings.DefaultTimeout);

            var received2 = new TestLatch(1);
            router.Tell(Tuple.Create(2, received2));
            received2.Ready(TestKitSettings.DefaultTimeout);

            var received3 = new TestLatch(1);
            router.Tell(Tuple.Create(3, received3));
            received3.Ready(TestKitSettings.DefaultTimeout);

            busy.CountDown();

            var busyPath = usedActors[0];
            busyPath.Should().NotBeNull();

            var path1 = usedActors[1];
            var path2 = usedActors[2];
            var path3 = usedActors[3];

            path1.Should().NotBeNull(busyPath);
            path2.Should().NotBeNull(busyPath);
            path3.Should().NotBeNull(busyPath);
        }
        public void Listener_must_listen_in()
        {
            //arrange
            var fooLatch = new TestLatch(2);
            var barLatch = new TestLatch(2);
            var barCount = new AtomicCounter(0);

            var broadcast = Sys.ActorOf<BroadcastActor>();
            var newListenerProps = Props.Create(() => new ListenerActor(fooLatch, barLatch, barCount));
            var a1 = Sys.ActorOf(newListenerProps);
            var a2 = Sys.ActorOf(newListenerProps);
            var a3 = Sys.ActorOf(newListenerProps);

            //act
            broadcast.Tell(new Listen(a1));
            broadcast.Tell(new Listen(a2));
            broadcast.Tell(new Listen(a3));

            broadcast.Tell(new Deafen(a3));

            broadcast.Tell(new WithListeners(a => a.Tell("foo")));
            broadcast.Tell("foo");

            //assert
            barLatch.Ready(TestLatch.DefaultTimeout);
            Assert.Equal(2, barCount.Current);

            fooLatch.Ready(TestLatch.DefaultTimeout);
            foreach (var actor in new[] {a1, a2, a3, broadcast})
            {
                Sys.Stop(actor);
            }
        }
        public void GetTimeout()
        {
            var timeoutLatch = new TestLatch();
            var timeoutActor = Sys.ActorOf(Props.Create(() => new TimeoutActor(timeoutLatch)));

            timeoutLatch.Ready(TestLatch.DefaultTimeout);
            Sys.Stop(timeoutActor);
        }
Exemple #7
0
            public RoundRobinPoolActor(TestLatch doneLatch, AtomicCounter counter)
            {
                _doneLatch = doneLatch;
                _counter = counter;

                Receive<string>(s => s == "hit", c => Sender.Tell(id.Value));
                Receive<string>(s => s == "end", c => _doneLatch.CountDown());
            }
            public BroadcastTarget(TestLatch doneLatch, AtomicCounter counter)
            {
                _doneLatch = doneLatch;
                _counter = counter;

                Receive<string>(s => s == "end", c => _doneLatch.CountDown());
                Receive<int>(msg => _counter.AddAndGet(msg));
            }
Exemple #9
0
 public void Props_must_create_actor_by_producer()
 {
     TestLatch latchProducer = new TestLatch(Sys);
     TestLatch latchActor = new TestLatch(Sys);
     var props = Props.CreateBy<TestProducer>(latchProducer, latchActor);
     ActorRef actor = Sys.ActorOf(props);
     latchActor.Ready(TimeSpan.FromSeconds(1));
 }
 public void RescheduleTimeout()
 {
     var timeoutLatch = new TestLatch(Sys);
     var timeoutActor = Sys.ActorOf(Props.Create(() => new TimeoutActor(timeoutLatch)));
     timeoutActor.Tell(Tick);
     timeoutLatch.Ready(TestLatch.DefaultTimeout);
     Sys.Stop(timeoutActor);
 }
Exemple #11
0
        public async Task Scatter_gather_router_must_return_response_even_if_one_of_the_actors_has_stopped()
        {
            var shutdownLatch = new TestLatch(1);
            var actor1 = Sys.ActorOf(Props.Create(() => new StopActor(1)));
            var actor2 = Sys.ActorOf(Props.Create(() => new StopActor(14)));
            var paths = new []{actor1,actor2};
            var routedActor = Sys.ActorOf(new ScatterGatherFirstCompletedGroup(paths, TimeSpan.FromSeconds(3)).Props());

            routedActor.Tell(new Broadcast(new Stop(1)));
            shutdownLatch.Open();

            var res = await routedActor.Ask<int>(0, TimeSpan.FromSeconds(10));
            res.ShouldBe(14);
        }
        public void BroadcastGroup_router_must_broadcast_message_using_Ask()
        {
            var doneLatch = new TestLatch(2);
            var counter1 = new AtomicCounter(0);
            var counter2 = new AtomicCounter(0);
            var actor1 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter1)));
            var actor2 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter2)));

            var routedActor = Sys.ActorOf(Props.Create<TestActor>().WithRouter(new BroadcastGroup(actor1.Path.ToString(), actor2.Path.ToString())));
            routedActor.Ask(new Broadcast(1));
            routedActor.Tell(new Broadcast("end"));

            doneLatch.Ready(TimeSpan.FromSeconds(1));

            counter1.Current.ShouldBe(1);
            counter2.Current.ShouldBe(1);
        }
            public Listener(TestLatch latch, ImmutableList<Address> expectedAddresses)
            {
                _latch = latch;
                _expectedAddresses = expectedAddresses;

                Receive<ClusterEvent.CurrentClusterState>(state =>
                {
                    _members = state.Members;
                });

                Receive<ClusterEvent.MemberUp>(m =>
                {
                    _members = _members.Remove(m.Member).Add(m.Member);

                    if (!_members.Select(c => c.Address).Except(_expectedAddresses).Any())
                        _latch.CountDown();
                });
            }
        public void Scatter_gather_router_must_deliver_a_broadcast_message_using_tell()
        {
            var doneLatch = new TestLatch(sys, 2);
            var counter1 = new AtomicInteger(0);
            var counter2 = new AtomicInteger(0);
            var actor1 = sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter1)));
            var actor2 = sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter2)));

            var routedActor = sys.ActorOf(Props.Create<TestActor>().WithRouter(new ScatterGatherFirstCompletedGroup(TimeSpan.FromSeconds(1), actor1.Path.ToString(), actor2.Path.ToString())));
            routedActor.Tell(new Broadcast(1));
            routedActor.Tell(new Broadcast("end"));

            doneLatch.Ready(TimeSpan.FromSeconds(1));

            counter1.Value.ShouldBe(1);
            counter2.Value.ShouldBe(1);

        }
Exemple #15
0
        public void GracefulStop_must_complete_Task_with_TaskCanceledException_when_actor_not_terminated_within_timeout()
        {
            //arrange
            var target = Sys.ActorOf<TargetActor>();
            var latch = new TestLatch();

            //act
            target.Tell(Tuple.Create(latch, TimeSpan.FromSeconds(2)));

            //assert
            XAssert.Throws<TaskCanceledException>(() =>
            {
                var task = target.GracefulStop(TimeSpan.FromMilliseconds(500));
                task.Wait();
                var result = task.Result;
            });
            latch.Open();

        }
Exemple #16
0
        public void BroadcastGroup_router_must_broadcast_message_using_Ask()
        {
            var doneLatch = new TestLatch(2);

            var counter1 = new AtomicCounter(0);
            var actor1 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter1)));

            var counter2 = new AtomicCounter(0);
            var actor2 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter2)));

            var paths = new List<string> { actor1.Path.ToString(), actor2.Path.ToString() };
            var routedActor = Sys.ActorOf(new BroadcastGroup(paths).Props());
            routedActor.Ask(new Broadcast(1));
            routedActor.Tell(new Broadcast("end"));

            doneLatch.Ready(RemainingOrDefault);

            counter1.Current.Should().Be(1);
            counter2.Current.Should().Be(1);
        }
Exemple #17
0
        public void Random_must_be_able_to_shut_down_its_instance()
        {
            const int routeeCount = 7;
            var testLatch = new TestLatch(Sys, routeeCount);
            var router = Sys.ActorOf(new RandomPool(routeeCount).Props(Props.Create(() => new HelloWorldActor(testLatch))));
            router.Tell("hello", TestActor);
            router.Tell("hello", TestActor);
            router.Tell("hello", TestActor);
            router.Tell("hello", TestActor);
            router.Tell("hello", TestActor);

            Within(TimeSpan.FromSeconds(2), () => {
                ExpectMsg("world");
                ExpectMsg("world");
                ExpectMsg("world");
                ExpectMsg("world");
                ExpectMsg("world");
                return true;
            });
            
            Sys.Stop(router);
            testLatch.Ready(TimeSpan.FromSeconds(5));
        }
            public StopActor(int id, TestLatch shutdownLatch)
            {
                _id = id;
                _shutdownLatch = shutdownLatch;

                Receive<Stop>(s =>
                {
                    if (s.Id == null || s.Id == _id)
                    {
                        Context.Stop(Self);
                    }
                });

                Receive<int>(n => n == _id, _ =>
                {

                });

                ReceiveAny(x =>
                {
                    Thread.Sleep(100 * _id);
                    Sender.Tell(_id);
                });
            }
Exemple #19
0
        public void Random_pool_must_be_able_to_shut_down_its_instance()
        {
            const int routeeCount = 7;
            var testLatch = new TestLatch(routeeCount);

            var actor = Sys.ActorOf(new RandomPool(routeeCount)
                .Props(Props.Create(() => new HelloWorldActor(testLatch))), "random-shutdown");

            actor.Tell("hello");
            actor.Tell("hello");
            actor.Tell("hello");
            actor.Tell("hello");
            actor.Tell("hello");

            Within(TimeSpan.FromSeconds(2), () => {
                for (int i = 1; i <= 5; i++)
                {
                    ExpectMsg("world");
                }
            });

            Sys.Stop(actor);
            testLatch.Ready(5.Seconds());
        }
        public void Smallest_mailbox_router_must_deliver_messages_to_idle_actor()
        {
            var usedActors = new ConcurrentDictionary<int, string>();
            var router = Sys.ActorOf(new SmallestMailboxPool(3).Props(Props.Create(() => new SmallestMailboxActor(usedActors))));

            var busy = new TestLatch(1);
            var received0 = new TestLatch(1);
            router.Tell(Tuple.Create(busy, received0));
            received0.Ready(TestLatch.DefaultTimeout);

            var received1 = new TestLatch(1);
            router.Tell(Tuple.Create(1, received1));
            received1.Ready(TestLatch.DefaultTimeout);

            var received2 = new TestLatch(1);
            router.Tell(Tuple.Create(2, received2));
            received2.Ready(TestLatch.DefaultTimeout);

            var received3 = new TestLatch(1);
            router.Tell(Tuple.Create(3, received3));
            received3.Ready(TestLatch.DefaultTimeout);

            busy.CountDown();

            var busyPath = usedActors[0];
            Assert.NotEqual(busyPath, null);

            Assert.Equal(usedActors.Count, 4);
            var path1 = usedActors[1];
            var path2 = usedActors[2];
            var path3 = usedActors[3];

            Assert.NotEqual(path1, busyPath);
            Assert.NotEqual(path2, busyPath);
            Assert.NotEqual(path3, busyPath);
        }
 public BroadcastTarget(TestLatch latch, AtomicCounter counter)
 {
     _latch = latch;
     _counter = counter;
 }
        public void Tail_chopping_router_must_deliver_a_broadcast_message_using_tell()
        {
            var doneLatch = new TestLatch(2);
            var counter1 = new AtomicCounter(0);
            var counter2 = new AtomicCounter(0);

            var actor1 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter1)), "Actor1");
            var actor2 = Sys.ActorOf(Props.Create(() => new BroadcastTarget(doneLatch, counter2)), "Actor2");

            var routedActor = Sys.ActorOf(Props.Create<TestActor>()
                .WithRouter(new TailChoppingGroup(new[] { actor1.Path.ToString(), actor2.Path.ToString() }, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(100))
            ));

            routedActor.Tell(new Broadcast(1));
            routedActor.Tell(new Broadcast("end"));

            doneLatch.Ready(TimeSpan.FromSeconds(1));

            counter1.Current.ShouldBe(1);
            counter2.Current.ShouldBe(1);
        }
Exemple #23
0
 public TestResizer(TestLatch latch)
 {
     _latch = latch;
 }
Exemple #24
0
        public void Router_in_general_must_use_specified_resizer_when_resizer_not_configured()
        {
            var latch = new TestLatch(1);
            var resizer = new TestResizer(latch);
            var router =
                Sys.ActorOf(
                    Props.Create<BlackHoleActor>()
                        .WithRouter(new RoundRobinPool(0, resizer, SupervisorStrategy.DefaultStrategy, null)));
            latch.Open();
            router.Tell(new GetRoutees(),TestActor);
            ExpectMsg<Routees>().Members.Count().ShouldBe(2);
            Sys.Stop(router);

        }             
Exemple #25
0
        public void Router_in_general_must_not_terminate_when_resizer_is_used()
        {
            var latch = new TestLatch(1);
            var resizer = new TestResizer(latch);
            var router =
                Sys.ActorOf(new RoundRobinPool( 0, resizer,SupervisorStrategy.DefaultStrategy,null).Props(Props.Create<BlackHoleActor>()));

            Watch(router);

            latch.Open();
            //Await.ready(latch, remainingOrDefault); //TODO: what is remainingOrDefault
            
            router.Tell(new GetRoutees(),TestActor);
            var routees = ExpectMsg<Routees>().Members.ToList();

            routees.Count().ShouldBe(2);
            routees.ForEach(r => r.Send(PoisonPill.Instance,TestActor));
            // expect no Terminated
            ExpectNoMsg(TimeSpan.FromSeconds(2));
        }
Exemple #26
0
 public SenderActor(ActorRef replyTo, TestLatch latch)
 {
     _latch = latch;
     _replyTo = replyTo;
 }
Exemple #27
0
        public void An_ActorRef_should_suppport_reply_via_Sender()
        {
            var latch = new TestLatch(Sys, 4);
            var serverRef = Sys.ActorOf(Props.Create<ReplyActor>());
            var clientRef = Sys.ActorOf(Props.Create(() => new SenderActor(serverRef, latch)));

            clientRef.Tell("complex");
            clientRef.Tell("simple");
            clientRef.Tell("simple");
            clientRef.Tell("simple");

            latch.Ready(TimeSpan.FromSeconds(3));
            latch.Reset();

            clientRef.Tell("complex2");
            clientRef.Tell("simple");
            clientRef.Tell("simple");
            clientRef.Tell("simple");

            latch.Ready(TimeSpan.FromSeconds(3));
            Sys.Stop(clientRef);
            Sys.Stop(serverRef);
        }
 public LatchCmd(TestLatch latch, object data)
 {
     Latch = latch;
     Data = data;
 }
Exemple #29
0
 public TestProducer(TestLatch lp, TestLatch la)
 {
     latchActor = la;
     lp.Reset();
     lp.CountDown();
 }
 public HelloWorldActor(TestLatch testLatch)
 {
     _testLatch = testLatch;
 }