public void When_ScheduleRepeatedly_using_canceled_Cancelable_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new TaskBasedScheduler();

            var canceled = Cancelable.CreateCanceled();

            scheduler.ScheduleRepeatedly(0, 100, () => TestActor.Tell("Test1"), canceled);
            scheduler.ScheduleRepeatedly(50, 100, () => TestActor.Tell("Test2"), canceled);

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        public void When_ScheduleRepeatedly_with_invalid_interval_Then_exception_is_thrown(int invalidInterval)
        {
            IActionScheduler testScheduler = new TaskBasedScheduler();

            XAssert.Throws <ArgumentOutOfRangeException>(() =>
                                                         testScheduler.ScheduleRepeatedly(42, invalidInterval, () => { })
                                                         );
        }
        public void When_ScheduleRepeatedly_with_0_delay_Then_action_is_executed_immediately()
        {
            IActionScheduler testScheduler = new TaskBasedScheduler();
            var manualResetEvent           = new ManualResetEventSlim();

            manualResetEvent.IsSet.ShouldBeFalse();
            testScheduler.ScheduleRepeatedly(0, 100, () => manualResetEvent.Set());

            manualResetEvent.Wait(500).ShouldBeTrue();
        }
        public void ScheduleRepeatedly_in_TimeSpan_Tests(int initialDelay, int interval)
        {
            // Prepare, set up actions to be fired
            IActionScheduler testScheduler = new TaskBasedScheduler();

            testScheduler.ScheduleRepeatedly(TimeSpan.FromMilliseconds(initialDelay), TimeSpan.FromMilliseconds(interval), () => TestActor.Tell("Test"));

            //Just check that we receives more than one message
            ExpectMsg("Test");
            ExpectMsg("Test");
            ExpectMsg("Test");
        }
        public void When_ScheduleRepeatedly_action_crashes_Then_no_more_calls_will_be_scheduled()
        {
            IActionScheduler testScheduler = new TaskBasedScheduler();
            var timesCalled = 0;

            testScheduler.ScheduleRepeatedly(0, 10, () => { Interlocked.Increment(ref timesCalled); throw new Exception("Crash"); });
            AwaitCondition(() => timesCalled >= 1);
            Thread.Sleep(200);  //Allow any scheduled actions to be fired.

            //We expect only one of the scheduled actions to actually fire
            timesCalled.ShouldBe(1);
        }
        public void When_ScheduleRepeatedly_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new TaskBasedScheduler();

            var cancelable = new Cancelable(scheduler);

            scheduler.ScheduleRepeatedly(100, 2, () => TestActor.Tell("Test"), cancelable);
            cancelable.Cancel();

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        public void When_canceling_existing_running_repeaters_Then_their_future_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new TaskBasedScheduler();

            var cancelable = new Cancelable(scheduler);

            scheduler.ScheduleRepeatedly(0, 150, () => TestActor.Tell("Test"), cancelable);
            ExpectMsg("Test");
            cancelable.Cancel();

            //Validate that no more messages were sent
            ExpectNoMsg(200);
        }
        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
            IActionScheduler scheduler = new TaskBasedScheduler();

            var cancelableOdd = new Cancelable(scheduler);

            scheduler.ScheduleRepeatedly(1, 150, () => TestActor.Tell("Test"), cancelableOdd);
            cancelableOdd.CancelAfter(50);

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

            //Validate that no messages were sent
            ExpectNoMsg(200);
        }
        public void ScheduleRepeatedly_in_milliseconds_Tests_and_verify_the_interval(int initialDelay, int interval)
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new TaskBasedScheduler();

            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.ScheduleRepeatedly(initialDelay, interval, () => receiver.Tell(""), 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);
        }