test(app): competing launches wait on a thread of their own
The single-instance test ran the second launch with Task.Run and waited on it. A task waited on before it starts may run inline on the waiting thread, which owns the instance mutex, and a mutex lets its owner in again at once, so the "nobody exits in time" check could see the launch get its turn. The launches now run on dedicated threads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
1 file changed
+22
-2
@@ -162,7 +162,7 @@ public sealed class ProjectFilesTests(TestContext context)
|
|||||||
Assert.Null(SingleInstance.TryAcquire(data));
|
Assert.Null(SingleInstance.TryAcquire(data));
|
||||||
|
|
||||||
// The next launch waits (the mutex belongs to a thread, so it waits on its own) and gets its turn once the first exits.
|
// The next launch waits (the mutex belongs to a thread, so it waits on its own) and gets its turn once the first exits.
|
||||||
var next = Task.Run(() =>
|
var next = OnOwnThread(() =>
|
||||||
{
|
{
|
||||||
using var instance = SingleInstance.WaitForTurn(data, TimeSpan.FromSeconds(20));
|
using var instance = SingleInstance.WaitForTurn(data, TimeSpan.FromSeconds(20));
|
||||||
return instance is not null;
|
return instance is not null;
|
||||||
@@ -175,7 +175,7 @@ public sealed class ProjectFilesTests(TestContext context)
|
|||||||
var holder = Assert.NotNull(SingleInstance.TryAcquire(data));
|
var holder = Assert.NotNull(SingleInstance.TryAcquire(data));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Assert.False(Task.Run(() => SingleInstance.WaitForTurn(data, TimeSpan.FromMilliseconds(200)) is { } got && Dispose(got)).Result);
|
Assert.False(OnOwnThread(() => SingleInstance.WaitForTurn(data, TimeSpan.FromMilliseconds(200)) is { } got && Dispose(got)).Result);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -187,6 +187,26 @@ public sealed class ProjectFilesTests(TestContext context)
|
|||||||
instance.Dispose();
|
instance.Dispose();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A thread of its own, never this one: a mutex this thread owns would let it in at once, and a task waited on
|
||||||
|
// before it started may run inline on the waiting thread.
|
||||||
|
static Task<bool> OnOwnThread(Func<bool> launch)
|
||||||
|
{
|
||||||
|
var done = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
new Thread(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
done.SetResult(launch());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
done.SetException(ex);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
{ IsBackground = true }.Start();
|
||||||
|
return done.Task;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Dialogs that pick one folder and cancel everything else.</summary>
|
/// <summary>Dialogs that pick one folder and cancel everything else.</summary>
|
||||||
|
|||||||
Reference in new issue
Block a user