Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions apps/swiftly/src/FiveStack.Utilities/TimerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions apps/swiftly/test/TimerUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Reflection.TargetInvocationException>(() =>
Fire(timer, () => throw new InvalidOperationException("fixture")));
Assert.IsType<InvalidOperationException>(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(
Expand Down
Loading