-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition testing global state (#28536)
Part of #27306
- Loading branch information
1 parent
7cc7429
commit 1ff156c
Showing
3 changed files
with
78 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
}); | ||
|
||
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
😅