Skip to content

Commit

Permalink
Update config key to be hierarchical. Change await on positive case t…
Browse files Browse the repository at this point in the history
…o be longer and cancel using the reload token.
  • Loading branch information
kjerk committed Mar 3, 2020
1 parent 1a9b615 commit de16b88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Hosting/Hosting/src/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 16 additions & 6 deletions src/Hosting/Hosting/test/HostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,9 +84,9 @@ public void CreateDefaultBuilder_EnablesValidateOnBuild()
}

[Fact]
public void CreateDefaultBuilder_ConfigJsonDoesNotReload()
public async Task CreateDefaultBuilder_ConfigJsonDoesNotReload()
{
var reloadFlagConfig = new Dictionary<string, string>() {{ "HOSTBUILDER_CONFIG_RELOAD", "false" }};
var reloadFlagConfig = new Dictionary<string, string>() {{ "hostbuilder:configreload", "false" }};
var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json");

string SaveRandomConfig()
Expand All @@ -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<string, string>() { { "HOSTBUILDER_CONFIG_RELOAD", "true" } };
var reloadFlagConfig = new Dictionary<string, string>() { { "hostbuilder:configreload", "true" } };
var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json");

string SaveRandomConfig()
Expand All @@ -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
}
Expand Down

0 comments on commit de16b88

Please sign in to comment.