Skip to content

Commit

Permalink
Change flag to memory data source. Change content root default instea…
Browse files Browse the repository at this point in the history
…d of runtime default.
  • Loading branch information
kjerk committed Feb 11, 2020
1 parent 126381c commit 1a9b615
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/Hosting/Hosting/test/HostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,26 @@ public void CreateDefaultBuilder_EnablesValidateOnBuild()
[Fact]
public void CreateDefaultBuilder_ConfigJsonDoesNotReload()
{
Environment.SetEnvironmentVariable("DOTNET_HOSTBUILDER_CONFIG_RELOAD", "false");
Environment.CurrentDirectory = Path.GetTempPath();
var reloadFlagConfig = new Dictionary<string, string>() {{ "HOSTBUILDER_CONFIG_RELOAD", "false" }};
var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json");

string SaveRandomConfig()
{
var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}";
File.WriteAllText(Path.Combine(Path.GetTempPath(), "appsettings.json"), $"{{ \"Hello\": \"{newMessage}\" }}");
File.WriteAllText(appSettingsPath, $"{{ \"Hello\": \"{newMessage}\" }}");
return newMessage;
}

var dynamicConfigMessage1 = SaveRandomConfig();
var host = Host.CreateDefaultBuilder().Build();

var host = Host.CreateDefaultBuilder()
.UseContentRoot(Path.GetDirectoryName(appSettingsPath))
.ConfigureHostConfiguration(builder =>
{
builder.AddInMemoryCollection(reloadFlagConfig);
})
.Build();

var config = host.Services.GetRequiredService<IConfiguration>();

Assert.Equal(dynamicConfigMessage1, config["Hello"]);
Expand All @@ -110,26 +118,34 @@ string SaveRandomConfig()
[Fact]
public void CreateDefaultBuilder_ConfigJsonDoesReload()
{
Environment.SetEnvironmentVariable("DOTNET_HOSTBUILDER_CONFIG_RELOAD", "true");
Environment.CurrentDirectory = Path.GetTempPath();
var reloadFlagConfig = new Dictionary<string, string>() { { "HOSTBUILDER_CONFIG_RELOAD", "true" } };
var appSettingsPath = Path.Combine(Path.GetTempPath(), "appsettings.json");

string SaveRandomConfig()
{
var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}";
File.WriteAllText(Path.Combine(Path.GetTempPath(), "appsettings.json"), $"{{ \"Hello\": \"{newMessage}\" }}");
File.WriteAllText(appSettingsPath, $"{{ \"Hello\": \"{newMessage}\" }}");
return newMessage;
}

var dynamicConfigMessage1 = SaveRandomConfig();
var host = Host.CreateDefaultBuilder().Build();

var host = Host.CreateDefaultBuilder()
.UseContentRoot(Path.GetDirectoryName(appSettingsPath))
.ConfigureHostConfiguration(builder =>
{
builder.AddInMemoryCollection(reloadFlagConfig);
})
.Build();

var config = host.Services.GetRequiredService<IConfiguration>();

Assert.Equal(dynamicConfigMessage1, config["Hello"]);

var dynamicConfigMessage2 = SaveRandomConfig();
Task.Delay(1000).Wait(); // Give reload time to fire if it's going to.
Assert.NotEqual(dynamicConfigMessage1, dynamicConfigMessage2); // Messages are different.
Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload.
Assert.Equal(dynamicConfigMessage2, config["Hello"]); // Config DID reload from disk
}

internal class ServiceA { }
Expand Down

0 comments on commit 1a9b615

Please sign in to comment.