diff --git a/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs b/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs index 098c017f..526967dc 100644 --- a/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs +++ b/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs @@ -162,7 +162,7 @@ public sealed class ProjectFilesTests(TestContext context) 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. - var next = Task.Run(() => + var next = OnOwnThread(() => { using var instance = SingleInstance.WaitForTurn(data, TimeSpan.FromSeconds(20)); return instance is not null; @@ -175,7 +175,7 @@ public sealed class ProjectFilesTests(TestContext context) var holder = Assert.NotNull(SingleInstance.TryAcquire(data)); 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 { @@ -187,6 +187,26 @@ public sealed class ProjectFilesTests(TestContext context) instance.Dispose(); 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 OnOwnThread(Func launch) + { + var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + new Thread(() => + { + try + { + done.SetResult(launch()); + } + catch (Exception ex) + { + done.SetException(ex); + } + }) + { IsBackground = true }.Start(); + return done.Task; + } } /// Dialogs that pick one folder and cancel everything else.