Exemplo n.º 1
0
        public void TestTaskDelay()
        {
            var i      = 0;
            var run    = true;
            var thread = Thread.CurrentThread.ManagedThreadId;
            var co     = CoroutineContainer.StartCoroutine(Run);

            Assert.AreEqual(0, i);
            while (run && co.IsRunning())
            {
                TimerManager.Update(TimerManager.Now + TimeSpan.FromMilliseconds(100));
                CoroutineManager.OneLoop();
                Thread.Yield();
            }
            Assert.IsTrue(co.IsSuccess());
            Assert.AreEqual(3, i);
            async IWaitable Run()
            {
                i++;
                Assert.AreEqual(thread, Thread.CurrentThread.ManagedThreadId);
                await Task.Delay(1);

                i++;
                Assert.AreEqual(thread, Thread.CurrentThread.ManagedThreadId);
                await Task.Delay(1);

                i++;
                Assert.AreEqual(thread, Thread.CurrentThread.ManagedThreadId);
                run = false;
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var run = true;
            var coroutineManager = new CoroutineManager();
            var container        = coroutineManager.CreateContainer();

            container.StartCoroutine(MainLoop());

            IEnumerable MainLoop()
            {
                yield return(IsPrimeAsync(1).With <bool>(out var a));

                Console.WriteLine(a); // true

                yield return(IsPrimeAsync(2).With <bool>(out var b));

                Console.WriteLine(b); // true

                yield return(IsPrimeAsync(100).With <bool>(out var c));

                Console.WriteLine(c); // false

                run = false;
            }

            while (run)
            {
                coroutineManager.OneLoop();
            }
        }
Exemplo n.º 3
0
        public void Test2()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(Run());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            Tick();
            co.Throw();
            Assert.AreEqual(1, i);
            Assert.IsTrue(co.IsSuccess());

            IEnumerable Run()
            {
                Assert.AreEqual(0, i);
                yield return(null);

                Assert.AreEqual(0, i);
                i++;
                Assert.AreEqual(1, i);
            }
        }
Exemplo n.º 4
0
        public void TestAbort1()
        {
            var i = 0;

            CoroutineContainer.StartCoroutine(RunFather()).With(out var co);
            Assert.AreEqual(0, i);

            for (var tick = 0; tick < 1000; tick++)
            {
                CoroutineManager.OneLoop();
                if (i == 10)
                {
                    co.Abort();
                }
            }
            Assert.AreEqual(10, i);

            IEnumerable RunFather()
            {
                while (true)
                {
                    i++;
                    yield return(null);
                }
            }
        }
Exemplo n.º 5
0
        public void TestYield()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(Run());

            Assert.AreEqual(0, i);
            Assert.IsTrue(co.IsRunning());

            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);

            Assert.IsTrue(co.IsSuccess());

            IEnumerable Run()
            {
                i++;
                yield return(WaitFor.Yield());

                i++;
                yield return(WaitFor.Yield());

                i++;
            }
        }
Exemplo n.º 6
0
        public void TestCascadeThrow2()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            Assert.IsTrue(co.IsError());
            Tick();

            //handle
            Assert.AreEqual(3, i);

            IEnumerable RunFather()
            {
                i++;
                yield return(CoroutineContainer.StartCoroutine(RunChild()));

                Assert.Fail();
            }

            IEnumerable RunChild()
            {
                i++;
                yield return(null);

                i++;
                throw new ArgumentException();
            }
        }
Exemplo n.º 7
0
        public void TestCascadeThrow3()
        {
            var i = 0;

            CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            Tick();

            //handle
            Assert.AreEqual(-10, i);

            IEnumerable RunFather()
            {
                i++;

                yield return(CoroutineContainer.StartCoroutine(RunChild()).Catch(e =>
                {
                    i = -10;
                }));

                Assert.Fail();
            }

            IEnumerable RunChild()
            {
                i++;
                yield return(null);

                i++;
                throw new ArgumentException();
            }
        }
Exemplo n.º 8
0
        public void TestAwait()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(Run);

            Assert.AreEqual(0, i);
            Assert.IsTrue(co.IsRunning());

            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);

            Assert.IsTrue(co.IsSuccess());

            async IWaitable Run()
            {
                i++;
                await WaitFor.Yield();

                i++;
                await WaitFor.Yield();

                i++;
            }
        }
Exemplo n.º 9
0
        public void TestCascadeThrow1()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            Assert.IsTrue(co.IsRunning());
            Tick();
            Assert.AreEqual(1, i);
            Assert.IsTrue(co.IsError());

            IEnumerable RunFather()
            {
                i++;
                yield return(CoroutineContainer.StartCoroutine(RunChild(true)));

                Assert.Fail();
            }

            IEnumerable RunChild(bool b)
            {
                if (b)
                {
                    throw new ArgumentException();
                }
                yield break;
            }
        }
Exemplo n.º 10
0
        public void TestClear()
        {
            var i = 0;

            CoroutineContainer.StartCoroutine(Run());
            CoroutineContainer.StartCoroutine(Run());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);

            CoroutineContainer.ClearAllCoroutines();

            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);

            IEnumerable Run()
            {
                while (true)
                {
                    yield return(null);

                    i++;
                }
            }
        }
Exemplo n.º 11
0
        public void TestCascade()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(RunFather());

            Assert.IsTrue(co.IsRunning());
            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(4, i);
            Assert.IsTrue(co.IsSuccess());
            Tick();
            Assert.IsTrue(co.IsSuccess());

            IEnumerable RunFather()
            {
                i++;
                yield return(CoroutineContainer.StartCoroutine(RunChild()));

                i++;
            }

            IEnumerable RunChild()
            {
                i++;
                yield return(null);

                i++;
            }
        }
Exemplo n.º 12
0
 protected void Tick()
 {
     for (var i = 0; i < 5000; i++)
     {
         TimerManager.Update(TimerManager.Now + TimeSpan.FromMilliseconds(100));
         CoroutineManager.OneLoop();
     }
 }
Exemplo n.º 13
0
        public void TestAbort3()
        {
            var i = 0;
            var j = 0;

            var co2 = CoroutineContainer.StartCoroutine(RunChild());

            Assert.AreEqual(0, i);

            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);

            var co1 = CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(2, i);

            CoroutineManager.OneLoop();
            //father++ child++
            Assert.AreEqual(4, i);
            CoroutineManager.OneLoop();
            //father wait child++
            Assert.AreEqual(5, i);

            co2.Abort();
            Assert.AreEqual(6, i);
            Assert.AreEqual(1, j);

            CoroutineManager.OneLoop();
            //father++ child abort
            Assert.IsTrue(co2.IsAborted());
            Assert.AreEqual(6, i);

            Tick();
            Assert.AreEqual(6, i);
            Assert.AreEqual(1, j);

            Assert.AreEqual(WaitableStatus.Abort, co2.Status);
            Assert.AreEqual(WaitableStatus.Success, co1.Status);

            IEnumerable RunFather()
            {
                i++;
                yield return(co2.Catch(e => j++));

                i++;
            }

            IEnumerable RunChild()
            {
                while (true)
                {
                    i++;
                    yield return(null);
                }
            }
        }
Exemplo n.º 14
0
 public static void Main(string[] args)
 {
     container.StartCoroutine(OneLoop);
     while (true)
     {
         frame++;
         manager.OneLoop();
         Thread.Sleep(1);
     }
 }
Exemplo n.º 15
0
        public static void Main(string[] args)
        {
            var coroutineManager = new CoroutineManager();

            Container = coroutineManager.CreateContainer();

            Container.StartCoroutine(MainLoop);
            while (true)
            {
                coroutineManager.OneLoop();
                Thread.Sleep(10);
            }
        }
Exemplo n.º 16
0
        public void TestAbort4()
        {
            var i = 0;

            var co2 = CoroutineContainer.StartCoroutine(RunChild());

            Assert.AreEqual(0, i);

            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);

            var co1 = CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(2, i);

            CoroutineManager.OneLoop();
            Assert.AreEqual(4, i);

            co1.Abort(false);
            Assert.AreEqual(4, i);

            CoroutineManager.OneLoop();
            Assert.AreEqual(5, i);

            CoroutineManager.OneLoop();
            Assert.AreEqual(6, i);

            IEnumerable RunFather()
            {
                i++;
                yield return(co2);

                i++;
            }

            IEnumerable RunChild()
            {
                while (true)
                {
                    i++;
                    yield return(null);
                }
            }
        }
Exemplo n.º 17
0
        public void Test1()
        {
            var i  = 0;
            var co = CoroutineContainer.StartCoroutine(Run());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            Tick();
            Assert.AreEqual(1, i);
            Assert.IsTrue(co.IsSuccess());

            IEnumerable Run()
            {
                i++;
                yield break;
            }
        }
Exemplo n.º 18
0
        public void TestAbort2()
        {
            var       i   = 0;
            IWaitable co2 = null;

            var co1 = CoroutineContainer.StartCoroutine(RunFather());

            Assert.AreEqual(0, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(1, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(2, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);
            co1.Abort();
            Assert.AreEqual(3, i);
            CoroutineManager.OneLoop();
            Assert.AreEqual(3, i);

            Tick();
            Assert.AreEqual(3, i);
            Assert.AreEqual(WaitableStatus.Abort, co1.Status);
            Assert.AreEqual(WaitableStatus.Abort, co2.Status);
            Assert.IsTrue(co1.IsAborted());
            Assert.IsTrue(co2.IsAborted());

            IEnumerable RunFather()
            {
                i++;
                co2 = CoroutineContainer.StartCoroutine(RunChild());
                yield return(co2);

                i++;
            }

            IEnumerable RunChild()
            {
                while (true)
                {
                    i++;
                    yield return(null);
                }
            }
        }