예제 #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());
        }
예제 #2
0
        private TimeRange getTaskTimeRange()
        {
            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));
        }
예제 #3
0
        public void Run_When_Work_Canceled_And_Zero_Tasks_Then_Method_Returns_Immediately()
        {
            const double RUN_MAX_DURATION_S = 0.5;

            var work = new Work(m_scheduler);

            work.Dispose();

            var time = StopWatchUtils.MeasureActionTime(() => m_scheduler.Run());

            Assert.IsTrue(time.TotalSeconds < RUN_MAX_DURATION_S);
        }
예제 #4
0
        public void RunOne_When_Zero_Tasks_Then_Method_Does_Not_Return()
        {
            const int    SCHEDULE_WORK_AFTER_MS = 3000;
            const double RUN_MIN_DURATION_S     = 2.0;

            var time = StopWatchUtils.MeasureActionTime(() =>
            {
                scheduleTaskAfterDelay(SCHEDULE_WORK_AFTER_MS);
                m_scheduler.RunOne();
            });

            Assert.IsTrue(time.TotalSeconds > RUN_MIN_DURATION_S);
        }
예제 #5
0
        public void Run_When_Work_Exists_And_Zero_Tasks_Then_Method_Does_Not_Return()
        {
            const int    WORK_CANCEL_DELAY_MS = 3000;
            const double RUN_MIN_DURATION_S   = 2.0;

            var time = StopWatchUtils.MeasureActionTime(() =>
            {
                cancelWorkAfterTimeout(WORK_CANCEL_DELAY_MS);
                m_scheduler.Run();
            });

            Assert.IsTrue(time.TotalSeconds > RUN_MIN_DURATION_S);
        }
예제 #6
0
        public virtual async Task Run()
        {
            const int PING_COUNT = 100;

            var duration = await StopWatchUtils.MeasureActionTime(async() =>
            {
                var firstPlayer  = m_player1.Ping(PING_COUNT, m_player2, GAME_1_NAME);
                var secondPlayer = m_player2.Ping(PING_COUNT, m_player1, GAME_2_NAME);
                await Task.WhenAll(firstPlayer, secondPlayer);
            });

            Console.WriteLine(RUN_DURATION_MESSAGE_FORMAT, duration.TotalMilliseconds);
        }
예제 #7
0
        public virtual void Run()
        {
            m_engine.RunStrandConcurrentSchedulerTest(NUMBER_OF_CONCURRENT_TASKS, NUMBER_OF_EXCLUSIVE_TASKS);
            m_engine.RunExclusiveConcurrentSchedulerTest(NUMBER_OF_CONCURRENT_TASKS, NUMBER_OF_EXCLUSIVE_TASKS);

            var exclusiveConcurrentSchedulerDuration = StopWatchUtils.MeasureActionTime(() =>
                                                                                        m_engine.RunExclusiveConcurrentSchedulerTest(NUMBER_OF_CONCURRENT_TASKS, NUMBER_OF_EXCLUSIVE_TASKS).Wait());

            printResults("ExclusiveConcurrentScheduler:", exclusiveConcurrentSchedulerDuration);

            var strandConcurrentSchedulerDuration = StopWatchUtils.MeasureActionTime(() =>
                                                                                     m_engine.RunStrandConcurrentSchedulerTest(NUMBER_OF_CONCURRENT_TASKS, NUMBER_OF_EXCLUSIVE_TASKS).Wait());


            printResults("StrandConcurrentScheduler:", strandConcurrentSchedulerDuration);
        }