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));
            Assert.AreEqual(0, ticksCount);

            timer.Start();

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

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

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

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

            timer.Stop();

            scheduler.AdvanceBy(TimeSpan.FromMilliseconds(10));
            Assert.AreEqual(4, ticksCount);
        }
Beispiel #2
0
        public void DispatcherInvokeBasicTest()
        {
            TestTaskScheduler scheduler = (TestTaskScheduler)ApplicationHost.Current.TaskScheduler;

            using (scheduler.DisableImmediateProcessing())
            {
                Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

                int index = 1;
                int task1 = 0;
                int task2 = 0;
                int task3 = 0;

                dispatcher.InvokeAsync(() => task3 = index++, (DispatcherPriority)1);
                dispatcher.InvokeAsync(() => task1 = index++, (DispatcherPriority)3);
                dispatcher.Invoke(() => task2      = index++, (DispatcherPriority)2);

                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
                Assert.AreEqual(0, task3);

                scheduler.ProcessDueOperations();

                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
                Assert.AreEqual(3, task3);
            }
        }
Beispiel #3
0
        public void DispatcherDisableProcessingTest()
        {
            TestTaskScheduler scheduler = (TestTaskScheduler)ApplicationHost.Current.TaskScheduler;

            using (scheduler.DisableImmediateProcessing())
            {
                Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

                int         index = 1;
                int         task1 = 0;
                int         task2 = 0;
                IDisposable dispatcherProcessingDisabled = null;

                dispatcher.InvokeAsync(() =>
                {
                    task1 = index++;
                    dispatcherProcessingDisabled = dispatcher.DisableProcessing();
                });

                dispatcher.InvokeAsync(() => task2 = index++);

                Assert.AreEqual(0, task1);
                Assert.AreEqual(0, task2);
                Assert.AreEqual(null, dispatcherProcessingDisabled);

                scheduler.ProcessDueOperations();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(0, task2);
                Assert.AreNotEqual(null, dispatcherProcessingDisabled);

                dispatcherProcessingDisabled.Dispose();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(0, task2);

                scheduler.ProcessDueOperations();
                Assert.AreEqual(1, task1);
                Assert.AreEqual(2, task2);
            }
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
        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);
        }