Example #1
0
        public void TaskAllMethodCombinesTasksInParallel()
        {
            const int   taskCount = 10;
            const int   taskTime  = 500; //ms
            const float tolerance = 50;  //ms
            const int   testLimit = 600; //ms

            UnityTask[]   tasks   = new UnityTask[taskCount];
            List <Thread> threads = new List <Thread>(taskCount);

            for (int i = 0; i < taskCount; i++)
            {
                UnityTask task = new UnityTask();
                tasks[i] = task;
                Thread thread = new Thread(() =>
                {
                    try
                    {
                        Thread.Sleep(taskTime);
                        task.Resolve();
                    }
                    catch (Exception e)
                    {
                        task.Reject(e);
                    }
                });
                thread.Start();
                threads.Add(thread);
            }

            DateTime startTime = DateTime.Now;
            object   result    = UnityTask.All(tasks).Result; // Result blocks until finished
            TimeSpan totalTime = DateTime.Now - startTime;

            Assert.AreEqual(totalTime.TotalMilliseconds, (float)taskTime, tolerance);
        }