From 4d4264fb1c00eda2afcc73150352b75847445641 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Tue, 27 Jan 2026 00:47:57 +0000 Subject: [PATCH] fix: make thread assertion tests deterministic Replace Thread.Sleep() with ManualResetEventSlim synchronization to ensure threads stay alive until assertions complete. This fixes flaky test failures where ThreadStateException was thrown because the thread completed before IsThreadPoolThread could be accessed. Co-Authored-By: Claude Opus 4.5 --- .../ThreadAssertionTests.cs | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/TUnit.Assertions.Tests/ThreadAssertionTests.cs b/TUnit.Assertions.Tests/ThreadAssertionTests.cs index 358d4f3c04..15b8c59310 100644 --- a/TUnit.Assertions.Tests/ThreadAssertionTests.cs +++ b/TUnit.Assertions.Tests/ThreadAssertionTests.cs @@ -1,5 +1,4 @@ using TUnit.Assertions.Extensions; -using TUnit.Assertions.Extensions; namespace TUnit.Assertions.Tests; @@ -15,17 +14,17 @@ public async Task Test_Thread_IsAlive() [Test] public async Task Test_Thread_IsAlive_NewThread() { - var signal = new ManualResetEventSlim(false); - Thread? thread = null; + var startSignal = new ManualResetEventSlim(false); + var completeSignal = new ManualResetEventSlim(false); - thread = new Thread(() => + var thread = new Thread(() => { - signal.Set(); - Thread.Sleep(100); + startSignal.Set(); + completeSignal.Wait(); }); thread.Start(); - signal.Wait(); + startSignal.Wait(); try { @@ -33,6 +32,7 @@ public async Task Test_Thread_IsAlive_NewThread() } finally { + completeSignal.Set(); thread.Join(); } } @@ -61,12 +61,20 @@ public async Task Test_Thread_IsBackground() [Test] public async Task Test_Thread_IsBackground_Started() { - var thread = new Thread(() => Thread.Sleep(100)) + var startSignal = new ManualResetEventSlim(false); + var completeSignal = new ManualResetEventSlim(false); + + var thread = new Thread(() => + { + startSignal.Set(); + completeSignal.Wait(); + }) { IsBackground = true }; thread.Start(); + startSignal.Wait(); try { @@ -74,6 +82,7 @@ public async Task Test_Thread_IsBackground_Started() } finally { + completeSignal.Set(); thread.Join(); } } @@ -129,8 +138,17 @@ public async Task Test_Thread_IsNotThreadPoolThread() [Test] public async Task Test_Thread_IsNotThreadPoolThread_NewThread() { - var thread = new Thread(() => Thread.Sleep(50)); + var startSignal = new ManualResetEventSlim(false); + var completeSignal = new ManualResetEventSlim(false); + + var thread = new Thread(() => + { + startSignal.Set(); + completeSignal.Wait(); + }); + thread.Start(); + startSignal.Wait(); try { @@ -138,6 +156,7 @@ public async Task Test_Thread_IsNotThreadPoolThread_NewThread() } finally { + completeSignal.Set(); thread.Join(); } }