示例#1
0
        public void CoroutineService_StartCoroutine_WithTask_RunsTask()
        {
            Stopwatch sw = new Stopwatch();

            bool didComplete = false;

            var coroutineService = new CoroutineService();

            async Task DoSomethingAsync()
            {
                await coroutineService.WaitForSecondsAsync(0.1f);

                didComplete = true;
            }

            var task = DoSomethingAsync();

            // Start a coroutine
            coroutineService.StartCoroutine(task);

            coroutineService.SetTime(10);
            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Coroutine did not complete");
            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine was not removed from scheduled");
        }
示例#2
0
        public void CoroutineService_WaitForSecondsAsync_ReturnTaskThatCompletesAfterNumSeconds()
        {
            float     timeoutSeconds = 0.2f;
            Stopwatch sw             = new Stopwatch();

            var coroutineService = new CoroutineService();

            Task task = coroutineService.WaitForSecondsAsync(0.1f);

            sw.Start();

            while (!task.IsCompleted && sw.ElapsedMilliseconds < timeoutSeconds)
            {
                coroutineService.SetTime(sw.ElapsedMilliseconds * 1000);
                Thread.Sleep(1);
            }

            Assert.Less(timeoutSeconds, sw.ElapsedMilliseconds, "Task timed out");
        }
        public IEnumerator CoroutineService_WaitForSecondsAsync_WaitForSeconds()
        {
            var   coroutineService = new CoroutineService();
            float delaySeconds     = 0.1f;
            float startTimeSeconds = Time.time;

            coroutineService.SetTime(startTimeSeconds);

            Task task = coroutineService.WaitForSecondsAsync(delaySeconds);

            while (!task.IsCompleted)
            {
                coroutineService.SetTime(Time.time);
                coroutineService.TickCoroutines();
                yield return(null);
            }

            Assert.IsTrue(Time.time >= startTimeSeconds + delaySeconds, "Not enough time has passed");
        }