Skip to content

Commit

Permalink
Fix for environment variable race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRandall committed Mar 5, 2019
1 parent f02c900 commit bb7df16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
50 changes: 31 additions & 19 deletions Source/FunctionMonkey.Testing/AcceptanceTestScaffold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace FunctionMonkey.Testing
/// </summary>
public class AcceptanceTestScaffold
{
private static int _environmentVariablesRegistered = 0;
private static bool _environmentVariablesRegistered = false;
private static object _loadingAppVariablesLock = new object();

/// <summary>
/// Setup the scaffold with the default TestFunctionHostBuilder
Expand Down Expand Up @@ -102,12 +103,12 @@ protected virtual void RegisterFunctionMonkeyMocks(IServiceCollection serviceCol
/// <param name="oneTimeOnly">Defaults to true, if true only set the variables one time</param>
public void AddEnvironmentVariables(Stream appSettingsStream, bool oneTimeOnly = true)
{
if (Interlocked.Exchange(ref _environmentVariablesRegistered, 1) == 1)
if (_environmentVariablesRegistered && oneTimeOnly)
{
return;
}
}

SetEnvironmentVariables(appSettingsStream);
SetEnvironmentVariables(appSettingsStream, oneTimeOnly);
}

/// <summary>
Expand All @@ -117,40 +118,51 @@ public void AddEnvironmentVariables(Stream appSettingsStream, bool oneTimeOnly =
/// <param name="oneTimeOnly">Defaults to true, if true only set the variables one time</param>
public void AddEnvironmentVariables(string appSettingsPath, bool oneTimeOnly = true)
{
if (Interlocked.Exchange(ref _environmentVariablesRegistered, 1) == 1)
if (_environmentVariablesRegistered && oneTimeOnly)
{
return;
}

using (Stream stream = new FileStream(appSettingsPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
SetEnvironmentVariables(stream);
SetEnvironmentVariables(stream, oneTimeOnly);
}
}

private static void SetEnvironmentVariables(Stream appSettings)
private static void SetEnvironmentVariables(Stream appSettings, bool oneTimeOnly)
{
string json;
using (StreamReader reader = new StreamReader(appSettings))
lock (_loadingAppVariablesLock)
{
json = reader.ReadToEnd();
}
if (_environmentVariablesRegistered && oneTimeOnly)
{
return;
}

if (!string.IsNullOrWhiteSpace(json))
{
JObject settings = JObject.Parse(json);
JObject values = (JObject)settings["Values"];
if (values != null)
string json;
using (StreamReader reader = new StreamReader(appSettings))
{
foreach (JProperty property in values.Properties())
json = reader.ReadToEnd();
}

if (!string.IsNullOrWhiteSpace(json))
{
JObject settings = JObject.Parse(json);
JObject values = (JObject)settings["Values"];
if (values != null)
{
if (property.Value != null)
foreach (JProperty property in values.Properties())
{
Environment.SetEnvironmentVariable(property.Name, property.Value.ToString());
if (property.Value != null)
{
Environment.SetEnvironmentVariable(property.Name, property.Value.ToString());
}
}
}
}

_environmentVariablesRegistered = true;
}

}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions Source/FunctionMonkey.Testing/FunctionMonkey.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.21.6-beta000</Version>
<Version>0.21.7-beta000</Version>
<Authors>James Randall</Authors>
<Company>James Randall</Company>
<Copyright></Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<PackageProjectUrl>https://functionmonkey.azurefromthetrenches.com/</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyVersion>0.21.6.0</AssemblyVersion>
<FileVersion>0.21.6.0</FileVersion>
<AssemblyVersion>0.21.7.0</AssemblyVersion>
<FileVersion>0.21.7.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit bb7df16

Please sign in to comment.