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

Atomically swap config data #1202

Merged
merged 4 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -48,7 +48,7 @@ public override void Load()

internal void Load(IDictionary envVariables)
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var filteredEnvVariables = envVariables
.Cast<DictionaryEntry>()
Expand All @@ -58,8 +58,10 @@ internal void Load(IDictionary envVariables)
foreach (var envVariable in filteredEnvVariables)
{
var key = ((string)envVariable.Key).Substring(_prefix.Length);
Data[key] = (string)envVariable.Value;
data[key] = (string)envVariable.Value;
}

Data = data;
}

private static string NormalizeKey(string key)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration.Test;
using Xunit;

Expand Down Expand Up @@ -148,5 +152,48 @@ public void ReplaceDoubleUnderscoreInEnvironmentVariables()
Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:_db1_ProviderName"));
}

[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
var dic = new Dictionary<string, string>
{
{"Number", "-2"},
{"Text", "Foo"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
configurationBuilder.AddEnvironmentVariables();
var config = configurationBuilder.Build();

MyOptions options = null;

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
void ReloadLoop()
{
while (!cts.IsCancellationRequested)
{
config.Reload();
}
}

_ = Task.Run(ReloadLoop);

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}
}

Assert.Equal(-2, options.Number);
Assert.Equal("Foo", options.Text);
}

private sealed class MyOptions
{
public int Number { get; set; }
public string Text { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ private static string TrimNewLine(string value)
/// </summary>
public override void Load()
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

if (Source.FileProvider == null)
{
if (Source.Optional)
{
Data = data;
return;
}

Expand All @@ -61,10 +62,12 @@ public override void Load()
{
if (Source.IgnoreCondition == null || !Source.IgnoreCondition(file.Name))
{
Data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
data.Add(NormalizeKey(file.Name), TrimNewLine(streamReader.ReadToEnd()));
}
}
}

Data = data;
}

private string GetDirectoryName()
Expand Down
43 changes: 43 additions & 0 deletions src/Configuration/Config.KeyPerFile/test/KeyPerFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Xunit;
Expand Down Expand Up @@ -179,6 +181,47 @@ public void CanUnIgnoreDefaultFiles()
Assert.Equal("SecretValue1", config["ignore.Secret1"]);
Assert.Equal("SecretValue2", config["Secret2"]);
}

[Fact]
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
var testFileProvider = new TestFileProvider(
new TestFile("Number", "-2"),
new TestFile("Text", "Foo"));

var config = new ConfigurationBuilder()
.AddKeyPerFile(o => o.FileProvider = testFileProvider)
.Build();

MyOptions options = null;

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
void ReloadLoop()
{
while (!cts.IsCancellationRequested)
{
config.Reload();
}
}

_ = Task.Run(ReloadLoop);

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}
}

Assert.Equal(-2, options.Number);
Assert.Equal("Foo", options.Text);
}

private sealed class MyOptions
{
public int Number { get; set; }
public string Text { get; set; }
}
}

class TestFileProvider : IFileProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Configuration.Ini;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.Xml;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Configuration.Xml;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Xunit;
Expand Down Expand Up @@ -944,6 +944,50 @@ public async Task TouchingFileWillReloadForUserSecrets()
Assert.True(token.HasChanged);
}

[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "File watching is flaky on non windows.")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "File watching is flaky on non windows.")]
martincostello marked this conversation as resolved.
Show resolved Hide resolved
public void BindingDoesNotThrowIfReloadedDuringBinding()
{
WriteTestFiles();

var configurationBuilder = CreateBuilder();
configurationBuilder.Add(new TestIniSourceProvider(_iniFile));
configurationBuilder.Add(new TestJsonSourceProvider(_jsonFile));
configurationBuilder.Add(new TestXmlSourceProvider(_xmlFile));
configurationBuilder.AddEnvironmentVariables();
configurationBuilder.AddCommandLine(new[] { "--CmdKey1=CmdValue1" });
configurationBuilder.AddInMemoryCollection(_memConfigContent);

var config = configurationBuilder.Build();

using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
{
void ReloadLoop()
{
while (!cts.IsCancellationRequested)
{
config.Reload();
}
}

_ = Task.Run(ReloadLoop);

MyOptions options = null;

while (!cts.IsCancellationRequested)
{
options = config.Get<MyOptions>();
}

Assert.Equal("CmdValue1", options.CmdKey1);
Assert.Equal("IniValue1", options.IniKey1);
Assert.Equal("JsonValue1", options.JsonKey1);
Assert.Equal("MemValue1", options.MemKey1);
Assert.Equal("XmlValue1", options.XmlKey1);
}
}

public void Dispose()
{
_fileProvider.Dispose();
Expand All @@ -966,5 +1010,18 @@ public void Dispose()
await Task.Delay(_msDelay);
}
}

private sealed class MyOptions
{
public string CmdKey1 { get; set; }

public string IniKey1 { get; set; }

public string JsonKey1 { get; set; }

public string MemKey1 { get; set; }

public string XmlKey1 { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.Ini" />
<Reference Include="Microsoft.Extensions.Configuration.Json" />
<Reference Include="Microsoft.Extensions.Configuration.Xml" />
Expand Down