Files
DodoSSH/tests/DodoSSH.Client.App.Tests/ProbedDeviceKeyStoreTests.cs
T
jaap-jan e240c0cf26
ci / build and test (push) Successful in 3m25s
ci / android head (push) Successful in 3m55s
ci / desktop nightly (push) Failing after 37s
ci / api image (push) Successful in 23s
Tests: prove the device-key probe runs off the caller's thread by blocking it, not by comparing thread ids
TheChoiceIsMadeOnce_OffTheCallersThread failed on CI with the code doing the right thing. It
compared the probe's managed thread id with the test's, but the test runs on a pool thread, and that
same thread is free to pick up the queued probe as soon as the test awaits.

The probe now waits on a release the test only gives once construction has returned. A probe run on
the constructing thread could never be released, and the test fails after ten seconds instead of
passing by luck. Twenty runs pass; an inline probe fails it.
2026-10-03 19:50:25 +02:00

68 lines
2.6 KiB
C#

using DodoSSH.Client.App.Platform;
using DodoSSH.Client.Session;
using NSubstitute;
namespace DodoSSH.Client.App.Tests;
/// <summary>
/// That the slow probe choosing a device key store runs once, and not on the thread that asked.
/// </summary>
/// <remarks>
/// On Windows the probe creates and deletes a TPM key, and it used to run synchronously on the UI thread at
/// startup and again after every unlock. See <see cref="ProbedDeviceKeyStore"/>.
/// </remarks>
public sealed class ProbedDeviceKeyStoreTests
{
private static CancellationToken Token => TestContext.Current.CancellationToken;
/// <remarks>
/// "Off the caller's thread" is proved by blocking, not by comparing thread ids. The probe waits for a
/// release the test only gives once construction has returned, so a probe run on the thread that
/// constructed the store could never be released and times out instead. Thread ids proved nothing: the
/// test itself runs on a pool thread, and that same thread is free to pick up the queued probe the
/// moment the test awaits, which is what failed on CI with the code doing exactly the right thing.
/// </remarks>
[Fact]
public async Task TheChoiceIsMadeOnce_OffTheCallersThread()
{
var probes = 0;
var releasedInTime = false;
using var release = new ManualResetEventSlim();
var inner = Substitute.For<IDeviceKeyStore>();
var store = new ProbedDeviceKeyStore(() =>
{
Interlocked.Increment(ref probes);
releasedInTime = release.Wait(TimeSpan.FromSeconds(10));
return inner;
});
var available = store.IsAvailableAsync(Token);
available.IsCompleted.ShouldBeFalse("nothing has released the probe yet");
release.Set();
(await available).ShouldBeTrue();
(await store.IsAvailableAsync(Token)).ShouldBeTrue();
await store.TryLoadAsync(Token);
releasedInTime.ShouldBeTrue("the probe ran on the thread that asked, so nothing could release it");
probes.ShouldBe(1);
// Availability is the choice, not a second probe through the store it chose.
await inner.DidNotReceive().IsAvailableAsync(Arg.Any<CancellationToken>());
await inner.Received(1).TryLoadAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task AMachineWithNoStore_IsUnavailable_AndForgettingIsHarmless()
{
var store = new ProbedDeviceKeyStore(static () => new UnavailableDeviceKeyStore());
(await store.IsAvailableAsync(Token)).ShouldBeFalse();
(await store.TryLoadAsync(Token)).ShouldBeNull();
await store.ForgetAsync(Token);
}
}