Example #1
0
        public void CoroutineService_StartCoroutine_RunsCoroutine_WithNestedCoroutine()
        {
            bool didCompleteInner  = false;
            bool didCompleteOutter = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutineInner()
            {
                yield return(null);

                didCompleteInner = true;
            }

            IEnumerator RunCoroutineOutter()
            {
                yield return(coroutineService.StartCoroutine(RunCoroutineInner()));

                didCompleteOutter = true;
            }

            coroutineService.StartCoroutine(RunCoroutineOutter());

            coroutineService.TickCoroutines();

            Assert.IsTrue(didCompleteInner, "Coroutine did not complete");
            Assert.IsTrue(didCompleteOutter, "Coroutine did not complete");
        }
        public IEnumerator StartCoroutine_Successfully()
        {
            const int testValue1 = 5;

            yield return(_coroutineService.StartCoroutine(TestCoroutine(testValue1)));

            Assert.AreEqual(testValue1, _testValue);
        }
Example #3
0
        public void CoroutineService_WaitUntil_CompletesCoroutineAfterPredicateReturnsFalse()
        {
            bool condition = true;

            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitUntil(() => condition));

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            Assert.IsFalse(didComplete, "Completed too early");

            condition = false;

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
        public IEnumerator CoroutineService_WaitForNumTicks_WaitsForNumTicks_WithServiceRunner()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

                IEnumerator RunCoroutine()
                {
                    yield return(coroutineService.WaitForNumTicks(2));

                    didComplete = true;
                }

                coroutineService.StartCoroutine(RunCoroutine());

                // This test may complete before ServiceRunner receives it's first update, depending on order of execution
                yield return(null);

                yield return(null);

                Assert.IsTrue(didComplete, "Coroutine did not complete");
            }
        }
Example #5
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");
        }
Example #6
0
 public virtual void LoadSceneAsync(string sceneName, Action <string> onDone = null)
 {
     coroutineService.StartCoroutine(LoadSceneAsyncCoroutine(sceneName, onDone));
     IsLoadingScene   = true;
     LoadingSceneName = sceneName;
     SceneLoadStartEvent?.Invoke(sceneName);
 }
Example #7
0
        public void CoroutineService_TickCoroutines_WithTask_Throws_WhenTaskFails()
        {
            var coroutineService = new CoroutineService();

            Assert.Throws <InvalidOperationException>(() =>
            {
                coroutineService.StartCoroutine(Task.FromException(new InvalidOperationException()));
            });
        }
Example #8
0
        public void CoroutineService_StartCoroutine_ThrowsException_WhenCoroutineThrowsImmediately()
        {
            IEnumerator RunCoroutine()
            {
                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            Assert.Throws <InvalidOperationException>(() => coroutineService.StartCoroutine(RunCoroutine()));
        }
Example #9
0
        public void CoroutineService_TickCoroutines_TicksScheduledCoroutines()
        {
            int numCompleted = 0;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                numCompleted++;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());
            coroutineService.StartCoroutine(RunCoroutine());

            Assert.AreEqual(2, coroutineService.NumScheduledCoroutines, "Coroutines were not scheduled");

            coroutineService.TickCoroutines();

            Assert.AreEqual(2, numCompleted, "Coroutines did not complete");
        }
Example #10
0
        public void CoroutineService_StartCoroutine_RunsParallelCoroutine()
        {
            bool didComplete1 = false;
            bool didComplete2 = false;

            IEnumerator RunCoroutine1()
            {
                yield return(null);

                didComplete1 = true;
            }

            IEnumerator RunCoroutine2()
            {
                yield return(null);

                yield return(null);

                didComplete2 = true;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine1());
            coroutineService.StartCoroutine(RunCoroutine2());

            Assert.AreEqual(2, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete1, "Coroutine did not complete");
            Assert.AreEqual(1, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete2, "Coroutine did not complete");
            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");
        }
Example #11
0
    /// <summary>
    /// Starts the timer
    /// </summary>
    /// <param name="onDoneEvent">Callback when the timer runs out</param>
    /// <param name="elapsedTime">How much time has already elapsed before starting? Useful when synchronizing a timer over network that is already running.</param>
    public virtual void Start(Action onDoneEvent = null, float elapsedTime = 0)
    {
        if (Duration == 0)
        {
            throw new Exception("Duration is cannot be 0, did you call Set()?");
        }

        if (IsRunning)
        {
            Stop();
        }

        TimeStarted = Time.time;
        ElapsedTime = elapsedTime;

        if (onDoneEvent != null)
        {
            onDone += onDoneEvent;
        }

        Task      = coroutineService.StartCoroutine(TimerCoroutine());
        IsRunning = true;
    }
Example #12
0
        public void CoroutineService_TickCoroutines_ThrowsException_WhenCoroutineThrowsInThatTick()
        {
            IEnumerator RunCoroutine()
            {
                yield return(null);

                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());

            Assert.Throws <InvalidOperationException>(() => coroutineService.TickCoroutines());
        }
Example #13
0
        public void CoroutineService_WaitForNextTick_CompletesCoroutineAfterNextTicks()
        {
            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNextTick());

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
Example #14
0
        public void CoroutineService_TickCoroutines_TicksFixedScheduledCoroutine()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForFixedTick());

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickFixedCoroutines();

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
Example #15
0
        public void CoroutineService_WaitForSecondsRealtime_CompletesCoroutineAfterNumRealtimeSeconds()
        {
            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForSecondsRealtime(0.1f));

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.SetRealTime(1);

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
Example #16
0
        public void CoroutineService_StartCoroutine_RunsCoroutine_MultipleFrames()
        {
            bool didComplete = false;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                yield return(null);

                didComplete = true;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();
            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Coroutine did not complete");
        }
Example #17
0
        public void CoroutineService_TickCoroutines_AdjustsTickCount()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNumTicks(2));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // 0->1
            coroutineService.TickCoroutines(); // 1->2

            coroutineService.TickCoroutines(); // 2->3, coroutine completes after 2 full ticks

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
        public IEnumerator CoroutineService_WaitForLateTick_RunsAsUnityCoroutine()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

                IEnumerator RunCoroutine()
                {
                    yield return(null);

                    didComplete = true;
                }

                yield return(coroutineService.StartCoroutine(RunCoroutine()));

                Assert.IsTrue(didComplete, "Coroutine did complete when it shouldn't have");
            }
        }
Example #19
0
        public void CoroutineService_TickCoroutines_AssignsCoroutineException_WhenCoroutineThrowsInThatTick()
        {
            IEnumerator RunCoroutine()
            {
                yield return(null);

                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            ICoroutine coroutine = coroutineService.StartCoroutine(RunCoroutine());

            try
            {
                coroutineService.TickCoroutines();
            }
            catch
            { }

            Assert.IsInstanceOf(typeof(InvalidOperationException), coroutine.Exception, "Did not assign exception");
        }
Example #20
0
        public void CoroutineService_StopCoroutine_StopsCoroutine()
        {
            bool didComplete = false;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                yield return(null);

                didComplete = true;
            }

            var coroutineService = new CoroutineService();
            var coroutine        = coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();
            coroutineService.StopCoroutine(coroutine);
            coroutineService.TickCoroutines();

            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine did not stop properly");
            Assert.IsFalse(didComplete, "Coroutine did not stop properly");
        }
        public IEnumerator CoroutineService_WaitForSecondsRealtime_CompletesCoroutineAfterRealtimeSeconds_WithServiceRunner()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

                IEnumerator RunCoroutine()
                {
                    yield return(coroutineService.WaitForSecondsRealtime(0.1f));

                    didComplete = true;
                }

                coroutineService.StartCoroutine(RunCoroutine());

                yield return(new WaitForSecondsRealtime(0.11f));

                Assert.IsTrue(didComplete, "Coroutine did not complete");
            }
        }
Example #22
0
        public void CoroutineService_SetNumTick_UpdatesCoroutineTickCount()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNumTicks(10));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // Still waits

            Assert.IsFalse(isCompleted, "Coroutine completed too early");

            coroutineService.SetNumTick(10);
            coroutineService.TickCoroutines(); // Completes

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
Example #23
0
        public void CoroutineService_SetRealTime_UpdatesCoroutineRealTime()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForSecondsRealtime(1f));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // Still waits

            Assert.IsFalse(isCompleted, "Coroutine completed too early");

            coroutineService.SetRealTime(1f);
            coroutineService.TickCoroutines(); // Completes

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
        public IEnumerator CoroutineService_WaitForFixedTick_WaitsForFixedUpdate_WithServiceRunner()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

                IEnumerator RunCoroutine()
                {
                    yield return(coroutineService.WaitForFixedTick());

                    didComplete = true;
                }

                coroutineService.StartCoroutine(RunCoroutine());

                yield return(new WaitForFixedUpdate());

                Assert.IsTrue(didComplete, "Coroutine did not complete");
            }
        }