Example #1
0
        public void SchedulingTest()
        {
            int dueOperationsCount = 0;

            TestTaskScheduler scheduler = new TestTaskScheduler();

            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(1), () => dueOperationsCount++);
            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(2), () => dueOperationsCount++);
            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(2), () => dueOperationsCount++);
            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(4), () => dueOperationsCount++);
            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(4), () => dueOperationsCount++);
            scheduler.ScheduleTask(TimeSpan.FromMilliseconds(5), () => dueOperationsCount++);

            scheduler.AdvanceTo(TimeSpan.FromMilliseconds(1));
            Assert.AreEqual(1, dueOperationsCount);

            scheduler.AdvanceTo(TimeSpan.FromMilliseconds(3));
            Assert.AreEqual(3, dueOperationsCount);

            scheduler.AdvanceTo(TimeSpan.FromMilliseconds(6));
            Assert.AreEqual(6, dueOperationsCount);
        }
Example #2
0
        public void DispatcherTimerStartStopTest()
        {
            TestTaskScheduler scheduler = new TestTaskScheduler();
            DispatcherTimer timer = new DispatcherTimer(Dispatcher.CurrentDispatcher, scheduler, TimeSpan.FromMilliseconds(10), DispatcherPriority.Normal);

            int ticksCount = 0;

            timer.Tick += (sender, e) => ticksCount++;

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(10));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(0, ticksCount);

            timer.Start();

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(10));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(1, ticksCount);

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(5));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(1, ticksCount);

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(5));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(2, ticksCount);

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(20));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(4, ticksCount);

            timer.Stop();

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(10));
            Dispatcher.CurrentDispatcher.ProcessQueue();
            Assert.AreEqual(4, ticksCount);
        }