Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public async Task BackgroundService_AsynchronousException_StopAsync_ThrowsExcept
using var host = builder.Build();
await host.StartAsync();

// Wait for the background service to fail
await Task.Delay(TimeSpan.FromMilliseconds(200));
// Wait for the host to react to the background service failure
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var stoppingTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
lifetime.ApplicationStopping.Register(() => stoppingTcs.TrySetResult());
await stoppingTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));

await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
Expand Down Expand Up @@ -107,8 +110,11 @@ public async Task BackgroundService_AsynchronousException_StopTwiceAsync_ThrowsE
using var host = builder.Build();
await host.StartAsync();

// Wait for the background service to fail
await Task.Delay(TimeSpan.FromMilliseconds(200));
// Wait for the host to react to the background service failure
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var stoppingTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
lifetime.ApplicationStopping.Register(() => stoppingTcs.TrySetResult());
await stoppingTcs.Task.WaitAsync(TimeSpan.FromSeconds(10));

await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
Expand Down Expand Up @@ -208,6 +214,7 @@ public async Task BackgroundService_IgnoreException_DoesNotThrow()
[Fact]
public async Task BackgroundService_IgnoreException_StopAsync_DoesNotThrow()
{
var signal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var builder = new HostBuilder()
.ConfigureServices(services =>
{
Expand All @@ -216,14 +223,15 @@ public async Task BackgroundService_IgnoreException_StopAsync_DoesNotThrow()
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
options.ShutdownTimeout = TimeSpan.FromSeconds(1);
});
services.AddHostedService<AsynchronousFailureService>();
services.AddSingleton(signal);
services.AddHostedService<SignalingFailureService>();
});

using var host = builder.Build();
await host.StartAsync();

// Wait a bit for the background service to fail
await Task.Delay(TimeSpan.FromMilliseconds(200));
// Wait for the background service to reach its failure point
await signal.Task.WaitAsync(TimeSpan.FromSeconds(10));

await host.StopAsync();
}
Expand Down Expand Up @@ -294,6 +302,23 @@ private class StopFailureService : IHostedService
public Task StopAsync(CancellationToken cancellationToken) => throw new InvalidOperationException("Stop failure");
}

private class SignalingFailureService : BackgroundService
{
private readonly TaskCompletionSource _signal;

public SignalingFailureService(TaskCompletionSource signal)
{
_signal = signal;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(TimeSpan.FromMilliseconds(100));
_signal.TrySetResult();
throw new InvalidOperationException("Signaling asynchronous failure");
}
}

private class SuccessfulService : BackgroundService
{
private readonly IHostApplicationLifetime _lifetime;
Expand Down
Loading