Beispiel #1
0
        public void SpawnAndWait_SeveralTasks_AreExecutedInParallel()
        {
            int nrOfTasks = Environment.ProcessorCount - 2;

            if (nrOfTasks < 2)
            {
                Assert.Inconclusive("System does not have enough processors, skipping test");
            }

            int taskDurationInMs = 500;

            var tasks = new Action[nrOfTasks];

            for (int i = 0; i < nrOfTasks; i++)
            {
                tasks[i] = () => Thread.Sleep(taskDurationInMs);
            }

            var stopWatch = Stopwatch.StartNew();

            Utils.SpawnAndWait(tasks);
            stopWatch.Stop();

            stopWatch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(taskDurationInMs);
            stopWatch.ElapsedMilliseconds.Should().BeLessThan((int)(1.9 * taskDurationInMs));
        }
Beispiel #2
0
        public void SpawnAndWait_TaskWithTimeout_TimeoutsAndReturnsFalse()
        {
            int taskDurationInMs = 500, timeoutInMs = taskDurationInMs / 2;
            var tasks = new Action[] { () => Thread.Sleep(taskDurationInMs) };

            var  stopWatch        = Stopwatch.StartNew();
            bool hasFinishedTasks = Utils.SpawnAndWait(tasks, timeoutInMs);

            stopWatch.Stop();

            hasFinishedTasks.Should().BeFalse();
            stopWatch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(timeoutInMs - TestMetadata.ToleranceInMs);
            stopWatch.ElapsedMilliseconds.Should().BeLessThan(taskDurationInMs);
        }