From de16b88781a0a8171a6268a43f8b1211679bdc8b Mon Sep 17 00:00:00 2001 From: Keith Kjer Date: Tue, 3 Mar 2020 15:59:46 -0800 Subject: [PATCH] Update config key to be hierarchical. Change await on positive case to be longer and cancel using the reload token. --- src/Hosting/Hosting/src/Host.cs | 2 +- src/Hosting/Hosting/test/HostTests.cs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Hosting/Hosting/src/Host.cs b/src/Hosting/Hosting/src/Host.cs index e3d9707c7b0..023749a37c1 100644 --- a/src/Hosting/Hosting/src/Host.cs +++ b/src/Hosting/Hosting/src/Host.cs @@ -71,7 +71,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) { var env = hostingContext.HostingEnvironment; - var reloadOnChange = hostingContext.Configuration.GetValue("HOSTBUILDER_CONFIG_RELOAD", true); + var reloadOnChange = hostingContext.Configuration.GetValue("hostbuilder:configreload", true); config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: reloadOnChange) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: reloadOnChange); diff --git a/src/Hosting/Hosting/test/HostTests.cs b/src/Hosting/Hosting/test/HostTests.cs index ff2f26846dc..644b2d3cd53 100644 --- a/src/Hosting/Hosting/test/HostTests.cs +++ b/src/Hosting/Hosting/test/HostTests.cs @@ -7,6 +7,7 @@ using System.Diagnostics.Tracing; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -83,9 +84,9 @@ public void CreateDefaultBuilder_EnablesValidateOnBuild() } [Fact] - public void CreateDefaultBuilder_ConfigJsonDoesNotReload() + public async Task CreateDefaultBuilder_ConfigJsonDoesNotReload() { - var reloadFlagConfig = new Dictionary() {{ "HOSTBUILDER_CONFIG_RELOAD", "false" }}; + var reloadFlagConfig = new Dictionary() {{ "hostbuilder:configreload", "false" }}; var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json"); string SaveRandomConfig() @@ -110,15 +111,15 @@ string SaveRandomConfig() Assert.Equal(dynamicConfigMessage1, config["Hello"]); var dynamicConfigMessage2 = SaveRandomConfig(); - Task.Delay(1000).Wait(); // Give reload time to fire if it's going to. + await Task.Delay(1000); // Give reload time to fire if it's going to. Assert.NotEqual(dynamicConfigMessage1, dynamicConfigMessage2); // Messages are different. Assert.Equal(dynamicConfigMessage1, config["Hello"]); // Config did not reload } [Fact] - public void CreateDefaultBuilder_ConfigJsonDoesReload() + public async Task CreateDefaultBuilder_ConfigJsonDoesReload() { - var reloadFlagConfig = new Dictionary() { { "HOSTBUILDER_CONFIG_RELOAD", "true" } }; + var reloadFlagConfig = new Dictionary() { { "hostbuilder:configreload", "true" } }; var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json"); string SaveRandomConfig() @@ -143,7 +144,16 @@ string SaveRandomConfig() Assert.Equal(dynamicConfigMessage1, config["Hello"]); var dynamicConfigMessage2 = SaveRandomConfig(); - Task.Delay(1000).Wait(); // Give reload time to fire if it's going to. + + var configReloadedCancelTokenSource = new CancellationTokenSource(); + var configReloadedCancelToken = configReloadedCancelTokenSource.Token; + + config.GetReloadToken().RegisterChangeCallback(o => + { + //configReloadedCancelTokenSource.Cancel(); + }, null); + // Wait for up to 10 seconds, if config reloads at any time, cancel the wait. + await Task.WhenAny(Task.Delay(10000, configReloadedCancelToken)); // Task.WhenAny ignores the task throwing on cancellation. Assert.NotEqual(dynamicConfigMessage1, dynamicConfigMessage2); // Messages are different. Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload from disk }