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

PR: Add test code coverage for Host.StopAsync #40875

Merged
2 commits merged into from
Dec 15, 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 @@ -19,13 +19,25 @@ namespace Microsoft.Extensions.Hosting
{
public partial class HostTests
{
[Fact]
public async Task StopAsyncWithCancellation()
{
var builder = new HostBuilder();
using var host = builder.Build();
await host.StartAsync();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
Assert.True(cts.Token.IsCancellationRequested);
await host.StopAsync(cts.Token);
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void CreateDefaultBuilder_IncludesContentRootByDefault()
{
var expected = Directory.GetCurrentDirectory();
var builder = Host.CreateDefaultBuilder();
var host = builder.Build();
using var host = builder.Build();
var config = host.Services.GetRequiredService<IConfiguration>();
Assert.Equal(expected, config["ContentRoot"]);
var env = host.Services.GetRequiredService<IHostEnvironment>();
Expand All @@ -38,7 +50,7 @@ public void CreateDefaultBuilder_IncludesCommandLineArguments()
{
var expected = Directory.GetParent(Directory.GetCurrentDirectory()).FullName; // It must exist
var builder = Host.CreateDefaultBuilder(new string[] { "--contentroot", expected });
var host = builder.Build();
using var host = builder.Build();
var env = host.Services.GetRequiredService<IHostEnvironment>();
Assert.Equal(expected, env.ContentRootPath);
}
Expand All @@ -48,7 +60,7 @@ public void CreateDefaultBuilder_IncludesCommandLineArguments()
public void CreateDefaultBuilder_RegistersEventSourceLogger()
{
var listener = new TestEventListener();
var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder()
.Build();

var logger = host.Services.GetRequiredService<ILogger<HostTests>>();
Expand Down Expand Up @@ -89,7 +101,7 @@ public void CreateDefaultBuilder_EnablesActivityTracking()
}
});
var loggerProvider = new ScopeDelegateLoggerProvider(logger);
var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder()
.ConfigureLogging(logging =>
{
logging.AddProvider(loggerProvider);
Expand All @@ -103,7 +115,7 @@ public void CreateDefaultBuilder_EnablesActivityTracking()
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void CreateDefaultBuilder_EnablesScopeValidation()
{
var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder()
.UseEnvironment(Environments.Development)
.ConfigureServices(serices =>
{
Expand Down Expand Up @@ -144,7 +156,7 @@ string SaveRandomConfig()

var dynamicConfigMessage1 = SaveRandomConfig();

var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder()
.UseContentRoot(Path.GetDirectoryName(appSettingsPath))
.ConfigureHostConfiguration(builder =>
{
Expand Down Expand Up @@ -178,7 +190,7 @@ string SaveRandomConfig()

var dynamicConfigMessage1 = SaveRandomConfig();

var host = Host.CreateDefaultBuilder()
using var host = Host.CreateDefaultBuilder()
.UseContentRoot(Path.GetDirectoryName(appSettingsPath))
.ConfigureHostConfiguration(builder =>
{
Expand Down