make sure all infinite coroutines are canceled in tests

This commit is contained in:
Ell 2021-03-20 21:20:08 +01:00
parent d9f8c383ea
commit 1e8a9e4e6c
2 changed files with 47 additions and 41 deletions

View file

@ -134,7 +134,7 @@ namespace Tests {
}; };
} }
CoroutineHandler.Start(AlwaysRunning()); var always = CoroutineHandler.Start(AlwaysRunning());
CoroutineHandler.Start(GrandParent()); CoroutineHandler.Start(GrandParent());
Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at event 0."); 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, counterGrandParent, "Grand Parent counter is invalid at event 0.");
@ -164,6 +164,7 @@ namespace Tests {
Assert.AreEqual(1, counterGrandParent, "Grand Parent 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, counterParent, "Parent counter is invalid at event 4.");
Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4."); Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4.");
always.Cancel();
} }
[Test] [Test]
@ -175,7 +176,7 @@ namespace Tests {
var counterCoroutineA = 0; var counterCoroutineA = 0;
var counter = 0; var counter = 0;
CoroutineHandler.Start(OnCoroutineCreatedInfinite()); var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite());
CoroutineHandler.Start(OnEvent1()); CoroutineHandler.Start(OnEvent1());
CoroutineHandler.Tick(1); CoroutineHandler.Tick(1);
CoroutineHandler.RaiseEvent(event1); CoroutineHandler.RaiseEvent(event1);
@ -185,10 +186,10 @@ namespace Tests {
CoroutineHandler.RaiseEvent(event3); CoroutineHandler.RaiseEvent(event3);
Assert.AreEqual(3, counter); Assert.AreEqual(3, counter);
Assert.AreEqual(2, counterCoroutineA); Assert.AreEqual(2, counterCoroutineA);
infinite.Cancel();
IEnumerator<Wait> OnCoroutineCreatedInfinite() { IEnumerator<Wait> OnCoroutineCreatedInfinite() {
while (true) while (true) {
{
yield return new Wait(coroutineCreated); yield return new Wait(coroutineCreated);
counterCoroutineA++; counterCoroutineA++;
} }
@ -259,14 +260,19 @@ namespace Tests {
} }
const int highPriority = int.MaxValue; const int highPriority = int.MaxValue;
CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority); var before1 = CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority);
CoroutineHandler.Start(ShouldExecuteAfter()); var after = CoroutineHandler.Start(ShouldExecuteAfter());
CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority); var before0 = CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority);
CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1); var @finally = CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1);
CoroutineHandler.Tick(1); CoroutineHandler.Tick(1);
CoroutineHandler.RaiseEvent(myEvent); CoroutineHandler.RaiseEvent(myEvent);
Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid."); Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid.");
Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid."); Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid.");
before1.Cancel();
after.Cancel();
before0.Cancel();
@finally.Cancel();
} }
[Test] [Test]

View file

@ -167,7 +167,7 @@ namespace Tests {
p.OnFinished += ac => CoroutineHandler.Start(Child()); p.OnFinished += ac => CoroutineHandler.Start(Child());
} }
CoroutineHandler.Start(AlwaysRunning()); var always = CoroutineHandler.Start(AlwaysRunning());
CoroutineHandler.Start(GrandParent()); CoroutineHandler.Start(GrandParent());
Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at time 0."); 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, counterGrandParent, "Grand Parent counter is invalid at time 0.");
@ -193,6 +193,7 @@ namespace Tests {
Assert.AreEqual(1, counterGrandParent, "Grand Parent 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, counterParent, "Parent counter is invalid at time 4.");
Assert.AreEqual(1, counterChild, "Child counter is invalid at time 4."); Assert.AreEqual(1, counterChild, "Child counter is invalid at time 4.");
always.Cancel();
} }
[Test] [Test]
@ -239,13 +240,18 @@ namespace Tests {
} }
const int highPriority = int.MaxValue; const int highPriority = int.MaxValue;
CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority); var before1 = CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority);
CoroutineHandler.Start(ShouldExecuteAfter()); var after = CoroutineHandler.Start(ShouldExecuteAfter());
CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority); var before0 = CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority);
CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1); var @finally = CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1);
CoroutineHandler.Tick(10); CoroutineHandler.Tick(10);
Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid."); Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid.");
Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid."); Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid.");
before1.Cancel();
after.Cancel();
before0.Cancel();
@finally.Cancel();
} }
[Test] [Test]
@ -268,8 +274,8 @@ namespace Tests {
} }
} }
CoroutineHandler.Start(IncrementCounter0Ever10Seconds()); var incCounter0 = CoroutineHandler.Start(IncrementCounter0Ever10Seconds());
CoroutineHandler.Start(IncrementCounter1Every5Seconds()); var incCounter1 = CoroutineHandler.Start(IncrementCounter1Every5Seconds());
CoroutineHandler.Tick(3); CoroutineHandler.Tick(3);
Assert.AreEqual(0, counter0, "Incorrect counter0 value after 3 seconds."); Assert.AreEqual(0, counter0, "Incorrect counter0 value after 3 seconds.");
Assert.AreEqual(0, counter1, "Incorrect counter1 value after 3 seconds."); Assert.AreEqual(0, counter1, "Incorrect counter1 value after 3 seconds.");
@ -284,6 +290,9 @@ namespace Tests {
CoroutineHandler.Tick(5); CoroutineHandler.Tick(5);
Assert.AreEqual(1, counter0, "Incorrect counter0 value after 10 seconds."); Assert.AreEqual(1, counter0, "Incorrect counter0 value after 10 seconds.");
Assert.AreEqual(2, counter1, "Incorrect counter1 value after next 5 seconds."); Assert.AreEqual(2, counter1, "Incorrect counter1 value after next 5 seconds.");
incCounter0.Cancel();
incCounter1.Cancel();
} }
[Test] [Test]
@ -329,63 +338,55 @@ namespace Tests {
} }
[Test] [Test]
public void TestTickWithNestedAddAndRaiseEvent() public void TestTickWithNestedAddAndRaiseEvent() {
{
var coroutineCreated = new Event(); var coroutineCreated = new Event();
var counterCoroutineA = 0; var counterCoroutineA = 0;
var counter = 0; var counter = 0;
CoroutineHandler.Start(OnCoroutineCreatedInfinite()); var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite());
CoroutineHandler.Start(OnEvent1()); CoroutineHandler.Start(OnEvent1());
CoroutineHandler.Tick(1); CoroutineHandler.Tick(1);
CoroutineHandler.Tick(1); CoroutineHandler.Tick(1);
CoroutineHandler.Tick(1); CoroutineHandler.Tick(1);
Assert.AreEqual(3, counter); Assert.AreEqual(3, counter);
Assert.AreEqual(2, counterCoroutineA); Assert.AreEqual(2, counterCoroutineA);
infinite.Cancel();
IEnumerator<Wait> OnCoroutineCreatedInfinite() IEnumerator<Wait> OnCoroutineCreatedInfinite() {
{ while (true) {
while (true)
{
yield return new Wait(coroutineCreated); yield return new Wait(coroutineCreated);
counterCoroutineA++; counterCoroutineA++;
} }
} }
IEnumerator<Wait> OnEvent1() IEnumerator<Wait> OnEvent1() {
{
yield return new Wait(1); yield return new Wait(1);
counter++; counter++;
CoroutineHandler.Start(OnEvent2()); CoroutineHandler.Start(OnEvent2());
CoroutineHandler.RaiseEvent(coroutineCreated); CoroutineHandler.RaiseEvent(coroutineCreated);
} }
IEnumerator<Wait> OnEvent2() IEnumerator<Wait> OnEvent2() {
{
yield return new Wait(1); yield return new Wait(1);
counter++; counter++;
CoroutineHandler.Start(OnEvent3()); CoroutineHandler.Start(OnEvent3());
CoroutineHandler.RaiseEvent(coroutineCreated); CoroutineHandler.RaiseEvent(coroutineCreated);
} }
IEnumerator<Wait> OnEvent3() IEnumerator<Wait> OnEvent3() {
{
yield return new Wait(1); yield return new Wait(1);
counter++; counter++;
} }
} }
[Test] [Test]
public void TestTickWithNestedAddAndRaiseEventOnFinish() public void TestTickWithNestedAddAndRaiseEventOnFinish() {
{
var onChildCreated = new Event(); var onChildCreated = new Event();
var onParentCreated = new Event(); var onParentCreated = new Event();
var counterAlwaysRunning = 0; var counterAlwaysRunning = 0;
IEnumerator<Wait> AlwaysRunning() IEnumerator<Wait> AlwaysRunning() {
{ while (true) {
while (true)
{
yield return new Wait(1); yield return new Wait(1);
counterAlwaysRunning++; counterAlwaysRunning++;
} }
@ -393,16 +394,14 @@ namespace Tests {
var counterChild = 0; var counterChild = 0;
IEnumerator<Wait> Child() IEnumerator<Wait> Child() {
{
yield return new Wait(1); yield return new Wait(1);
counterChild++; counterChild++;
} }
var counterParent = 0; var counterParent = 0;
IEnumerator<Wait> Parent() IEnumerator<Wait> Parent() {
{
yield return new Wait(1); yield return new Wait(1);
counterParent++; counterParent++;
// OnFinish I will start child. // OnFinish I will start child.
@ -410,8 +409,7 @@ namespace Tests {
var counterGrandParent = 0; var counterGrandParent = 0;
IEnumerator<Wait> GrandParent() IEnumerator<Wait> GrandParent() {
{
yield return new Wait(1); yield return new Wait(1);
counterGrandParent++; counterGrandParent++;
// Nested corotuine starting. // Nested corotuine starting.
@ -424,7 +422,7 @@ namespace Tests {
}; };
} }
CoroutineHandler.Start(AlwaysRunning()); var always = CoroutineHandler.Start(AlwaysRunning());
CoroutineHandler.Start(GrandParent()); CoroutineHandler.Start(GrandParent());
Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at event 0."); 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, counterGrandParent, "Grand Parent counter is invalid at event 0.");
@ -450,6 +448,8 @@ namespace Tests {
Assert.AreEqual(1, counterGrandParent, "Grand Parent 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, counterParent, "Parent counter is invalid at event 4.");
Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4."); Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4.");
always.Cancel();
} }
} }
} }