From 1e8a9e4e6cb4bf6fd79b03ef515d7267e7d77c34 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 20 Mar 2021 21:20:08 +0100 Subject: [PATCH] make sure all infinite coroutines are canceled in tests --- Tests/EventBasedCoroutineTests.cs | 22 +++++++---- Tests/TimeBasedCoroutineTests.cs | 66 +++++++++++++++---------------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Tests/EventBasedCoroutineTests.cs b/Tests/EventBasedCoroutineTests.cs index 7909ade..dcdd6cd 100644 --- a/Tests/EventBasedCoroutineTests.cs +++ b/Tests/EventBasedCoroutineTests.cs @@ -134,7 +134,7 @@ namespace Tests { }; } - CoroutineHandler.Start(AlwaysRunning()); + var always = CoroutineHandler.Start(AlwaysRunning()); 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."); @@ -164,6 +164,7 @@ namespace Tests { 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."); + always.Cancel(); } [Test] @@ -175,7 +176,7 @@ namespace Tests { var counterCoroutineA = 0; var counter = 0; - CoroutineHandler.Start(OnCoroutineCreatedInfinite()); + var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite()); CoroutineHandler.Start(OnEvent1()); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(event1); @@ -185,10 +186,10 @@ namespace Tests { CoroutineHandler.RaiseEvent(event3); Assert.AreEqual(3, counter); Assert.AreEqual(2, counterCoroutineA); + infinite.Cancel(); IEnumerator OnCoroutineCreatedInfinite() { - while (true) - { + while (true) { yield return new Wait(coroutineCreated); counterCoroutineA++; } @@ -259,14 +260,19 @@ namespace Tests { } const int highPriority = int.MaxValue; - CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority); - CoroutineHandler.Start(ShouldExecuteAfter()); - CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority); - CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1); + 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); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid."); Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid."); + + before1.Cancel(); + after.Cancel(); + before0.Cancel(); + @finally.Cancel(); } [Test] diff --git a/Tests/TimeBasedCoroutineTests.cs b/Tests/TimeBasedCoroutineTests.cs index d20255a..a13eb55 100644 --- a/Tests/TimeBasedCoroutineTests.cs +++ b/Tests/TimeBasedCoroutineTests.cs @@ -167,7 +167,7 @@ namespace Tests { p.OnFinished += ac => CoroutineHandler.Start(Child()); } - CoroutineHandler.Start(AlwaysRunning()); + var always = CoroutineHandler.Start(AlwaysRunning()); 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."); @@ -193,6 +193,7 @@ namespace Tests { 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."); + always.Cancel(); } [Test] @@ -239,13 +240,18 @@ namespace Tests { } const int highPriority = int.MaxValue; - CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority); - CoroutineHandler.Start(ShouldExecuteAfter()); - CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority); - CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1); + 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); CoroutineHandler.Tick(10); Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid."); Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid."); + + before1.Cancel(); + after.Cancel(); + before0.Cancel(); + @finally.Cancel(); } [Test] @@ -268,8 +274,8 @@ namespace Tests { } } - CoroutineHandler.Start(IncrementCounter0Ever10Seconds()); - CoroutineHandler.Start(IncrementCounter1Every5Seconds()); + var incCounter0 = CoroutineHandler.Start(IncrementCounter0Ever10Seconds()); + var incCounter1 = CoroutineHandler.Start(IncrementCounter1Every5Seconds()); CoroutineHandler.Tick(3); Assert.AreEqual(0, counter0, "Incorrect counter0 value after 3 seconds."); Assert.AreEqual(0, counter1, "Incorrect counter1 value after 3 seconds."); @@ -284,6 +290,9 @@ namespace Tests { CoroutineHandler.Tick(5); Assert.AreEqual(1, counter0, "Incorrect counter0 value after 10 seconds."); Assert.AreEqual(2, counter1, "Incorrect counter1 value after next 5 seconds."); + + incCounter0.Cancel(); + incCounter1.Cancel(); } [Test] @@ -329,63 +338,55 @@ namespace Tests { } [Test] - public void TestTickWithNestedAddAndRaiseEvent() - { + public void TestTickWithNestedAddAndRaiseEvent() { var coroutineCreated = new Event(); var counterCoroutineA = 0; var counter = 0; - CoroutineHandler.Start(OnCoroutineCreatedInfinite()); + var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite()); CoroutineHandler.Start(OnEvent1()); CoroutineHandler.Tick(1); CoroutineHandler.Tick(1); CoroutineHandler.Tick(1); Assert.AreEqual(3, counter); Assert.AreEqual(2, counterCoroutineA); + infinite.Cancel(); - IEnumerator OnCoroutineCreatedInfinite() - { - while (true) - { + IEnumerator OnCoroutineCreatedInfinite() { + while (true) { yield return new Wait(coroutineCreated); counterCoroutineA++; } } - IEnumerator OnEvent1() - { + IEnumerator OnEvent1() { yield return new Wait(1); counter++; CoroutineHandler.Start(OnEvent2()); CoroutineHandler.RaiseEvent(coroutineCreated); } - IEnumerator OnEvent2() - { + IEnumerator OnEvent2() { yield return new Wait(1); counter++; CoroutineHandler.Start(OnEvent3()); CoroutineHandler.RaiseEvent(coroutineCreated); } - IEnumerator OnEvent3() - { + IEnumerator OnEvent3() { yield return new Wait(1); counter++; } } [Test] - public void TestTickWithNestedAddAndRaiseEventOnFinish() - { + public void TestTickWithNestedAddAndRaiseEventOnFinish() { var onChildCreated = new Event(); var onParentCreated = new Event(); var counterAlwaysRunning = 0; - IEnumerator AlwaysRunning() - { - while (true) - { + IEnumerator AlwaysRunning() { + while (true) { yield return new Wait(1); counterAlwaysRunning++; } @@ -393,16 +394,14 @@ namespace Tests { var counterChild = 0; - IEnumerator Child() - { + IEnumerator Child() { yield return new Wait(1); counterChild++; } var counterParent = 0; - IEnumerator Parent() - { + IEnumerator Parent() { yield return new Wait(1); counterParent++; // OnFinish I will start child. @@ -410,8 +409,7 @@ namespace Tests { var counterGrandParent = 0; - IEnumerator GrandParent() - { + IEnumerator GrandParent() { yield return new Wait(1); counterGrandParent++; // Nested corotuine starting. @@ -424,7 +422,7 @@ namespace Tests { }; } - CoroutineHandler.Start(AlwaysRunning()); + var always = CoroutineHandler.Start(AlwaysRunning()); 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."); @@ -450,6 +448,8 @@ namespace Tests { 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."); + always.Cancel(); } + } } \ No newline at end of file