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
40 changes: 38 additions & 2 deletions TrayApp.Test/ShutdownSignalsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,53 @@ public void ARepeatedTermination_IsNotCancelled()
public void ASignalAfterQuit_IsNotCancelled()
{
using TrayApplication host = Host();
int endedRuns = 0;
host.EndRun = () => endedRuns++;
using ShutdownSignals signals = new(() => host);

// The Quit item (or anything else) already posted the shutdown.
Assert.IsTrue(host.RequestShutdown());
// The path the real Quit item takes. Its stop action may hang, and the next signal has to end the
// process rather than queue a second shutdown behind it.
host.OnQuitRequested(null, EventArgs.Empty);

PosixSignalContext context = new(PosixSignal.SIGHUP);
signals.OnTermination(context);

Assert.AreEqual(1, endedRuns);
Assert.IsFalse(context.Cancel);
}

[TestMethod]
public void ASignalAfterTheDurationExpires_IsNotCancelled()
{
using TrayApplication host = Host();
int endedRuns = 0;
host.EndRun = () => endedRuns++;
using ShutdownSignals signals = new(() => host);

// What the --for timer does when the window ends; under systemd the next SIGTERM is `systemctl stop`.
host.OnDurationExpired();

PosixSignalContext context = new(PosixSignal.SIGTERM);
signals.OnTermination(context);

Assert.AreEqual(1, endedRuns);
Assert.IsFalse(context.Cancel);
}

[TestMethod]
public void QuitAfterASignal_DoesNotEndTheRunAgain()
{
using TrayApplication host = Host();
int endedRuns = 0;
host.EndRun = () => endedRuns++;

// The signal's shutdown is already posted, so Quit has nothing left to start.
Assert.IsTrue(host.RequestShutdown());
host.OnQuitRequested(null, EventArgs.Empty);

Assert.AreEqual(0, endedRuns);
}

[TestMethod]
public void RequestShutdown_PostsOnlyOnce()
{
Expand Down
51 changes: 44 additions & 7 deletions TrayApp/Tray/TrayApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ internal TrayApplication(TrayAppDefinition app, TrayMenu menu, TimeSpan? duratio
this.menu = menu;
this.duration = duration;
this.onToggled = onToggled;
EndRun = Shutdown;

BuildNativeMenu();
}

/// <summary>
/// Gets or sets what ends the run once a shutdown has been claimed.
/// </summary>
/// <remarks>
/// The lifetime's shutdown, or the dispatcher's when there is no lifetime. Settable only so the tests can
/// drive Quit and the <c>--for</c> expiry without shutting down the test host's dispatcher.
/// </remarks>
internal Action EndRun { get; set; }

/// <summary>
/// Gets the native menu the model was projected onto.
/// </summary>
Expand Down Expand Up @@ -249,7 +259,38 @@ internal void OnTrayIconClicked(object? sender, EventArgs e)
}
}

private void OnQuitRequested(object? sender, EventArgs e) => Shutdown();
/// <summary>
/// Ends the run for the Quit item.
/// </summary>
/// <remarks>
/// Internal so the tests can drive the path the real Quit item takes, which needs a platform to wire up.
/// </remarks>
internal void OnQuitRequested(object? sender, EventArgs e) => ShutdownFromUIThread();

/// <summary>
/// Ends the run for the <c>--for</c> expiry.
/// </summary>
internal void OnDurationExpired()
{
expiryTimer?.Stop();
ShutdownFromUIThread();
}

/// <summary>
/// Claims the shutdown and ends the run, from the UI thread that Quit and the expiry are raised on.
/// </summary>
/// <remarks>
/// Every path that ends the run has to claim <see cref="shutdownRequested"/>, not only a signal's. A stop
/// action that hangs after Quit or the expiry is still a stop under way, so the next Ctrl+C or
/// <c>SIGTERM</c> must get its default handling and end the process, as it does in the console mode.
/// </remarks>
private void ShutdownFromUIThread()
{
if (Interlocked.Exchange(ref shutdownRequested, 1) == 0)
{
EndRun();
}
}

/// <summary>
/// Ends the run the way the Quit item does, from any thread.
Expand All @@ -269,7 +310,7 @@ internal bool RequestShutdown()
return false;
}

Dispatcher.UIThread.Post(Shutdown);
Dispatcher.UIThread.Post(() => EndRun());
return true;
}

Expand Down Expand Up @@ -317,11 +358,7 @@ private void ApplyDuration()
}

expiryTimer = new DispatcherTimer { Interval = window };
expiryTimer.Tick += (_, _) =>
{
expiryTimer.Stop();
Shutdown();
};
expiryTimer.Tick += (_, _) => OnDurationExpired();

expiryTimer.Start();
}
Expand Down
Loading