TestServer registers NoopHostLifetime to avoid hangs from not disposing (#23761)

This commit is contained in:
Brennan 2020-07-07 19:50:31 -07:00 committed by GitHub
parent 3709eda270
commit d0ab959c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.TestHost
{
internal class NoopHostLifetime : IHostLifetime
{
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

View File

@ -8,6 +8,7 @@ using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.TestHost
{
@ -17,6 +18,7 @@ namespace Microsoft.AspNetCore.TestHost
{
return builder.ConfigureServices(services =>
{
services.AddSingleton<IHostLifetime, NoopHostLifetime>();
services.AddSingleton<IServer, TestServer>();
});
}

View File

@ -79,6 +79,21 @@ namespace Microsoft.AspNetCore.TestHost
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task UseTestServerRegistersNoopHostLifetime()
{
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer()
.Configure(app => { });
})
.StartAsync();
Assert.IsType<NoopHostLifetime>(host.Services.GetService<IHostLifetime>());
}
[Fact]
public void CreateWithDelegate()
{