Inheritance: SchedulerBase, IDateTimeOffsetNowTimeProvider
コード例 #1
0
 public void Now_Should_be_accurate()
 {
     using (var sys = ActorSystem.Create("Foo"))
     {
         ITimeProvider timeProvider = new DedicatedThreadScheduler(sys);
         Math.Abs((timeProvider.Now - DateTimeOffset.Now).TotalMilliseconds).ShouldBeLessThan(20);
     }
 }
        public void When_ScheduleOnce_using_canceled_Cancelable_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var canceled = Cancelable.CreateCanceled();
            scheduler.ScheduleOnce(0, () => TestActor.Tell("Test"), canceled);

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

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleOnce(100, () => TestActor.Tell("Test"), cancelable);
            cancelable.Cancel();

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        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 ScheduleRepeatedly_in_TimeSpan_Tests(int initialDelay, int interval)
        {
            // Prepare, set up actions to be fired
            IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);

            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_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_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 ScheduleTellOnceTests(int[] times)
        {
            // Prepare, set up messages to be sent
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            foreach(var time in times)
            {
                scheduler.ScheduleTellOnce(time, TestActor, "Test" + time, ActorRefs.NoSender);
            }

            ExpectMsg("Test1");
            ExpectMsg("Test50");
            ExpectMsg("Test110");

            ExpectNoMsg(50);
        }
        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 DedicatedThreadScheduler(Sys.Settings.Config, Log);

            try
            {
                var cancelable = new Cancelable(scheduler);
                scheduler.ScheduleRepeatedly(100, 2, () => TestActor.Tell("Test"), cancelable);
                cancelable.Cancel();

                //Validate that no messages were sent
                ExpectNoMsg(150);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
        }
        public void When_ScheduleRepeatedly_using_canceled_Cancelable_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new DedicatedThreadScheduler(Sys.Settings.Config, Log);

            try
            {
                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);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
        }
        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_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);
        }
        public void When_ScheduleRepeatedly_with_invalid_interval_Then_exception_is_thrown(int invalidInterval)
        {
            IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);

            XAssert.Throws<ArgumentOutOfRangeException>(() =>
                testScheduler.ScheduleRepeatedly(42, invalidInterval, () => { })
                );
        }
        public void When_ScheduleOnce_many_at_the_same_time_Then_all_fires(int[] times)
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new DedicatedThreadScheduler(Sys);

            foreach(var time in times)
            {
                var t = time;
                scheduler.ScheduleOnce(time, () => TestActor.Tell("Test" + t));
            }

            //Perform the test
            ExpectMsg("Test1");
            ExpectMsg("Test1");
            ExpectMsg("Test50");
            ExpectMsg("Test50");
            ExpectMsg("Test100");
            ExpectMsg("Test100");
            ExpectNoMsg(50);
        }
        public void ScheduleOnceTests()
        {
            // Prepare, set up actions to be fired
            IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);

            testScheduler.ScheduleOnce(50, () => TestActor.Tell("Test1"));
            testScheduler.ScheduleOnce(100, () => TestActor.Tell("Test2"));

            ExpectMsg("Test1");
            ExpectMsg("Test2");

            ExpectNoMsg(100);
        }
 public void When_ScheduleRepeatedly_action_crashes_Then_no_more_calls_will_be_scheduled()
 {
     IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);
     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_with_0_delay_Then_action_is_executed_immediately()
        {
            IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);
            var manualResetEvent = new ManualResetEventSlim();
            manualResetEvent.IsSet.ShouldBeFalse();
            testScheduler.ScheduleRepeatedly(0, 100, () => manualResetEvent.Set());

            manualResetEvent.Wait(500).ShouldBeTrue();
        }
        public void When_ScheduleTellOnce_with_invalid_delay_Then_exception_is_thrown(int invalidTime)
        {
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            XAssert.Throws<ArgumentOutOfRangeException>(() =>
                scheduler.ScheduleTellOnce(invalidTime, TestActor, "Test", ActorRefs.NoSender)
                );
            ExpectNoMsg(50);
        }
        public void When_ScheduleTellOnce_many_at_the_same_time_Then_all_fires(int[] times)
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            foreach(var time in times)
            {
                scheduler.ScheduleTellOnce(time, TestActor, "Test" + time, ActorRefs.NoSender);
            }

            //Perform the test
            ExpectMsg("Test1");
            ExpectMsg("Test1");
            ExpectMsg("Test50");
            ExpectMsg("Test50");
            ExpectMsg("Test100");
            ExpectMsg("Test100");
            ExpectNoMsg(50);
        }
        public void When_ScheduleOnce_with_invalid_delay_Then_exception_is_thrown(int invalidTime)
        {
            IActionScheduler testScheduler = new DedicatedThreadScheduler(Sys);

            XAssert.Throws<ArgumentOutOfRangeException>(() =>
                testScheduler.ScheduleOnce(invalidTime, () => { })
                );
        }
        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_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 DedicatedThreadScheduler(Sys.Settings.Config, Log);

            try
            {
                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);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
        }