diff --git a/apps/swiftly/src/FiveStack.Utilities/TimerUtility.cs b/apps/swiftly/src/FiveStack.Utilities/TimerUtility.cs index 78d21dec..7cc9c34a 100644 --- a/apps/swiftly/src/FiveStack.Utilities/TimerUtility.cs +++ b/apps/swiftly/src/FiveStack.Utilities/TimerUtility.cs @@ -28,22 +28,33 @@ public static CancellationTokenSource AddTimer(float interval, Action callback) if (TooShortToDelay(interval)) { CancellationTokenSource immediate = new CancellationTokenSource(); - _core!.Scheduler.NextTick(() => - { - if (!immediate.IsCancellationRequested) - { - callback(); - } - }); Timers.Add(immediate); + _core!.Scheduler.NextTick(() => RunOnce(immediate, callback)); return immediate; } - CancellationTokenSource timer = _core!.Scheduler.DelayBySeconds(interval, callback); + CancellationTokenSource? timer = null; + timer = _core!.Scheduler.DelayBySeconds(interval, () => RunOnce(timer!, callback)); Timers.Add(timer); return timer; } + private static void RunOnce(CancellationTokenSource timer, Action callback) + { + try + { + if (!timer.IsCancellationRequested) + { + callback(); + } + } + finally + { + // Scheduler tokens are owned by SwiftlyS2; only forget our reference. + Timers.Remove(timer); + } + } + public static CancellationTokenSource Repeat(float interval, Action callback) { CancellationTokenSource? timer = null; diff --git a/apps/swiftly/test/TimerUtilityTests.cs b/apps/swiftly/test/TimerUtilityTests.cs index 3c62f2ad..0c5bf0ee 100644 --- a/apps/swiftly/test/TimerUtilityTests.cs +++ b/apps/swiftly/test/TimerUtilityTests.cs @@ -4,6 +4,50 @@ public class TimerUtilityTests { + private static void Fire(CancellationTokenSource timer, Action callback) + { + typeof(TimerUtility).GetMethod("RunOnce", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)! + .Invoke(null, new object[] { timer, callback }); + } + + [Fact] + public void Completed_Timers_Are_Forgotten_Even_When_Their_Callback_Throws() + { + using var timer = new CancellationTokenSource(); + TimerUtility.Timers.Add(timer); + var error = Assert.Throws(() => + Fire(timer, () => throw new InvalidOperationException("fixture"))); + Assert.IsType(error.InnerException); + Assert.DoesNotContain(timer, TimerUtility.Timers); + } + + [Fact] + public void Completed_Timers_Release_Only_Their_Own_Registry_Entry() + { + using var completed = new CancellationTokenSource(); + using var waiting = new CancellationTokenSource(); + TimerUtility.Timers.Add(completed); + TimerUtility.Timers.Add(waiting); + try + { + int calls = 0; + Fire(completed, () => calls++); + Assert.Equal(1, calls); + Assert.DoesNotContain(completed, TimerUtility.Timers); + Assert.Contains(waiting, TimerUtility.Timers); + } + finally { TimerUtility.Kill(waiting); } + } + + [Fact] + public void Canceled_One_Shot_Callbacks_Do_Not_Run_And_Are_Forgotten() + { + using var timer = new CancellationTokenSource(); + TimerUtility.Timers.Add(timer); + timer.Cancel(); + Fire(timer, () => throw new InvalidOperationException("must not run")); + Assert.DoesNotContain(timer, TimerUtility.Timers); + } private static string Source([CallerFilePath] string testFile = "") { return File.ReadAllText(