public void CoroutineThreadIntShouldBeExecutedTheRightNumberOfCyclesByHundredsOfThings()
        {
            // ReSharper disable once ConvertToConstant.Local
            int cycleLength     = 100;
            int coroutinesCount = 30;
            int runningTimeMs   = 1000;
            var ft = new CoroutineThread(1);
            var fl = new List <WaitCoroutine>();

            for (int i = 0; i < coroutinesCount; i++)
            {
                var fb = new WaitCoroutine(cycleLength);
                fl.Add(fb);
                ft.AddCoroutine(fb);
            }

            ft.Start();
            Thread.Sleep(1000 + 100);
            ft.Stop();
            Thread.Sleep(300);
            var total = 0;

            foreach (var fb in fl)
            {
                total += fb.Cycles;
                Console.WriteLine("Result: " + fb.Cycles);
            }
            Console.WriteLine("Total: " + total);
            Assert.IsTrue(total > 100, "Executed " + total);
        }
        public void ShouldBeExecutedTheRightNumberOfCyclesDefaultThreads()
        {
            var threadContainer = new CoroutineThread(100);

            threadContainer.Start();
            _reset = new ManualResetEvent(false);
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;

            var first = new WaitCoroutine(hundred)
            {
                Thread = threadContainer
            };
            var second = new WaitCoroutine(200)
            {
                Thread = threadContainer
            };
            var firstt  = new Thread(ExecuteTimerCoroutine);
            var secondt = new Thread(ExecuteTimerCoroutine);

            firstt.Start(first);
            secondt.Start(second);
            _reset.Set();
            Thread.Sleep(1000);
            threadContainer.Stop();
            Thread.Sleep(300);
            Assert.IsTrue((first.Cycles + second.Cycles) < 100);
            Console.WriteLine("First: " + first.Cycles);
            Console.WriteLine("Second: " + second.Cycles);
        }
        public void CoroutineThreadIntShouldBePossibleToPauseAndRestart()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;
            var ft      = new CoroutineThread(1);
            var first   = new WaitCoroutine(hundred);
            var second  = new WaitCoroutine(200);

            ft.AddCoroutine(first, second);
            ft.Start();
            Thread.Sleep(1000);
            ft.Pause();
            Thread.Sleep(300);
            var firstCycles  = first.Cycles;
            var secondCycles = second.Cycles;

            Assert.AreEqual(ft.Status, CoroutineThreadStatus.Paused);
            Thread.Sleep(1500);
            Assert.AreEqual(firstCycles, first.Cycles);
            Assert.AreEqual(secondCycles, second.Cycles);
            ft.Start();
            Thread.Sleep(300);
            Assert.AreEqual(CoroutineThreadStatus.Running, ft.Status);
            ft.Stop();
            Thread.Sleep(300);
            Assert.AreEqual(CoroutineThreadStatus.Stopped, ft.Status);

            Assert.IsTrue(firstCycles <= first.Cycles);
            Assert.IsTrue(secondCycles <= second.Cycles);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Ticks all living coroutines, advancing time by the specified amount.
        /// </summary>
        /// <param name="elapsed">The time that has elapsed since the previous tick.</param>
        /// <returns>Whether any threads are still alive.</returns>
        public bool Tick(TimeSpan elapsed)
        {
            if (elapsed.Ticks < 0)
                throw new ArgumentOutOfRangeException(nameof(elapsed));
            if (_executingThread != null)
                throw new InvalidOperationException("Recursive ticking not allowed");

            _time += elapsed;

            for (int i = 0; i < _threads.Count; i++)
            {
                CoroutineThread thread = _threads[i];

                _executingThread = thread;
                try
                {
                    thread.Tick(elapsed);
                }
                finally
                {
                    _executingThread = null;
                }
            }

            return _threads.Count != 0;
        }
Exemplo n.º 5
0
 public void StopCoroutine()
 {
     IsRunning = false;
     CoroutineThread.Change(Timeout.Infinite, 10);
     CoroutineTPSCalc.Change(Timeout.Infinite, 1000);
     GameApplication.Log(Utils.LogEntryType.Info, "Stopped coroutine: " + Name);
 }
Exemplo n.º 6
0
 public override void Initialize()
 {
     if (Children != null && Children.Count > 0 && _threads.Count <= 0)
     {
         foreach (var n in Children)
         {
             var thread = new CoroutineThread <Result>(n.Execute, false, 1);
             _threads.Add(thread);
         }
     }
 }
        public void CoroutineIntItShouldBePossibleToDoOneLevelNesting()
        {
            var ft = new CoroutineThread(1);
            var co = new SingleNestedCoroutine();

            ft.AddCoroutine(co);
            ft.Start();
            Thread.Sleep(100);
            Assert.IsTrue(co.RunFinished);
            Assert.IsTrue(co.FirstCallFinished);
            ft.Stop();
        }
Exemplo n.º 8
0
        internal void TerminateThread(CoroutineThread <Result> thread)
        {
            if (_providersMap.TryGetValue(thread, out var nodes))
            {
                foreach (var node in nodes)
                {
                    node.Terminate();
                }
                nodes.Clear();
            }

            _manager.TerminateThreadAndDependencies(thread);
        }
        public void CoroutineThreadIntShouldBeExecutedTheRightNumberOfCycles()
        {
            var ft     = new CoroutineThread(1);
            var first  = new WaitCoroutine(100);
            var second = new WaitCoroutine(200);

            ft.AddCoroutine(first, second);
            ft.Start();
            Thread.Sleep(1100);
            ft.Stop();
            Thread.Sleep(300);
            Assert.IsTrue((first.Cycles + second.Cycles) >= 13, "Cycles were " + (first.Cycles + second.Cycles));
            Console.WriteLine("First: " + first.Cycles);
            Console.WriteLine("Second: " + second.Cycles);
        }
Exemplo n.º 10
0
        public void CoroutineIntShouldCallTheCoroutine()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;
            var ft      = new CoroutineThread(1);
            var co      = new CoroutineCallingLocal();

            ft.AddCoroutine(co);
            ft.Start();
            Thread.Sleep(100);
            ft.Stop();
            Thread.Sleep(300);
            Assert.IsTrue(co.LocalCalled);
            Assert.IsTrue(co.LocalSetted);
        }
Exemplo n.º 11
0
        public void CoroutineIntShouldCallTheCoroutineWaitingForIt()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;
            var ft      = new CoroutineThread(1);
            var co      = new LocalCoroutineWithWait();

            ft.AddCoroutine(co);
            ft.Start();
            Thread.Sleep(1000);
            ft.Stop();
            Thread.Sleep(300);
            Assert.IsTrue(co.LocalCalled);
            Assert.IsTrue(co.LocalSetted);
            Assert.AreEqual(0, co.Counter);
        }
Exemplo n.º 12
0
        public void CoroutineIntShouldCallTheCoroutineWithData()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;
            var data    = new Exception();
            var ft      = new CoroutineThread(1);
            var co      = new LocalCoroutineWithData(data);

            ft.AddCoroutine(co);
            ft.Start();
            Thread.Sleep(1000);
            ft.Stop();
            Thread.Sleep(300);
            Assert.IsTrue(co.LocalCalled);
            Assert.IsTrue(co.LocalSetted);
            Assert.AreEqual(data, co.Result);
        }
        public void CoroutineThreadIntShouldHandleCorrectlyTheSubroutines()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;
            var ft      = new CoroutineThread(1);
            var first   = new CoroutineWithSub(hundred);
            var second  = new CoroutineWithSub(200);

            ft.AddCoroutine(first);            //, second);
            ft.Start();
            Thread.Sleep(1000);
            ft.Stop();
            Thread.Sleep(300);
            //Assert.AreEqual(15, (int)(first.Cycles + second.Cycles));
            Console.WriteLine("First: " + first.Cycles);
            Console.WriteLine("Second: " + second.Cycles);
        }
        public void ShouldBeExecutedTheRightNumberOfCyclesByHundredsOfThingsDefaultThreads()
        {
            // ReSharper disable once ConvertToConstant.Local
            int hundred = 100;

            _reset = new ManualResetEvent(false);

            var threadContainer = new CoroutineThread(100);

            threadContainer.Start();

            var fl = new List <WaitCoroutine>();

            for (int i = 0; i < MULTIPLE_COUNT; i++)
            {
                var fb = new WaitCoroutine(hundred)
                {
                    Thread = threadContainer
                };
                fl.Add(fb);
                var flt = new Thread(ExecuteTimerCoroutine);
                flt.Start(fb);
            }
            _reset.Set();

            Thread.Sleep(1000);
            threadContainer.Stop();
            Thread.Sleep(300);
            var total = 0;

            foreach (var fb in fl)
            {
                total += fb.Cycles;
                Console.WriteLine("Result: " + fb.Cycles);
            }
            Console.WriteLine("Total: " + total);
            Assert.IsTrue(total < 100);
        }
Exemplo n.º 15
0
 internal void ProcessThread(CoroutineThread <Result> thread)
 {
     _manager.ProcessThread(thread);
 }
Exemplo n.º 16
0
 // ReSharper disable once RedundantAssignment
 public override CoroutineActionBehavior Process(CoroutineThread thread, ref IEnumerable cor)
 {
     cor = thread.Executor.Delay(_time);
     _time = default(TimeSpan);
     return CoroutineActionBehavior.Push;
 }
Exemplo n.º 17
0
 public override CoroutineActionBehavior Process(CoroutineThread thread, ref IEnumerable cor)
 {
     thread.SetResult(_value);
     _value = null;
     return CoroutineActionBehavior.Pop;
 }
Exemplo n.º 18
0
        /// <summary>
        /// Executes a coroutine in a new thread.
        /// </summary>
        /// <param name="cor">An enumerable object that can provide a coroutine.</param>
        public CoroutineThread Start(IEnumerable cor)
        {
            if (cor == null)
                throw new ArgumentNullException(nameof(cor));

            var thread = new CoroutineThread(this, cor);
            _threads.Add(thread);
            return thread;
        }
Exemplo n.º 19
0
        private static IEnumerable ParallelImpl(CoroutineThread[] threads)
        {
            while (true)
            {
                {
                    bool allFinished = true;

                    // ReSharper disable once ForCanBeConvertedToForeach
                    for (int i = 0; i < threads.Length; i++)
                    {
                        if (threads[i].Status == CoroutineThreadStatus.Faulted)
                            yield break;
                        if (threads[i].Status != CoroutineThreadStatus.Finished)
                            allFinished = false;
                    }

                    if (allFinished)
                        yield break;
                }

                yield return null;
            }
        }
Exemplo n.º 20
0
 internal void OnThreadDisposed(CoroutineThread thread)
 {
     _threads.Remove(thread);
 }
Exemplo n.º 21
0
 // ReSharper disable once RedundantAssignment
 public override CoroutineActionBehavior Process(CoroutineThread thread, ref IEnumerable cor)
 {
     cor = thread.Executor.Parallel(_enumerables);
     _enumerables = null;
     return CoroutineActionBehavior.Push;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Gets the next coroutine to be pushed onto the thread's stack. If null, yield until the next tick.
 /// </summary>
 /// <param name="thread">The current thread.</param>
 /// <param name="cor">A coroutine to push if the behavior is Push.</param>
 /// <returns>The behavior of the action.</returns>
 public abstract CoroutineActionBehavior Process(CoroutineThread thread, ref IEnumerable cor);
Exemplo n.º 23
0
 internal void ProcessThreadAsDependency(CoroutineThread <Result> thread)
 {
     _manager.ProcessThreadAsDependency(thread);
 }