Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase Hosting test delay to fix flaky BackgroundServiceAsyncExceptionGetsLogged test. #44953

Merged
merged 2 commits into from
Nov 20, 2020
Merged
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 @@ -1227,6 +1227,7 @@ public void ThrowExceptionForCustomImplementationOfIHostApplicationLifetime()
public async Task BackgroundServiceAsyncExceptionGetsLogged()
{
using TestEventListener listener = new TestEventListener();
var backgroundDelayTaskSource = new TaskCompletionSource<bool>();

using IHost host = CreateBuilder()
.ConfigureLogging(logging =>
Expand All @@ -1235,10 +1236,12 @@ public async Task BackgroundServiceAsyncExceptionGetsLogged()
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<AsyncThrowingService>();
services.AddHostedService(sp => new AsyncThrowingService(backgroundDelayTaskSource.Task));
})
.Start();

backgroundDelayTaskSource.SetResult(true);

// give the background service 1 minute to log the failure
TimeSpan timeout = TimeSpan.FromMinutes(1);
Stopwatch sw = Stopwatch.StartNew();
Expand Down Expand Up @@ -1370,9 +1373,16 @@ public ValueTask DisposeAsync()

private class AsyncThrowingService : BackgroundService
{
private readonly Task _executeDelayTask;

public AsyncThrowingService(Task executeDelayTask)
{
_executeDelayTask = executeDelayTask;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Yield();
await _executeDelayTask;

throw new Exception("Background Exception");
}
Expand Down