2021-03-18 14:37:15 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using Coroutine;
|
2021-03-18 16:38:34 +01:00
|
|
|
using NUnit.Framework;
|
2021-03-18 14:37:15 +01:00
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
namespace Tests {
|
|
|
|
public class TimeBasedCoroutineTests {
|
2021-03-18 09:04:46 +01:00
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestTimerBasedCoroutine() {
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> OnTimeTickCodeExecuted() {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++;
|
|
|
|
yield return new Wait(0.1d);
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cr = CoroutineHandler.Start(OnTimeTickCodeExecuted());
|
|
|
|
Assert.AreEqual(1, counter, "instruction before yield is not executed.");
|
|
|
|
Assert.AreEqual(string.Empty, cr.Name, "Incorrect default name found");
|
|
|
|
Assert.AreEqual(0, cr.Priority, "Default priority is not minimum");
|
2021-03-18 16:38:34 +01:00
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(2, counter, "instruction after yield is not executed.");
|
|
|
|
Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value.");
|
|
|
|
Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value.");
|
|
|
|
Assert.AreEqual(cr.MoveNextCount, 2, "Incorrect MoveNextCount value.");
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestCoroutineReturningWeirdYields() {
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> OnTimeTickNeverReturnYield() {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++; // 1
|
2021-03-18 16:38:34 +01:00
|
|
|
// condition that's expected to be false
|
|
|
|
if (counter == 100)
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(0.1d);
|
|
|
|
counter++; // 2
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
IEnumerator<Wait> OnTimeTickYieldBreak() {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++; // 3
|
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cr = new ActiveCoroutine[2];
|
|
|
|
cr[0] = CoroutineHandler.Start(OnTimeTickNeverReturnYield());
|
|
|
|
cr[1] = CoroutineHandler.Start(OnTimeTickYieldBreak());
|
2021-03-18 16:38:34 +01:00
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(3, counter, "Incorrect counter value.");
|
|
|
|
for (var i = 0; i < cr.Length; i++) {
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(true, cr[i].IsFinished, $"Incorrect IsFinished value on index {i}.");
|
|
|
|
Assert.AreEqual(false, cr[i].WasCanceled, $"Incorrect IsCanceled value on index {i}");
|
|
|
|
Assert.AreEqual(1, cr[i].MoveNextCount, $"Incorrect MoveNextCount value on index {i}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestCoroutineReturningDefaultYield() {
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> OnTimeTickYieldDefault() {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++; // 1
|
|
|
|
yield return default;
|
|
|
|
counter++; // 2
|
|
|
|
}
|
|
|
|
|
|
|
|
var cr = CoroutineHandler.Start(OnTimeTickYieldDefault());
|
2021-03-18 16:38:34 +01:00
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(2, counter, "Incorrect counter value.");
|
|
|
|
Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value.");
|
|
|
|
Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value.");
|
|
|
|
Assert.AreEqual(2, cr.MoveNextCount, "Incorrect MoveNextCount value.");
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestInfiniteCoroutineNeverFinishesUnlessCanceled() {
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> OnTimerTickInfinite() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++;
|
|
|
|
yield return new Wait(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
void SetCounterToUnreachableValue(ActiveCoroutine coroutine) {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter = -100;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cr = CoroutineHandler.Start(OnTimerTickInfinite());
|
2021-03-18 16:38:34 +01:00
|
|
|
cr.OnFinished += SetCounterToUnreachableValue;
|
|
|
|
for (var i = 0; i < 50; i++)
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(51, counter, "Incorrect counter value.");
|
|
|
|
Assert.AreEqual(false, cr.IsFinished, "Incorrect IsFinished value.");
|
|
|
|
Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value.");
|
|
|
|
Assert.AreEqual(51, cr.MoveNextCount, "Incorrect MoveNextCount value.");
|
2021-03-18 09:04:46 +01:00
|
|
|
|
|
|
|
cr.Cancel();
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(true, cr.WasCanceled, "Incorrect IsCanceled value after canceling.");
|
|
|
|
Assert.AreEqual(-100, counter, "OnFinished event not triggered when canceled.");
|
|
|
|
Assert.AreEqual(51, cr.MoveNextCount, "Incorrect MoveNextCount value.");
|
|
|
|
Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value.");
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestOnFinishedEventExecuted() {
|
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> OnTimeTick() {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter++;
|
|
|
|
yield return new Wait(0.1d);
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
void SetCounterToUnreachableValue(ActiveCoroutine coroutine) {
|
2021-03-18 09:04:46 +01:00
|
|
|
counter = -100;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cr = CoroutineHandler.Start(OnTimeTick());
|
2021-03-18 16:38:34 +01:00
|
|
|
cr.OnFinished += SetCounterToUnreachableValue;
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(50);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(-100, counter, "Incorrect counter value.");
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestNestedCoroutine() {
|
|
|
|
var counterAlwaysRunning = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> AlwaysRunning() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterAlwaysRunning++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterChild = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> Child() {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterChild++;
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterParent = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> Parent() {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterParent++;
|
|
|
|
// OnFinish I will start child.
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterGrandParent = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> GrandParent() {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterGrandParent++;
|
|
|
|
// Nested corotuine starting.
|
|
|
|
var p = CoroutineHandler.Start(Parent());
|
|
|
|
// Nested corotuine starting in OnFinished.
|
2021-03-18 16:38:34 +01:00
|
|
|
p.OnFinished += ac => CoroutineHandler.Start(Child());
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
var always = CoroutineHandler.Start(AlwaysRunning());
|
2021-03-18 09:04:46 +01:00
|
|
|
CoroutineHandler.Start(GrandParent());
|
|
|
|
Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at time 0.");
|
|
|
|
Assert.AreEqual(0, counterGrandParent, "Grand Parent counter is invalid at time 0.");
|
|
|
|
Assert.AreEqual(0, counterParent, "Parent counter is invalid at time 0.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at time 0.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(1, counterAlwaysRunning, "Always running counter is invalid at time 1.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at time 1.");
|
|
|
|
Assert.AreEqual(0, counterParent, "Parent counter is invalid at time 1.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at time 1.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(2, counterAlwaysRunning, "Always running counter is invalid at time 2.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at time 2.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at time 2.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at time 2.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(3, counterAlwaysRunning, "Always running counter is invalid at time 3.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at time 3.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at time 3.");
|
|
|
|
Assert.AreEqual(1, counterChild, "Child counter is invalid at time 3.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(1);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(4, counterAlwaysRunning, "Always running counter is invalid at time 4.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at time 4.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at time 4.");
|
|
|
|
Assert.AreEqual(1, counterChild, "Child counter is invalid at time 4.");
|
2021-03-20 21:20:08 +01:00
|
|
|
always.Cancel();
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestPriority() {
|
|
|
|
var counterShouldExecuteBefore0 = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> ShouldExecuteBefore0() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterShouldExecuteBefore0++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterShouldExecuteBefore1 = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> ShouldExecuteBefore1() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterShouldExecuteBefore1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterShouldExecuteAfter = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> ShouldExecuteAfter() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
if (counterShouldExecuteBefore0 == 1 &&
|
2021-03-18 16:38:34 +01:00
|
|
|
counterShouldExecuteBefore1 == 1) {
|
2021-03-18 09:04:46 +01:00
|
|
|
counterShouldExecuteAfter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counterShouldExecuteFinally = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> ShouldExecuteFinally() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(1);
|
2021-03-18 16:38:34 +01:00
|
|
|
if (counterShouldExecuteAfter > 0) {
|
2021-03-18 09:04:46 +01:00
|
|
|
counterShouldExecuteFinally++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 03:23:11 +01:00
|
|
|
const int highPriority = int.MaxValue;
|
2021-03-20 21:20:08 +01:00
|
|
|
var before1 = CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority);
|
|
|
|
var after = CoroutineHandler.Start(ShouldExecuteAfter());
|
|
|
|
var before0 = CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority);
|
|
|
|
var @finally = CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1);
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(10);
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid.");
|
|
|
|
Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid.");
|
2021-03-20 21:20:08 +01:00
|
|
|
|
|
|
|
before1.Cancel();
|
|
|
|
after.Cancel();
|
|
|
|
before0.Cancel();
|
|
|
|
@finally.Cancel();
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void TestTimeBasedCoroutineIsAccurate() {
|
|
|
|
var counter0 = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> IncrementCounter0Ever10Seconds() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(10);
|
|
|
|
counter0++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var counter1 = 0;
|
|
|
|
|
|
|
|
IEnumerator<Wait> IncrementCounter1Every5Seconds() {
|
|
|
|
while (true) {
|
2021-03-18 09:04:46 +01:00
|
|
|
yield return new Wait(5);
|
|
|
|
counter1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
var incCounter0 = CoroutineHandler.Start(IncrementCounter0Ever10Seconds());
|
|
|
|
var incCounter1 = CoroutineHandler.Start(IncrementCounter1Every5Seconds());
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(3);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(0, counter0, "Incorrect counter0 value after 3 seconds.");
|
|
|
|
Assert.AreEqual(0, counter1, "Incorrect counter1 value after 3 seconds.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(3);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(0, counter0, "Incorrect counter0 value after 6 seconds.");
|
|
|
|
Assert.AreEqual(1, counter1, "Incorrect counter1 value after 6 seconds.");
|
2021-03-18 09:04:46 +01:00
|
|
|
|
|
|
|
// it's 5 over here because IncrementCounter1Every5Seconds
|
|
|
|
// increments 5 seconds after last yield. not 5 seconds since start.
|
|
|
|
// So the when we send 3 seconds in the last SimulateTime,
|
|
|
|
// the 3rd second was technically ignored.
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(5);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(1, counter0, "Incorrect counter0 value after 10 seconds.");
|
|
|
|
Assert.AreEqual(2, counter1, "Incorrect counter1 value after next 5 seconds.");
|
2021-03-20 21:20:08 +01:00
|
|
|
|
|
|
|
incCounter0.Cancel();
|
|
|
|
incCounter1.Cancel();
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
|
|
|
public void InvokeLaterAndNameTest() {
|
|
|
|
var counter = 0;
|
2021-03-18 09:04:46 +01:00
|
|
|
var cr = CoroutineHandler.InvokeLater(new Wait(10), () => {
|
|
|
|
counter++;
|
|
|
|
}, "Bird");
|
|
|
|
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(5);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(0, counter, "Incorrect counter value after 5 seconds.");
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(5);
|
2021-03-18 16:38:34 +01:00
|
|
|
Assert.AreEqual(1, counter, "Incorrect counter value after 10 seconds.");
|
2021-03-18 09:04:46 +01:00
|
|
|
Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value.");
|
|
|
|
Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value.");
|
|
|
|
Assert.AreEqual(cr.MoveNextCount, 2, "Incorrect MoveNextCount value.");
|
|
|
|
Assert.AreEqual(cr.Name, "Bird", "Incorrect name of the coroutine.");
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
[Test]
|
2021-03-21 17:54:21 +01:00
|
|
|
public void CoroutineStatsAreUpdated() {
|
2021-03-20 03:23:11 +01:00
|
|
|
static IEnumerator<Wait> CoroutineTakesMax500Ms() {
|
2021-03-18 09:04:46 +01:00
|
|
|
Thread.Sleep(200);
|
|
|
|
yield return new Wait(10);
|
|
|
|
Thread.Sleep(500);
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
var cr = CoroutineHandler.Start(CoroutineTakesMax500Ms());
|
|
|
|
for (var i = 0; i < 5; i++)
|
2021-03-20 15:13:39 +01:00
|
|
|
CoroutineHandler.Tick(50);
|
2021-03-18 09:04:46 +01:00
|
|
|
|
2021-03-21 17:54:21 +01:00
|
|
|
Assert.IsTrue(cr.TotalMoveNextTime.TotalMilliseconds >= 700);
|
|
|
|
Assert.IsTrue(cr.LastMoveNextTime.TotalMilliseconds >= 500);
|
|
|
|
Assert.IsTrue(cr.MoveNextCount == 2);
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 20:56:43 +01:00
|
|
|
[Test]
|
2021-03-20 21:20:08 +01:00
|
|
|
public void TestTickWithNestedAddAndRaiseEvent() {
|
2021-03-20 20:56:43 +01:00
|
|
|
var coroutineCreated = new Event();
|
|
|
|
var counterCoroutineA = 0;
|
|
|
|
var counter = 0;
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite());
|
2021-03-20 20:56:43 +01:00
|
|
|
CoroutineHandler.Start(OnEvent1());
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
Assert.AreEqual(3, counter);
|
|
|
|
Assert.AreEqual(2, counterCoroutineA);
|
2021-03-20 21:20:08 +01:00
|
|
|
infinite.Cancel();
|
2021-03-20 20:56:43 +01:00
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> OnCoroutineCreatedInfinite() {
|
|
|
|
while (true) {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(coroutineCreated);
|
|
|
|
counterCoroutineA++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> OnEvent1() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counter++;
|
|
|
|
CoroutineHandler.Start(OnEvent2());
|
|
|
|
CoroutineHandler.RaiseEvent(coroutineCreated);
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> OnEvent2() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counter++;
|
|
|
|
CoroutineHandler.Start(OnEvent3());
|
|
|
|
CoroutineHandler.RaiseEvent(coroutineCreated);
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> OnEvent3() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2021-03-20 21:20:08 +01:00
|
|
|
public void TestTickWithNestedAddAndRaiseEventOnFinish() {
|
2021-03-20 20:56:43 +01:00
|
|
|
var onChildCreated = new Event();
|
|
|
|
var onParentCreated = new Event();
|
|
|
|
var counterAlwaysRunning = 0;
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> AlwaysRunning() {
|
|
|
|
while (true) {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterAlwaysRunning++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var counterChild = 0;
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> Child() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterChild++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var counterParent = 0;
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> Parent() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterParent++;
|
|
|
|
// OnFinish I will start child.
|
|
|
|
}
|
|
|
|
|
|
|
|
var counterGrandParent = 0;
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
IEnumerator<Wait> GrandParent() {
|
2021-03-20 20:56:43 +01:00
|
|
|
yield return new Wait(1);
|
|
|
|
counterGrandParent++;
|
|
|
|
// Nested corotuine starting.
|
|
|
|
var p = CoroutineHandler.Start(Parent());
|
|
|
|
CoroutineHandler.RaiseEvent(onParentCreated);
|
|
|
|
// Nested corotuine starting in OnFinished.
|
|
|
|
p.OnFinished += ac => {
|
|
|
|
CoroutineHandler.Start(Child());
|
|
|
|
CoroutineHandler.RaiseEvent(onChildCreated);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:20:08 +01:00
|
|
|
var always = CoroutineHandler.Start(AlwaysRunning());
|
2021-03-20 20:56:43 +01:00
|
|
|
CoroutineHandler.Start(GrandParent());
|
|
|
|
Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at event 0.");
|
|
|
|
Assert.AreEqual(0, counterGrandParent, "Grand Parent counter is invalid at event 0.");
|
|
|
|
Assert.AreEqual(0, counterParent, "Parent counter is invalid at event 0.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at event 0.");
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
Assert.AreEqual(1, counterAlwaysRunning, "Always running counter is invalid at event 1.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 1.");
|
|
|
|
Assert.AreEqual(0, counterParent, "Parent counter is invalid at event 1.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at event 1.");
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
Assert.AreEqual(2, counterAlwaysRunning, "Always running counter is invalid at event 2.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 2.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 2.");
|
|
|
|
Assert.AreEqual(0, counterChild, "Child counter is invalid at event 2.");
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
Assert.AreEqual(3, counterAlwaysRunning, "Always running counter is invalid at event 3.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 3.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 3.");
|
|
|
|
Assert.AreEqual(1, counterChild, "Child counter is invalid at event 3.");
|
|
|
|
CoroutineHandler.Tick(1);
|
|
|
|
Assert.AreEqual(4, counterAlwaysRunning, "Always running counter is invalid at event 4.");
|
|
|
|
Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 4.");
|
|
|
|
Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 4.");
|
|
|
|
Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4.");
|
2021-03-20 21:20:08 +01:00
|
|
|
always.Cancel();
|
2021-03-20 20:56:43 +01:00
|
|
|
}
|
2021-03-20 21:20:08 +01:00
|
|
|
|
2021-03-18 09:04:46 +01:00
|
|
|
}
|
2021-03-18 16:38:34 +01:00
|
|
|
}
|