public void ItShouldBePossibleToWaitForATaskToCompleteWithinTimeout()
        {
            _taskStarted = false;
            _taskRun     = false;
            _completed   = false;
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(CoroutineWaitingHavingTimeoutNotExploding());

            var target = new CoroutinesManager();

            target.TestInitialize();
            var rft = new RunnerForTest(target);

            target.StartCoroutine(coroutine.Object);
            target.TestRun();
            rft.RunCycleFor(200);
            target.TestRun();

            Assert.IsTrue(_taskStarted);
            Assert.IsTrue(_taskRun);
            Assert.IsTrue(_completed);
            coroutine.Verify(a => a.OnError(It.IsAny <Exception>()), Times.Never);
        }
Ejemplo n.º 2
0
        public void NotWaitingItShouldBePossibleToWaitForATaskToCompleteWithTimeoutError()
        {
            _notWaitingCompleted = false;
            _notWaitingStarted   = false;
            _taskRun             = false;
            _taskStarted         = false;
            _completedTaks       = false;
            _exception           = null;
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(NotWaitingHavingTimeout());

            var target = new CoroutinesManager();

            target.TestInitialize();
            var rtt = new RunnerForTest(target);

            target.StartCoroutine(coroutine.Object);

            rtt.RunCycle();             //Coroutine initialized
            Assert.IsTrue(_notWaitingStarted);
            rtt.RunCycle(2);            //Coroutine started
            Assert.IsTrue(_notWaitingCompleted);


            Assert.IsTrue(_taskStarted);
            Assert.IsNull(_exception);
            Assert.IsFalse(_completedTaks);

            rtt.RunCycleFor(150);
            Assert.IsTrue(_taskStarted);
            Assert.IsNotNull(_exception);
            Assert.IsFalse(_completedTaks);
        }
Ejemplo n.º 3
0
        public void RunningACoroutineFromEverywhereShouldPropagateExceptions()
        {
            var coroutine = new ExeCoRoutine();

            coroutine.Enumerable = ExecuteAndThrowAfterInitialization().GetEnumerator();

            var target = new CoroutinesManager();

            target.TestInitialize();
            RunnerFactory.Initialize(() => target);

            target.Start();
            var parent = Task.Run(() =>
            {
                var task = CoroutineResult.WaitForCoroutine(coroutine);
                task.Wait();
            });

            Exception expected = null;

            try
            {
                parent.Wait();
            }
            catch (Exception ex)
            {
                expected = ex;
            }
            target.Stop();

            Assert.IsNotNull(expected);
        }
Ejemplo n.º 4
0
        public void NotWaitingItShouldBePossibleToWaitForEnEntireForEach()
        {
            _notWaitingStarted     = false;
            _notWaitingCompleted   = false;
            _coroutineResultsCount = 0;
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(NotWaitingForAllResults(10));

            var target = new CoroutinesManager();

            target.TestInitialize();
            var rtt = new RunnerForTest(target);

            target.StartCoroutine(coroutine.Object);

            rtt.RunCycle();             //Coroutine initialized
            Assert.IsTrue(_notWaitingStarted);
            rtt.RunCycle(2);            //Coroutine started
            Assert.IsTrue(_notWaitingCompleted);
            Assert.AreEqual(0, _coroutineResultsCount);

            rtt.RunCycle(50);             //Coroutine started


            Assert.AreEqual(10, _coroutineResultsCount);
        }
        public void ItShouldBePossibleToWaitForCoroutineCallingFunctionThatWait()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(CoroutineWaitingForSingleResult(10));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun(20);

            Assert.AreEqual("RESULT", _coroutineSingleResult);
        }
Ejemplo n.º 6
0
        public void WhenTerminatingShouldBeCalledOnDestroy()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(ExecuteAndTerminate);

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);

            target.TestRun();

            coroutine.Verify(a => a.OnDestroy(), Times.Once);
        }
Ejemplo n.º 7
0
        public void RunnerShouldCallExecute()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(Execute());

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);

            target.TestRun();

            coroutine.Verify(a => a.Execute(), Times.Once);
        }
Ejemplo n.º 8
0
        public void NotWaitingItShouldBePossibleToWaitForCoroutineCallingFunctionThatWait()
        {
            const int items        = 10;
            const int itemsAndWait = items * 2;
            var       coroutine    = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(NotWaitingCoroutineWaitingForSingleResult(items));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun(itemsAndWait);

            Assert.AreEqual("RESULT", _coroutineSingleResult);
        }
Ejemplo n.º 9
0
        public void ExceptionOnFirstyCycleShouldBeHandledAndRoutineTerminatedForcibly()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(ExecuteAndThrowInstantly);

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);

            target.TestRun();

            coroutine.Verify(a => a.OnError(It.IsAny <NotImplementedException>()), Times.Once);
            coroutine.Verify(a => a.OnDestroy(), Times.Once);
        }
Ejemplo n.º 10
0
        public void ExceptionInNestedCallShouldBeHandledByCaller()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(ExecuteNestedAndThrowInsideNested(10));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun(6);

            Assert.AreEqual(6, _cyclesRunExecuteNested);

            coroutine.Verify(a => a.OnError(It.IsAny <NotImplementedException>()));
            coroutine.Verify(a => a.OnDestroy(), Times.Never);
        }
Ejemplo n.º 11
0
        public void ExceptionShouldTerminateCoroutineWhenOnErrorReturnTrue()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(ExecuteWithErrorAndContinue);
            coroutine.Setup(a => a.OnError(It.IsAny <Exception>()))
            .Returns(true);

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            _shouldThrowExceptionOnExecuteWithErrorAndContinue = true;
            target.TestRun(3);

            coroutine.Verify(a => a.OnError(It.IsAny <NotImplementedException>()), Times.Once);
            coroutine.Verify(a => a.OnDestroy(), Times.Once);
        }
Ejemplo n.º 12
0
        public void PausingRunnerShouldPauseAllCoroutines()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(Execute(10));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun();
            target.Pause();
            target.TestRunWhenPaused(10);
            target.Restart();
            target.TestRun();

            Assert.AreEqual(_cyclesRunSimpleExecute, 2);
            coroutine.Verify(a => a.OnDestroy(), Times.Never);
        }
        public void WaitForTaskToComplete()
        {
            const int waitTime  = 100;
            var       coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(CoroutineWaitingForTask(waitTime));

            var target = new CoroutinesManager();

            target.TestInitialize();
            var rft = new RunnerForTest(target);

            target.StartCoroutine(coroutine.Object);
            target.TestRun();                   //Initialize
            rft.RunCycleFor(waitTime * 2);
            target.TestRun();                   //CleanUp

            Assert.IsTrue(_taskStarted);
            Assert.IsTrue(_taskRun);
            Assert.IsTrue(_completed);
        }
        public void ItShouldBePossibleToWaitForEnEntireForEach()
        {
            _completed = false;
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(CoroutineWaitingForSeveralItems(10));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun(3);                  //Initialize the call
            target.TestRun(20);                 //Set results

            Assert.AreEqual(10, _partialCoroutineResultsCount);
            Assert.IsTrue(_completed);

            target.TestRun(2);                  //Copy the completed
            Assert.AreEqual(10, _coroutineResultsCount);
        }
Ejemplo n.º 15
0
        public void StoppingRunnerShouldSendManagerStoppedExceptionToAllCoroutinesDestroyingEvenIfNotRequired()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(Execute(3));
            coroutine.Setup(a => a.OnError(It.IsAny <Exception>()))
            .Returns(false);


            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            target.TestRun();
            target.Stop();
            target.TestRun();

            coroutine.Verify(a => a.OnError(It.IsAny <ManagerStoppedException>()), Times.Once);
            coroutine.Verify(a => a.OnDestroy(), Times.Once);
        }
Ejemplo n.º 16
0
        public void NotWaitingItShouldBePossibleToWaitForATaskToCompleteWithTimeout()
        {
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(NotWaitingCoroutineWaitingHavingTimeoutNotExploding());
            coroutine.Setup(o => o.OnError(It.IsAny <Exception>())).Returns(true);

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);
            Task.Factory.StartNew(() => target.TestRun(3));
            Thread.Sleep(300);
            target.TestRun();

            coroutine.Verify(a => a.OnError(It.IsAny <Exception>()), Times.Never);
            Assert.IsTrue(_taskStarted);
            Assert.IsTrue(_taskRun);
            Assert.IsTrue(_completed);
        }
Ejemplo n.º 17
0
        public void RunningACoroutineFromEverywhere()
        {
            var coroutine = new ExeCoRoutine();

            coroutine.Enumerable = Execute().GetEnumerator();

            var target = new CoroutinesManager();

            target.TestInitialize();
            RunnerFactory.Initialize(() => target);

            target.Start();
            var parent = Task.Run(() =>
            {
                var task = CoroutineResult.WaitForCoroutine(coroutine);
                task.Wait();
            });

            parent.Wait();
            target.Stop();

            Assert.AreEqual(1, coroutine.Cycles);
        }
Ejemplo n.º 18
0
        public void NestedCallsToCoroutinesShouldWork()
        {
            _cyclesRunSimpleExecute = 0;
            _cyclesRunExecuteNested = 0;
            var coroutine = new Mock <ICoroutineThread>();

            coroutine.Setup(a => a.Execute())
            .Returns(ExecuteNested(10));

            var target = new CoroutinesManager();

            target.TestInitialize();

            target.StartCoroutine(coroutine.Object);

            target.TestRun(20);

            Assert.AreEqual(10, _cyclesRunSimpleExecute);
            Assert.AreEqual(10, _cyclesRunExecuteNested);
            coroutine.Verify(a => a.OnDestroy(), Times.Never);

            target.TestRun();
            coroutine.Verify(a => a.OnDestroy(), Times.Once);
        }
Ejemplo n.º 19
0
 public RunnerForTest()
 {
     RunnerFactory.Initialize();
     _runner = (CoroutinesManager)RunnerFactory.Create();
     _runner.TestInitialize();
 }