Skip to content

Commit 617c885

Browse files
authored
Fix race condition in Microsoft.Extensions.Configuration test (#120242)
Fixes #109904
1 parent a2f46fc commit 617c885

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

src/libraries/Microsoft.Extensions.Configuration.EnvironmentVariables/tests/EnvironmentVariablesTest.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,27 +285,6 @@ public void AddEnvironmentVariablesUsingPrefixWithDoubleUnderscores_Bind_PrefixM
285285

286286
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
287287
public void BindingDoesNotThrowIfReloadedDuringBinding()
288-
{
289-
// Instrumentation for https://github.com/dotnet/runtime/issues/109904. Failfast to generate a crash dump when the test fails with NullReferenceException.
290-
try
291-
{
292-
BindingDoesNotThrowIfReloadedDuringBindingWorker();
293-
}
294-
catch (NullReferenceException) when (FailFast())
295-
{
296-
}
297-
298-
static bool FailFast()
299-
{
300-
Environment.FailFast(
301-
"Instrumentation for https://github.com/dotnet/runtime/issues/109904" + Environment.NewLine +
302-
"Microsoft.Extensions.Configuration.EnvironmentVariables.Test.EnvironmentVariablesTest.BindingDoesNotThrowIfReloadedDuringBinding [FAIL]" + Environment.NewLine +
303-
"System.NullReferenceException : Object reference not set to an instance of an object.");
304-
return false;
305-
}
306-
}
307-
308-
private void BindingDoesNotThrowIfReloadedDuringBindingWorker()
309288
{
310289
var dic = new Dictionary<string, string>
311290
{
@@ -317,8 +296,6 @@ private void BindingDoesNotThrowIfReloadedDuringBindingWorker()
317296
configurationBuilder.AddEnvironmentVariables();
318297
var config = configurationBuilder.Build();
319298

320-
MyOptions options = null;
321-
322299
using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
323300
{
324301
void ReloadLoop()
@@ -331,14 +308,17 @@ void ReloadLoop()
331308

332309
_ = Task.Run(ReloadLoop);
333310

334-
while (!cts.IsCancellationRequested)
311+
MyOptions options;
312+
313+
do
335314
{
336315
options = config.Get<MyOptions>();
337316
}
338-
}
317+
while (!cts.IsCancellationRequested);
339318

340-
Assert.Equal(-2, options.Number);
341-
Assert.Equal("Foo", options.Text);
319+
Assert.Equal(-2, options.Number);
320+
Assert.Equal("Foo", options.Text);
321+
}
342322
}
343323

344324
private sealed class MyOptions

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -966,23 +966,19 @@ void ReloadLoop()
966966

967967
_ = Task.Run(ReloadLoop);
968968

969-
MyOptions options = null;
969+
MyOptions options;
970970

971-
bool optionsInitialized = false;
972-
while (!cts.IsCancellationRequested)
971+
do
973972
{
974973
options = config.Get<MyOptions>();
975-
optionsInitialized = true;
976974
}
975+
while (!cts.IsCancellationRequested);
977976

978-
if (optionsInitialized)
979-
{
980-
Assert.Equal("CmdValue1", options.CmdKey1);
981-
Assert.Equal("IniValue1", options.IniKey1);
982-
Assert.Equal("JsonValue1", options.JsonKey1);
983-
Assert.Equal("MemValue1", options.MemKey1);
984-
Assert.Equal("XmlValue1", options.XmlKey1);
985-
}
977+
Assert.Equal("CmdValue1", options.CmdKey1);
978+
Assert.Equal("IniValue1", options.IniKey1);
979+
Assert.Equal("JsonValue1", options.JsonKey1);
980+
Assert.Equal("MemValue1", options.MemKey1);
981+
Assert.Equal("XmlValue1", options.XmlKey1);
986982
}
987983
}
988984

0 commit comments

Comments
 (0)