TestServer registers NoopHostLifetime to avoid hangs from not disposing (#23761)
This commit is contained in:
parent
3709eda270
commit
d0ab959c89
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue