Skip to content

Commit

Permalink
Don't throw from hosted services (#5699)
Browse files Browse the repository at this point in the history
- Shutting down the host cancels the hosted services and they shouldn't throw as a result. This reduces noise in tests as they call stop on the host on failure resulting in more exceptions than necessary
  • Loading branch information
davidfowl authored Sep 13, 2024
1 parent b5da17b commit fcbbf81
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async Task WatchNotifications(CancellationToken cancellationToken)

await Task.WhenAll(logWatchTasks).ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// this was expected as the token was canceled
}
Expand Down
21 changes: 14 additions & 7 deletions src/Aspire.Hosting/Health/ResourceHealthCheckScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ public ResourceHealthCheckScheduler(ResourceNotificationService resourceNotifica

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var resourceEvents = _resourceNotificationService.WatchAsync(stoppingToken);

await foreach (var resourceEvent in resourceEvents.ConfigureAwait(false))
try
{
if (resourceEvent.Snapshot.State?.Text == KnownResourceStates.Running)
var resourceEvents = _resourceNotificationService.WatchAsync(stoppingToken);

await foreach (var resourceEvent in resourceEvents.ConfigureAwait(false))
{
// Each time we receive an event that tells us that the resource is
// running we need to enable the health check annotation.
UpdateCheckEnablement(resourceEvent.Resource, true);
if (resourceEvent.Snapshot.State?.Text == KnownResourceStates.Running)
{
// Each time we receive an event that tells us that the resource is
// running we need to enable the health check annotation.
UpdateCheckEnablement(resourceEvent.Resource, true);
}
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
// This was expected as the token was canceled
}
}

private void UpdateCheckEnablement(IResource resource, bool enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task BackgroundServiceIsRegisteredInServiceProvider()
}

[Fact]
public async Task ExecuteThrowsOperationCanceledWhenAppStoppingTokenSignaled()
public async Task ExecuteDoesNotThrowOperationCanceledWhenAppStoppingTokenSignaled()
{
var hostApplicationLifetime = new TestHostApplicationLifetime();
var resourceNotificationService = new ResourceNotificationService(NullLogger<ResourceNotificationService>.Instance, hostApplicationLifetime);
Expand All @@ -44,10 +44,7 @@ public async Task ExecuteThrowsOperationCanceledWhenAppStoppingTokenSignaled()
// Signal the stopping token
hostApplicationLifetime.StopApplication();

await Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
await resourceLogForwarder.ExecuteTask;
});
await resourceLogForwarder.ExecuteTask;
}

[Fact]
Expand Down

0 comments on commit fcbbf81

Please sign in to comment.