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

Fix CreateDefaultBuilder_SecretsDoesReload test failure #52466

Closed
wants to merge 1 commit into from
Closed
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 @@ -275,44 +275,53 @@ static string SaveRandomConfig(string appSettingsPath)
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public async Task CreateDefaultBuilder_SecretsDoesReload()
{
var secretId = Assembly.GetExecutingAssembly().GetName().Name;
var reloadFlagConfig = new Dictionary<string, string>() { { "hostbuilder:reloadConfigOnChange", "true" } };
var secretPath = PathHelper.GetSecretsPathFromSecretsId(secretId);
var secretFileInfo = new FileInfo(secretPath);

Directory.CreateDirectory(secretFileInfo.Directory.FullName);

static string SaveRandomSecret(string secretPath)
var originalValue = Environment.GetEnvironmentVariable("DOTNET_USE_POLLING_FILE_WATCHER");
Copy link
Member

@maryamariyan maryamariyan May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers:

The diff looks tough to read but seems like this change is basically just adding a try/finally block around this entire code snippet and setting DOTNET_USE_POLLING_FILE_WATCHER environment variable

also:

-           config.GetReloadToken().RegisterChangeCallback(
-                _ => configReloadedCancelTokenSource.Cancel(), null);
+           using IDisposable token = config.GetReloadToken().RegisterChangeCallback(
+                _ => configReloadedCancelTokenSource.Cancel(), null);

try
{
var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}";
File.WriteAllText(secretPath, $"{{ \"Hello\": \"{newMessage}\" }}");
return newMessage;
}
Environment.SetEnvironmentVariable("DOTNET_USE_POLLING_FILE_WATCHER", "1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests run concurrently? If so, then setting the environment variable for the entire process might effect other tests, you can use RemoteExecutor to give the test it's own process, or use API to setup the polling file-watcher.

UsePollingFileWatcher = true,
UseActivePolling = true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. That makes sense. I could wrap this up in the RemoteExecutor to isolate it, but I'd like to ensure first that changing this test in this way is the correct approach?

var secretId = Assembly.GetExecutingAssembly().GetName().Name;
var reloadFlagConfig = new Dictionary<string, string>() { { "hostbuilder:reloadConfigOnChange", "true" } };
var secretPath = PathHelper.GetSecretsPathFromSecretsId(secretId);
var secretFileInfo = new FileInfo(secretPath);

var dynamicSecretMessage1 = SaveRandomSecret(secretPath);
var host = Host.CreateDefaultBuilder(new[] { "environment=Development", $"applicationName={secretId}" })
.ConfigureHostConfiguration(builder =>
Directory.CreateDirectory(secretFileInfo.Directory.FullName);

static string SaveRandomSecret(string secretPath)
{
builder.AddInMemoryCollection(reloadFlagConfig);
})
.Build();
var newMessage = $"Hello ASP.NET Core: {Guid.NewGuid():N}";
File.WriteAllText(secretPath, $"{{ \"Hello\": \"{newMessage}\" }}");
return newMessage;
}

var config = host.Services.GetRequiredService<IConfiguration>();
Assert.Equal(dynamicSecretMessage1, config["Hello"]);
var dynamicSecretMessage1 = SaveRandomSecret(secretPath);
var host = Host.CreateDefaultBuilder(new[] { "environment=Development", $"applicationName={secretId}" })
.ConfigureHostConfiguration(builder =>
{
builder.AddInMemoryCollection(reloadFlagConfig);
})
.Build();

using CancellationTokenSource configReloadedCancelTokenSource = new();
var configReloadedCancelToken = configReloadedCancelTokenSource.Token;
var config = host.Services.GetRequiredService<IConfiguration>();
Assert.Equal(dynamicSecretMessage1, config["Hello"]);

config.GetReloadToken().RegisterChangeCallback(
_ => configReloadedCancelTokenSource.Cancel(), null);
using CancellationTokenSource configReloadedCancelTokenSource = new();
var configReloadedCancelToken = configReloadedCancelTokenSource.Token;

// Only update the secrets after we've registered the change callback
var dynamicSecretMessage2 = SaveRandomSecret(secretPath);
using IDisposable token = config.GetReloadToken().RegisterChangeCallback(
_ => configReloadedCancelTokenSource.Cancel(), null);

// Wait for up to 1 minute, if config reloads at any time, cancel the wait.
await Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1), configReloadedCancelToken)); // Task.WhenAny ignores the task throwing on cancellation.
Assert.NotEqual(dynamicSecretMessage1, dynamicSecretMessage2); // Messages are different.
Assert.Equal(dynamicSecretMessage2, config["Hello"]);
// Only update the secrets after we've registered the change callback
var dynamicSecretMessage2 = SaveRandomSecret(secretPath);

// Wait for up to 1 minute, if config reloads at any time, cancel the wait.
await Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1), configReloadedCancelToken)); // Task.WhenAny ignores the task throwing on cancellation.
Assert.NotEqual(dynamicSecretMessage1, dynamicSecretMessage2); // Messages are different.
Assert.Equal(dynamicSecretMessage2, config["Hello"]);
}
finally
{
Environment.SetEnvironmentVariable("DOTNET_USE_POLLING_FILE_WATCHER", originalValue);
}
}

[Fact]
Expand Down