From b7d71175e9522a9d9c0ac59208ba92b02158c5c1 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 19 Nov 2020 12:25:51 -0600 Subject: [PATCH 1/2] Increase Hosting test delay to fix flaky BackgroundServiceAsyncExceptionGetsLogged test. Fix #43389 --- .../tests/UnitTests/Internal/HostTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs index 80653a63533f3..ff43cefbe6c58 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs @@ -1372,7 +1372,9 @@ private class AsyncThrowingService : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await Task.Yield(); + // Delay long enough that BackgroundService.StartAsync gets a chance to complete + // before the exception is thrown. But don't delay for too long so the test completes quickly. + await Task.Delay(50); throw new Exception("Background Exception"); } From ca52decd65e335619458928b6967574ddb073fc3 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 19 Nov 2020 15:13:41 -0600 Subject: [PATCH 2/2] Use a Task to control the delay on the background service. --- .../tests/UnitTests/Internal/HostTests.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs index ff43cefbe6c58..f7ce5afc57ca7 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs @@ -1227,6 +1227,7 @@ public void ThrowExceptionForCustomImplementationOfIHostApplicationLifetime() public async Task BackgroundServiceAsyncExceptionGetsLogged() { using TestEventListener listener = new TestEventListener(); + var backgroundDelayTaskSource = new TaskCompletionSource(); using IHost host = CreateBuilder() .ConfigureLogging(logging => @@ -1235,10 +1236,12 @@ public async Task BackgroundServiceAsyncExceptionGetsLogged() }) .ConfigureServices((hostContext, services) => { - services.AddHostedService(); + 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(); @@ -1370,11 +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) { - // Delay long enough that BackgroundService.StartAsync gets a chance to complete - // before the exception is thrown. But don't delay for too long so the test completes quickly. - await Task.Delay(50); + await _executeDelayTask; throw new Exception("Background Exception"); }