Exemple #1
0
        public void Task_OneSyncContext()
        {
            string testName = "Task_OneSyncContext";

            var baseline = DoBaseTestRun(testName + "-Baseline", numTasks);

            var syncContext = new AsyncTestContext();

            SynchronizationContext.SetSynchronizationContext(syncContext);

            var tasks = new List <Task>(numTasks);

            QueuedTaskSchedulerTests_Set1.TimeRun(1, baseline, testName, () =>
            {
                for (int i = 0; i < numTasks; i++)
                {
                    Task t = CreateTask(i);
                    t.Start();
                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
            });

            foreach (Task t in tasks)
            {
                Assert.IsTrue(t.IsCompleted, "Task is completed");
                Assert.IsFalse(t.IsFaulted, "Task did not fault");
                Assert.IsNull(t.Exception, "Task did not return an Exception");
            }
        }
Exemple #2
0
        private static TimeSpan DoBaseTestRun(string runName, int numberOfTasks, TaskScheduler taskScheduler = null)
        {
            Console.WriteLine("NumTasks=" + numberOfTasks);

            var baseline = QueuedTaskSchedulerTests_Set1.TimeRun(1, TimeSpan.Zero, runName, () =>
            {
                var taskList = new List <Task>(numberOfTasks);

                for (int i = 0; i < numberOfTasks; i++)
                {
                    Task t = CreateTask(i);
                    if (taskScheduler == null)
                    {
                        t.Start();
                    }
                    else
                    {
                        t.Start(taskScheduler);
                    }
                    taskList.Add(t);
                }

                Task.WaitAll(taskList.ToArray());
            });

            return(baseline);
        }
Exemple #3
0
        public void Task_OrleansTaskScheduler()
        {
            string testName = "Task_OrleansTaskScheduler";

            var baseline = DoBaseTestRun(testName + "-Baseline", numTasks);

            var tasks = new List <Task>(numTasks);

            UnitTestSchedulingContext rootContext = new UnitTestSchedulingContext();

            TaskScheduler taskScheduler = TestInternalHelper.InitializeSchedulerForTesting(rootContext);

            QueuedTaskSchedulerTests_Set1.TimeRun(1, baseline, testName, () =>
            {
                for (int i = 0; i < numTasks; i++)
                {
                    string context = i.ToString();
                    Task t         = CreateTask(i, context);
                    t.Start(taskScheduler);
                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
            });

            foreach (Task t in tasks)
            {
                Assert.IsTrue(t.IsCompleted, "Task is completed");
                Assert.IsFalse(t.IsFaulted, "Task did not fault");
                Assert.IsNull(t.Exception, "Task did not return an Exception");
            }
        }
Exemple #4
0
        public void Task_MasterTaskScheduler()
        {
            string testName = "Task_MasterTaskScheduler";

            var baseline = DoBaseTestRun(testName + "-Baseline", numTasks);

            var tasks = new List <Task>(numTasks);

            var masterScheduler = GetTaskScheduler();

            TaskScheduler[] schedulers = new TaskScheduler[numTasks];
            for (int i = 0; i < numTasks; i++)
            {
                schedulers[i] = new TaskSchedulerWrapper(masterScheduler);
            }

            QueuedTaskSchedulerTests_Set1.TimeRun(1, baseline, testName, () =>
            {
                for (int i = 0; i < numTasks; i++)
                {
                    Task t = CreateTask(i);
                    t.Start(schedulers[i]);
                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
            });

            foreach (Task t in tasks)
            {
                Assert.IsTrue(t.IsCompleted, "Task is completed");
                Assert.IsFalse(t.IsFaulted, "Task did not fault");
                Assert.IsNull(t.Exception, "Task did not return an Exception");
            }
        }
Exemple #5
0
        public void Task_LogicalCallContext()
        {
            string testName = "Task_LogicalCallContext";
            string name     = "Foo";
            string val      = "Bar";

            var baseline = DoBaseTestRun(testName + "-Baseline", numTasks);

            CallContext.LogicalSetData(name, val);
            Assert.AreEqual(val, CallContext.LogicalGetData(name), "LogicalGetData outside Task");

            var tasks = new List <Task>(numTasks);

            QueuedTaskSchedulerTests_Set1.TimeRun(1, baseline, testName, () =>
            {
                for (int i = 0; i < numTasks; i++)
                {
                    int id = i;
                    Task t = new Task(() =>
                    {
                        if (Verbose)
                        {
                            Console.WriteLine("Task: " + id);
                        }
                        Assert.AreEqual(val, CallContext.LogicalGetData(name), "LogicalGetData inside Task " + id);
                    });
                    t.Start();
                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
            });

            foreach (Task t in tasks)
            {
                Assert.IsTrue(t.IsCompleted, "Task is completed");
                Assert.IsFalse(t.IsFaulted, "Task did not fault");
                Assert.IsNull(t.Exception, "Task did not return an Exception");
            }
        }