Esempio n. 1
0
        public async Task WithTaskFactory_When_Tasks_Added_Then_Execution_Time_Of_The_Tasks_Does_Not_Intersect()
        {
            const int NUMBER_OF_TASKS         = 100;
            const int DEFAULT_THREAD_SLEEP    = 20;
            const int BEGIN_TASK_THREAD_SLEEP = 1;

            var tasks = Enumerable.Range(0, NUMBER_OF_TASKS)
                        .Select(i => TestTaskFactory.StartNew(() =>
            {
                Thread.Sleep(BEGIN_TASK_THREAD_SLEEP);
                var startTime = DateTime.Now;
                var duration  = StopWatchUtils.MeasureActionTime(() => Thread.Sleep(DEFAULT_THREAD_SLEEP));
                return(new TimeRange(startTime, duration, isReadOnly: true));
            }))
                        .ToArray();

            await Task.WhenAll(tasks);

            var timeRanges            = tasks.Select(task => task.Result);
            var timePeriodCollection  = new TimePeriodCollection(timeRanges);
            var timePeriodIntersector = new TimePeriodIntersector <TimeRange>();
            var intersectPeriods      = timePeriodIntersector.IntersectPeriods(timePeriodCollection);

            Assert.IsTrue(!intersectPeriods.Any());
        }
Esempio n. 2
0
        public async Task WithTaskFactory_When_Tasks_Added_And_Acquiring_DummyLock_Then_All_Tasks_Executed_Sequentially()
        {
            const int numberOfTasks        = 100;
            const int DEFAULT_THREAD_SLEEP = 2;
            var       lockRoot             = new object();

            var tasks = Enumerable.Range(0, numberOfTasks)
                        .Select(i => TestTaskFactory.StartNew(() =>
            {
                var lockTaken = false;
                Monitor.TryEnter(lockRoot, ref lockTaken);
                try
                {
                    Thread.Sleep(DEFAULT_THREAD_SLEEP);
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(lockRoot);
                    }
                }
                return(lockTaken);
            }))
                        .ToArray();

            await Task.WhenAll(tasks);

            var executedSequentially = tasks.All(task => task.Result);

            Assert.IsTrue(executedSequentially);
        }
        public async Task WithTaskFactory_When_Tasks_Finished_Then_All_Tasks_Executed_In_Same_Thread()
        {
            const int NUMBER_OF_TASKS = 1000;

            var tasks = Enumerable.Range(0, NUMBER_OF_TASKS)
                        .Select(_ => TestTaskFactory.StartNew(() => Thread.CurrentThread.ManagedThreadId)).ToArray();

            await Task.WhenAll(tasks);

            var threadId             = tasks.First().Result;
            var allTasksInSameThread = tasks.All(task => task.Result == threadId);

            Assert.IsTrue(allTasksInSameThread);
        }
Esempio n. 4
0
        public async Task Dispatch_When_Called_Inside_Strand_Action_Is_Executed_Inline()
        {
            var originalTaskThreadId   = INVALID_THREAD_ID;
            var inDispatchTaskThreadId = INVALID_THREAD_ID;

            var task = TestTaskFactory.StartNew(() =>
            {
                originalTaskThreadId = Thread.CurrentThread.ManagedThreadId;
                m_strandScheduler.Dispatch(() => inDispatchTaskThreadId = Thread.CurrentThread.ManagedThreadId).Wait();
            });

            await task;

            Assert.AreNotEqual(INVALID_THREAD_ID, originalTaskThreadId);
            Assert.AreNotEqual(INVALID_THREAD_ID, inDispatchTaskThreadId);
            Assert.AreEqual(originalTaskThreadId, inDispatchTaskThreadId);
        }
Esempio n. 5
0
        public async Task Post_When_Called_Inside_Strand_Action_Is_Posted()
        {
            var originalTaskId   = INVALID_TASK_ID;
            var inDispatchTaskId = INVALID_TASK_ID;

            Task innerTask = null;
            var  outerTask = TestTaskFactory.StartNew(() =>
            {
                originalTaskId = Task.CurrentId.Value;
                innerTask      = m_strandScheduler.Post(() => inDispatchTaskId = Task.CurrentId.Value);
            });

            await outerTask;
            await innerTask;

            Assert.AreNotEqual(INVALID_TASK_ID, originalTaskId);
            Assert.AreNotEqual(INVALID_TASK_ID, inDispatchTaskId);
            Assert.AreNotEqual(originalTaskId, inDispatchTaskId);
        }
Esempio n. 6
0
        public async Task WithTaskFactory_When_Tasks_Added_Then_All_Tasks_Executed_Sequentially()
        {
            const int numberOfTasks        = 10;
            const int DEFAULT_THREAD_SLEEP = 200;
            var       tasks = Enumerable.Range(0, numberOfTasks)
                              .Select(i => TestTaskFactory.StartNew(() =>
            {
                Thread.Sleep(DEFAULT_THREAD_SLEEP);
                return(DateTime.Now);
            }))
                              .ToArray();

            await Task.WhenAll(tasks);

            var allTasksExecutedSequentially = tasks.Aggregate(
                new
            {
                Result       = true,
                PreviousTask = (Task <DateTime>)null
            },
                (prevResult, currentTask) =>
            {
                if (!prevResult.Result)
                {
                    return(prevResult);
                }

                return(new
                {
                    Result = (prevResult.PreviousTask != null
                     ? currentTask.Result > prevResult.PreviousTask.Result : prevResult.Result),
                    PreviousTask = currentTask
                });
            },
                resultPair => resultPair.Result);

            Assert.IsTrue(allTasksExecutedSequentially);
        }