public void Test_GetScheduledTasks()
        {
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            PrivateObject privateScheduler       = new PrivateObject(scheduler, new PrivateType(typeof(MultiThreadedTaskScheduler)));
            object        tasks = privateScheduler.Invoke("GetScheduledTasks", TestUtils.PrivateInstanceFlags);

            Validate.Value.IsNotNull(tasks, "Could not get scheduled tasks.");
        }
        public void Test_InlineNullTask()
        {
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            PrivateObject privateScheduler       = new PrivateObject(scheduler, new PrivateType(typeof(MultiThreadedTaskScheduler)));

            TestUtils.TestForError <ArgumentNullException>(
                () => privateScheduler.Invoke("TryExecuteTaskInline", TestUtils.PrivateInstanceFlags, (Task)null, false),
                "Should not be able to inline execute null tasks.");
        }
        public void Test_QueueTask()
        {
            bool taskRan = false;
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            PrivateObject privateScheduler       = new PrivateObject(scheduler, new PrivateType(typeof(MultiThreadedTaskScheduler)));

            privateScheduler.Invoke("QueueTask", TestUtils.PrivateInstanceFlags, new Task(() => taskRan = true));

            SpinWait.SpinUntil(() => taskRan && scheduler.NumThreadsRunning == 0, TimeSpan.FromSeconds(3));
        }
        public void Test_InlineWrongThread()
        {
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            PrivateObject privateScheduler       = new PrivateObject(scheduler, new PrivateType(typeof(MultiThreadedTaskScheduler)));
            bool          executedTask           = (bool)privateScheduler.Invoke(
                "TryExecuteTaskInline",
                TestUtils.PrivateInstanceFlags,
                new Task(MultiThreadedTaskSchedulerTest.TestTask),
                false);

            Validate.Value.IsFalse(executedTask, "Should not be able to inline execute tasks from non running thread.");
        }
        public void Test_TwoTooManyTasks()
        {
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            TaskFactory factory = new TaskFactory(scheduler);

            for (int i = 0; i < MultiThreadedTaskSchedulerTest.DefaultNumThreads + 2; i++)
            {
                factory.StartNew(MultiThreadedTaskSchedulerTest.TestTask);
            }

            Validate.Value.AreEqual(scheduler.NumThreadsRunning, MultiThreadedTaskSchedulerTest.DefaultNumThreads, "Incorrent number of threads running");
        }
        public void Test_InlinePreviouslyQueued()
        {
            MultiThreadedTaskScheduler scheduler = new MultiThreadedTaskScheduler(MultiThreadedTaskSchedulerTest.DefaultNumThreads);
            PrivateObject privateScheduler       = new PrivateObject(scheduler, new PrivateType(typeof(MultiThreadedTaskScheduler)));

            privateScheduler.SetField("currentThreadIsProcessingItems", TestUtils.PrivateStaticFlags, true);

            bool executedTask = (bool)privateScheduler.Invoke(
                "TryExecuteTaskInline",
                TestUtils.PrivateInstanceFlags,
                new Task(MultiThreadedTaskSchedulerTest.TestTask),
                true);

            Validate.Value.IsFalse(executedTask, "Should not be able to inline execute previously scheduled tasks.");
        }