Skip to content

Commit

Permalink
Fix race problem in configuration tests (#93711)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Oct 19, 2023
1 parent 8964171 commit 4dc17af
Showing 1 changed file with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,37 +736,47 @@ public void GetCanReadInheritedProperties()
Assert.Equal("Derived:Sup", options.Virtual);
}

private static readonly object _syncLock = new object();

[Fact]
public void GetCanReadStaticProperty()
{
var dic = new Dictionary<string, string>
// The test uses ComplexOptions.StaticProperty which is possible to be changed by other tests.
lock (_syncLock)
{
{"StaticProperty", "stuff"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var options = new ComplexOptions();
config.Bind(options);
var dic = new Dictionary<string, string>
{
{"StaticProperty", "stuff"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var options = new ComplexOptions();
config.Bind(options);

Assert.Equal("stuff", ComplexOptions.StaticProperty);
Assert.Equal("stuff", ComplexOptions.StaticProperty);
}
}

[Fact]
public void BindCanReadStaticProperty()
{
var dic = new Dictionary<string, string>
// The test uses ComplexOptions.StaticProperty which is possible to be changed by other tests.
lock (_syncLock)
{
{"StaticProperty", "other stuff"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var dic = new Dictionary<string, string>
{
{"StaticProperty", "other stuff"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

var instance = new ComplexOptions();
config.Bind(instance);
var instance = new ComplexOptions();
config.Bind(instance);

Assert.Equal("other stuff", ComplexOptions.StaticProperty);
Assert.Equal("other stuff", ComplexOptions.StaticProperty);
}
}

[Fact]
Expand Down Expand Up @@ -2342,7 +2352,7 @@ public static void TestBindingInitializedAbstractMember()
ClassWithAbstractProp c = new();
c.AbstractProp = new Derived();
configuration.Bind(c);
Assert.Equal(1, c.AbstractProp.Value);
Assert.Equal(1, c.AbstractProp.Value);
}

[Fact]
Expand All @@ -2351,9 +2361,9 @@ public static void TestBindingUninitializedAbstractMember()
IConfiguration configuration = TestHelpers.GetConfigurationFromJsonString(@"{ ""AbstractProp"": {""Value"":1} }");
ClassWithAbstractProp c = new();
c.AbstractProp = null;
Assert.Throws<InvalidOperationException>(() => configuration.Bind(c));
Assert.Throws<InvalidOperationException>(() => configuration.Bind(c));
}

[Fact]
public void GetIConfigurationSection()
{
Expand Down Expand Up @@ -2465,8 +2475,8 @@ class MockConfigurationRoot : ConfigurationRoot, IConfigurationRoot
public MockConfigurationRoot(IList<IConfigurationProvider> providers) : base(providers)
{ }

IConfigurationSection IConfiguration.GetSection(string key) =>
this[key] is null ? null : new ConfigurationSection(this, key);
IConfigurationSection IConfiguration.GetSection(string key) =>
this[key] is null ? null : new ConfigurationSection(this, key);
}
}
}

0 comments on commit 4dc17af

Please sign in to comment.