Skip to content

Commit

Permalink
Fix race condition testing global state (#28536)
Browse files Browse the repository at this point in the history
Part of #27306
  • Loading branch information
ajcvickers authored Jul 28, 2022
1 parent 7cc7429 commit 1ff156c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
24 changes: 4 additions & 20 deletions test/EFCore.Design.Tests/Design/DbContextActivatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,24 @@ public class DbContextActivatorTest
[ConditionalFact]
public void CreateInstance_works()
{
EF.IsDesignTime = false;

var result = DbContextActivator.CreateInstance(typeof(TestContext));

Assert.IsType<TestContext>(result);

Assert.True(EF.IsDesignTime);
Assert.IsType<TestContext>(DbContextActivator.CreateInstance(typeof(TestContext)));
}

[ConditionalFact]
public void CreateInstance_with_arguments_works()
{
EF.IsDesignTime = false;

var result = DbContextActivator.CreateInstance(
Assert.IsType<TestContext>(DbContextActivator.CreateInstance(
typeof(TestContext),
null,
null,
new[] { "A", "B" });

Assert.IsType<TestContext>(result);

Assert.True(EF.IsDesignTime);
new[] { "A", "B" }));
}

private class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
Assert.True(EF.IsDesignTime);

options
=> options
.EnableServiceProviderCaching(false)
.UseInMemoryDatabase(nameof(DbContextActivatorTest));
}
}
}
74 changes: 74 additions & 0 deletions test/EFCore.Design.Tests/Design/DesignTimeFlagTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Design;

// The tests interact with global state and should never be run in parallel
[Collection("DesignTimeFlagTest")]
public class DesignTimeFlagTest
{
[ConditionalFact]
public void Operations_have_design_time_flag_set()
{
EF.IsDesignTime = false;

var handler = new OperationResultHandler();

new MockOperation<string>(handler, () =>
{
Assert.True(EF.IsDesignTime);
return "Twilight Sparkle";

This comment has been minimized.

Copy link
@AraHaan

AraHaan Jul 29, 2022

Member

😅

});

Assert.False(EF.IsDesignTime);
Assert.Equal("Twilight Sparkle", handler.Result);
}

[ConditionalFact]
public void CreateInstance_sets_design_time_flag()
{
EF.IsDesignTime = false;

Assert.IsType<TestContext>(DbContextActivator.CreateInstance(typeof(TestContext)));

Assert.True(EF.IsDesignTime);
}

[ConditionalFact]
public void CreateInstance_with_arguments_sets_design_time_flag()
{
EF.IsDesignTime = false;

Assert.IsType<TestContext>(DbContextActivator.CreateInstance(
typeof(TestContext),
null,
null,
new[] { "A", "B" }));

Assert.True(EF.IsDesignTime);
}

private class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
Assert.True(EF.IsDesignTime);

options
.EnableServiceProviderCaching(false)
.UseInMemoryDatabase(nameof(DbContextActivatorTest));
}
}

private class MockOperation<T> : OperationExecutor.OperationBase
{
public MockOperation(IOperationResultHandler resultHandler, Func<T> action)
: base(resultHandler)
=> Execute(action);
}
}

[CollectionDefinition("DesignTimeFlagTest", DisableParallelization = true)]
public class DesignTimeFlagTestCollection
{
}
18 changes: 0 additions & 18 deletions test/EFCore.Design.Tests/Design/OperationExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ public void Ctor_validates_arguments()

public class OperationBaseTests
{
[ConditionalFact]
public void Operations_have_design_time_flag_set()
{
EF.IsDesignTime = false;

var handler = new OperationResultHandler();
var result = "Twilight Sparkle";

new MockOperation<string>(handler, () =>
{
Assert.True(EF.IsDesignTime);
return result;
});

Assert.False(EF.IsDesignTime);
Assert.Equal(result, handler.Result);
}

[ConditionalFact]
public void Execute_catches_exceptions()
{
Expand Down

0 comments on commit 1ff156c

Please sign in to comment.