Skip to content

Commit 620997e

Browse files
CopilotdavidfowlCopilot
authored
Fix timeout handling in DcpHostNotificationTests to prevent test hangs (#12152)
* Initial plan * Fix timeout handling in DcpHostNotificationTests to prevent test hangs Co-authored-by: davidfowl <[email protected]> * Keep CancellationTokenSource and add DefaultTimeout to ReadAsync operations Co-authored-by: davidfowl <[email protected]> * Move DefaultTimeout to EnsureDcpContainerRuntimeAsync and other task-returning calls Co-authored-by: davidfowl <[email protected]> * Update tests/Aspire.Hosting.Tests/Dcp/DcpHostNotificationTests.cs * Update tests/Aspire.Hosting.Tests/Dcp/DcpHostNotificationTests.cs Co-authored-by: Copilot <[email protected]> * Update tests/Aspire.Hosting.Tests/Dcp/DcpHostNotificationTests.cs * Update tests/Aspire.Hosting.Tests/Dcp/DcpHostNotificationTests.cs --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: davidfowl <[email protected]> Co-authored-by: David Fowler <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent c687176 commit 620997e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tests/Aspire.Hosting.Tests/Dcp/DcpHostNotificationTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Globalization;
55
using Aspire.Hosting.Dcp;
66
using Aspire.Hosting.Resources;
7+
using Microsoft.AspNetCore.InternalTesting;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.Logging.Abstractions;
910
using Microsoft.Extensions.Options;
@@ -76,7 +77,7 @@ public async Task DcpHost_WithUnhealthyContainerRuntime_ShowsNotification()
7677
timeProvider);
7778

7879
// Act
79-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
80+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
8081

8182
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
8283
var interaction = await interactionService.Interactions.Reader.ReadAsync(cts.Token);
@@ -125,7 +126,7 @@ public async Task DcpHost_WithHealthyContainerRuntime_DoesNotShowNotification()
125126
timeProvider);
126127

127128
// Act
128-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
129+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
129130

130131
// Use a short timeout to check that no notification is sent
131132
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
@@ -180,7 +181,7 @@ public async Task DcpHost_WithDashboardDisabled_DoesNotShowNotification()
180181
timeProvider);
181182

182183
// Act
183-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
184+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
184185

185186
// Use a short timeout to check that no notification is sent
186187
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
@@ -235,7 +236,7 @@ public async Task DcpHost_WithPodmanUnhealthy_ShowsCorrectMessage()
235236
timeProvider);
236237

237238
// Act
238-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
239+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
239240

240241
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
241242
var interaction = await interactionService.Interactions.Reader.ReadAsync(cts.Token);
@@ -285,7 +286,7 @@ public async Task DcpHost_WithUnhealthyContainerRuntime_NotificationCancelledWhe
285286
timeProvider);
286287

287288
// Act
288-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
289+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
289290

290291
// Use ReadAsync with timeout to wait for the notification
291292
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
@@ -310,7 +311,11 @@ public async Task DcpHost_WithUnhealthyContainerRuntime_NotificationCancelledWhe
310311
timeProvider.Advance(TimeSpan.FromSeconds(5));
311312

312313
// Assert - The notification should now be cancelled
313-
await Assert.ThrowsAsync<TaskCanceledException>(() => Task.Delay(-1, interaction.CancellationToken));
314+
using (var testTimeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
315+
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(interaction.CancellationToken, testTimeoutCts.Token))
316+
{
317+
await Assert.ThrowsAsync<TaskCanceledException>(() => Task.Delay(-1, linkedCts.Token));
318+
}
314319
}
315320

316321
[Fact]
@@ -349,7 +354,7 @@ public async Task DcpHost_WithContainerRuntimeNotInstalled_ShowsNotificationWith
349354
timeProvider);
350355

351356
// Act
352-
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None);
357+
await dcpHost.EnsureDcpContainerRuntimeAsync(CancellationToken.None).DefaultTimeout();
353358

354359
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
355360
var interaction = await interactionService.Interactions.Reader.ReadAsync(cts.Token);

0 commit comments

Comments
 (0)