Skip to content
Merged
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
17 changes: 14 additions & 3 deletions src/WorkTracker.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,13 @@ internal void ShutdownCleanup(TimeSpan timeout)

_host.Services.GetRequiredService<MainViewModel>().Dispose();

var pomodoro = _host.Services.GetRequiredService<IPomodoroService>();
var pluginManager = _host.Services.GetRequiredService<IPluginManager>();
var host = _host;
var pluginStartup = _pluginStartup;
var teardown = Task.Run(async () =>
{
// Disposing plugins that are still starting would leave them half set up
await pluginStartup;
await pluginManager.DisposeAsync();
await StopPomodoroAndPluginsAsync(pluginStartup, pomodoro, pluginManager);
await host.StopAsync();
});

Expand All @@ -421,6 +420,18 @@ internal void ShutdownCleanup(TimeSpan timeout)
}
}

/// <summary>
/// Disposing plugins that are still starting would leave them half set up. The Pomodoro goes before
/// the plugins: its timer must not reach an indicator that is shutting down, and once the plugins are
/// gone it has no indicator left to turn off.
/// </summary>
internal static async Task StopPomodoroAndPluginsAsync(Task pluginStartup, IPomodoroService pomodoro, IPluginManager pluginManager)
{
await pluginStartup;
await pomodoro.DisposeAsync();
await pluginManager.DisposeAsync();
}

private void LogErrorSafe(Exception ex, string message)
{
try
Expand Down
7 changes: 7 additions & 0 deletions src/WorkTracker.Avalonia/Services/PomodoroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ public async ValueTask DisposeAsync()
_pluginManager.PluginStarted -= OnPluginStarted;
StopTimer();

// Outside a Pomodoro there is nothing to turn off, and asking would open a device nothing has used
if (_currentPhase == PomodoroPhase.Idle)
{
GC.SuppressFinalize(this);
return;
}

// Best-effort: turn off indicators with a timeout to avoid blocking app shutdown
try
{
Expand Down
42 changes: 42 additions & 0 deletions tests/WorkTracker.Avalonia.Tests/AppShutdownTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentAssertions;
using Moq;
using WorkTracker.Application.Plugins;
using WorkTracker.Avalonia.Services;

namespace WorkTracker.Avalonia.Tests;

public class AppShutdownTests
{
private readonly List<string> _steps = [];
private readonly Mock<IPomodoroService> _pomodoro = new();
private readonly Mock<IPluginManager> _pluginManager = new();

public AppShutdownTests()
{
_pomodoro.Setup(p => p.DisposeAsync()).Callback(() => _steps.Add("pomodoro")).Returns(ValueTask.CompletedTask);
_pluginManager.Setup(p => p.DisposeAsync()).Callback(() => _steps.Add("plugins")).Returns(ValueTask.CompletedTask);
}

[Fact]
public async Task StopPomodoroAndPlugins_StopsThePomodoroWhileTheIndicatorsAreStillRunning()
{
await App.StopPomodoroAndPluginsAsync(Task.CompletedTask, _pomodoro.Object, _pluginManager.Object);

_steps.Should().Equal("pomodoro", "plugins");
}

[Fact]
public async Task StopPomodoroAndPlugins_WaitsForPluginsStillStarting()
{
var pluginStartup = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

var stopping = App.StopPomodoroAndPluginsAsync(pluginStartup.Task, _pomodoro.Object, _pluginManager.Object);

_steps.Should().BeEmpty();

pluginStartup.SetResult();
await stopping;

_steps.Should().Equal("pomodoro", "plugins");
}
}
19 changes: 19 additions & 0 deletions tests/WorkTracker.Avalonia.Tests/Services/PomodoroServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,25 @@ public void Skip_ToShortBreak_SetsStatusIndicatorToShortBreak()
_statusIndicatorPlugin.Verify(p => p.SetStateAsync(StatusIndicatorState.ShortBreak, It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task Dispose_DuringAPomodoro_TurnsTheIndicatorOff()
{
_sut.Start();
_statusIndicatorPlugin.Invocations.Clear();

await _sut.DisposeAsync();

_statusIndicatorPlugin.Verify(p => p.SetStateAsync(StatusIndicatorState.Idle, It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task Dispose_OutsideAPomodoro_LeavesTheIndicatorAlone()
{
await _sut.DisposeAsync();

_statusIndicatorPlugin.Verify(p => p.SetStateAsync(It.IsAny<StatusIndicatorState>(), It.IsAny<CancellationToken>()), Times.Never);
}

[Fact]
public void Stop_SetsStatusIndicatorToIdle()
{
Expand Down
Loading