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

TestServer registers NoopHostLifetime to avoid hangs from not disposing #23761

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/Hosting/TestHost/src/NoopHostLifetime.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 2 additions & 0 deletions src/Hosting/TestHost/src/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNetCore.TestHost
{
Expand All @@ -17,6 +18,7 @@ public static IWebHostBuilder UseTestServer(this IWebHostBuilder builder)
{
return builder.ConfigureServices(services =>
{
services.AddSingleton<IHostLifetime, NoopHostLifetime>();
services.AddSingleton<IServer, TestServer>();
});
}
Expand Down
15 changes: 15 additions & 0 deletions src/Hosting/TestHost/test/TestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ public async Task GenericCreateAndStartHost_GetTestClient()
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()
{
Expand Down