Skip to content

Commit

Permalink
Fixing issues with ScriptSettingsManager reset when the configuration…
Browse files Browse the repository at this point in the history
… factory returned a cached configuration instance
  • Loading branch information
fabiocav committed Mar 29, 2018
1 parent 043561c commit e5b569e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/WebJobs.Script/Config/ScriptSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private void UpdateSettingInCache(string settingKey, string settingValue)

public virtual void Reset()
{
_configuration = new Lazy<IConfiguration>(_configurationFactory);
Lazy<IConfiguration> current = _configuration;
_configuration = new Lazy<IConfiguration>(() => InitializeConfiguration(current));

_settingsCache.Clear();
}
Expand All @@ -153,6 +154,24 @@ public virtual void SetSetting(string settingKey, string settingValue)
}
}

private IConfiguration InitializeConfiguration(Lazy<IConfiguration> currentConfiguration)
{
var configuration = _configurationFactory();

// If the factory returned a cached instance
// that is the same as the instance we previously had,
// reload the configuration on that instance.
if (configuration is IConfigurationRoot root &&
currentConfiguration != null &&
currentConfiguration.IsValueCreated &&
object.ReferenceEquals(currentConfiguration.Value, root))
{
root.Reload();
}

return configuration;
}

private static IConfigurationRoot BuildConfiguration()
{
var configurationBuilder = new ConfigurationBuilder()
Expand Down
19 changes: 19 additions & 0 deletions test/WebJobs.Script.Tests/ScriptSettingsManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests
Expand Down Expand Up @@ -37,5 +38,23 @@ public void UniqueSlotName_ReturnsExpectedValue(string siteName, string slotName
Assert.Equal(expectedValue, ScriptSettingsManager.Instance.AzureWebsiteUniqueSlotName);
}
}

[Fact]
public void Reset_ReloadsEnvironmentChanges()
{
using (var variable = new TestScopedEnvironmentVariable(nameof(Reset_ReloadsEnvironmentChanges), "foo"))
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

ScriptSettingsManager.Instance.SetConfigurationFactory(() => configuration);
Assert.Equal("foo", ScriptSettingsManager.Instance.GetSetting(nameof(Reset_ReloadsEnvironmentChanges)));

Environment.SetEnvironmentVariable(nameof(Reset_ReloadsEnvironmentChanges), "bar");
ScriptSettingsManager.Instance.Reset();
Assert.Equal("bar", ScriptSettingsManager.Instance.GetSetting(nameof(Reset_ReloadsEnvironmentChanges)));
}
}
}
}

0 comments on commit e5b569e

Please sign in to comment.