public void When_ScheduleTellRepeatedly_using_canceled_Cancelable_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            ITellScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var canceled = Cancelable.CreateCanceled();
            scheduler.ScheduleTellRepeatedly(0, 2, TestActor, "Test", ActorRefs.NoSender, canceled);
            scheduler.ScheduleTellRepeatedly(1, 2, TestActor, "Test", ActorRefs.NoSender, canceled);

            //Validate that no messages were sent
            ExpectNoMsg(100);
        }
        public void When_ScheduleTellRepeatedly_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleTellRepeatedly(100, 2, TestActor, "Test", ActorRefs.NoSender, cancelable);
            cancelable.Cancel();

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        public void ScheduleTellRepeatedly_TimeSpan_Tests(int initialDelay, int interval)
        {
            //Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            scheduler.ScheduleTellRepeatedly(TimeSpan.FromMilliseconds(initialDelay), TimeSpan.FromMilliseconds(interval), TestActor, "Test", ActorRefs.NoSender);

            //Just check that we receives more than one message
            ExpectMsg("Test");
            ExpectMsg("Test");
            ExpectMsg("Test");
        }
        public void When_canceling_existing_running_repeaters_Then_their_future_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleTellRepeatedly(0, 150, TestActor, "Test", ActorRefs.NoSender, cancelable);
            ExpectMsg("Test");
            cancelable.Cancel();

            //Validate that no more messages were sent
            ExpectNoMsg(200);
        }
        public void ScheduleTellRepeatedly_in_milliseconds_Tests(int initialDelay, int interval)
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(Sys.Scheduler);
            var receiver = ActorOf(dsl =>
            {
                //Receive three messages, and store the time when these were received
                //after three messages stop the actor and send the times to TestActor
                var messages = new List<DateTimeOffset>();
                dsl.Receive<string>((s, context) =>
                {
                    messages.Add(context.System.Scheduler.Now);
                    if(messages.Count == 3)
                    {
                        TestActor.Tell(messages);
                        cancelable.Cancel();
                        context.Stop(context.Self);
                    }
                });
            });
            scheduler.ScheduleTellRepeatedly(initialDelay, interval, receiver, "Test", ActorRefs.NoSender, cancelable);

            //Expect to get a list from receiver after it has received three messages
            var dateTimeOffsets = ExpectMsg<List<DateTimeOffset>>();
            dateTimeOffsets.ShouldHaveCount(3);
            Action<int, int> validate = (a, b) =>
            {
                var valA = dateTimeOffsets[a];
                var valB = dateTimeOffsets[b];
                var diffBetweenMessages = Math.Abs((valB - valA).TotalMilliseconds);
                var diffInMs = Math.Abs(diffBetweenMessages - interval);
                var deviate = (diffInMs / interval);
                deviate.Should(val => val < 0.1, string.Format("Expected the interval between message {1} and {2} to deviate maximum 10% from {0}. It was {3} ms between the messages. It deviated {4}%", interval, a + 1, b + 1, diffBetweenMessages, deviate * 100));
            };
            validate(0, 1);
            validate(1, 2);
        }
        public void When_canceling_existing_running_repeaters_by_scheduling_the_cancellation_ahead_of_time_Then_their_future_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelableOdd = new Cancelable(scheduler);
            scheduler.ScheduleTellRepeatedly(1, 150, TestActor, "Test", ActorRefs.NoSender, cancelableOdd);
            cancelableOdd.CancelAfter(50);

            //Expect one message
            ExpectMsg("Test");

            //Validate that no messages were sent
            ExpectNoMsg(200);
        }
 public void When_ScheduleTellRepeatedly_with_0_delay_Then_action_is_executed_immediately()
 {
     IScheduler scheduler = new DedicatedThreadScheduler(Sys);
     scheduler.ScheduleTellRepeatedly(0, 60 * 1000, TestActor, "Test", ActorRefs.NoSender);
     ExpectMsg("Test");
 }
        public void When_ScheduleTellRepeatedly_with_invalid_interval_Then_exception_is_thrown(int invalidInterval)
        {
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            XAssert.Throws<ArgumentOutOfRangeException>(() =>
                scheduler.ScheduleTellRepeatedly(42, invalidInterval, TestActor, "Test", ActorRefs.NoSender)
                );
            ExpectNoMsg(50);
        }