diff --git a/test/PatternKit.Generators.Tests/ObserverGeneratorTests.cs b/test/PatternKit.Generators.Tests/ObserverGeneratorTests.cs index c935477..cdfd1a3 100644 --- a/test/PatternKit.Generators.Tests/ObserverGeneratorTests.cs +++ b/test/PatternKit.Generators.Tests/ObserverGeneratorTests.cs @@ -299,7 +299,8 @@ public static async System.Threading.Tasks.Task Run() var demoType = asm.GetType("PatternKit.Examples.Generators.Demo"); var runMethod = demoType!.GetMethod("Run"); var task = (System.Threading.Tasks.Task)runMethod!.Invoke(null, null)!; - task.Wait(); + if (!task.Wait(TimeSpan.FromSeconds(30))) + throw new TimeoutException("Demo.Run() did not complete within 30 seconds."); var result = task.Result; Assert.Equal("AsyncHandler:42", result); } @@ -629,7 +630,7 @@ public static async System.Threading.Tasks.Task Run() evt.Publish(new Temperature(10)); // Wait deterministically for async handler to complete - await tcs.Task.WaitAsync(System.TimeSpan.FromSeconds(5)); + await tcs.Task.WaitAsync(System.TimeSpan.FromSeconds(30)); return string.Join("|", log); } @@ -655,7 +656,8 @@ public static async System.Threading.Tasks.Task Run() var demoType = asm.GetType("PatternKit.Examples.Generators.Demo"); var runMethod = demoType!.GetMethod("Run"); var task = (System.Threading.Tasks.Task)runMethod!.Invoke(null, null)!; - task.Wait(); + if (!task.Wait(TimeSpan.FromSeconds(30))) + throw new TimeoutException("Demo.Run() did not complete within 30 seconds."); var result = task.Result; // Both handlers should have been invoked diff --git a/test/PatternKit.Tests/Behavioral/AsyncTemplateMethodTests.cs b/test/PatternKit.Tests/Behavioral/AsyncTemplateMethodTests.cs index 25393b9..a848052 100644 --- a/test/PatternKit.Tests/Behavioral/AsyncTemplateMethodTests.cs +++ b/test/PatternKit.Tests/Behavioral/AsyncTemplateMethodTests.cs @@ -89,8 +89,10 @@ public Task Serializes_When_Synchronized() [Fact] public async Task Cancellation_Observed() { - var template = new SampleAsyncTemplate(delayMs: 100); - using var cts = new CancellationTokenSource(10); + // Use a long delay with a pre-cancelled token to avoid timing races + var template = new SampleAsyncTemplate(delayMs: 5000); + using var cts = new CancellationTokenSource(); + cts.Cancel(); // pre-cancel so cancellation is immediate and deterministic // Assert.ThrowsAnyAsync verifies that OperationCanceledException or derived types are thrown await Assert.ThrowsAnyAsync(