test(org): isolate in-memory store per fixture
TestWebApplicationFactory instances shared the same in-memory database
because EF Core's UseInMemoryDatabase("InMemory") returns the same
backing store to every DbContext that asks for it under the same
connection string, in the same process. Whichever fixture started
first defined the state, and every subsequent fixture inherited it,
making tests silently order-dependent and flaky.
Fix:
- Yavsc.Tests.Shared/InMemoryDatabaseName: helper that suffixes the
in-memory connection string with a per-fixture GUID.
- TestWebApplicationFactory: instance GUID + ConnectionStrings__
YavscConnection set as an environment variable in the constructor
and cleared in Dispose, so each factory gets its own backing store.
Env var is needed because IdentityServer8.EntityFramework exposes
ConfigureDbContext as Action<DbContextOptionsBuilder> with no
service-provider access, so the connection string is captured at
registration time. AddEnvironmentVariables is the last provider in
the config pipeline and wins regardless.
- WebServerFixture: process-static GUID (WebHostFixture is a
per-process singleton by design, so the test collection shares one
store; the GUID still isolates from TestWebApplicationFactory).
- AddIdentityDBAndStores: read the connection string at DbContext
construction time via the (sp, options) overload of AddDbContext,
so test fixtures can override it via the host's IConfiguration.
IdentityServer stores cannot do the same without subclassing the
framework's DbContexts; the env var path is the documented escape
hatch in HostingExtensions.AddIdentityServer.
- UsesInMemoryProvider: StartsWith instead of equality, so
'InMemory-{guid}' is still recognised as an in-memory connection
string.
Regression sentinel in
Controllers/TestWebApplicationFactoryIsolationTests: two factories
seed a marker client in the first, the second must not see it.
Suite: 45/45 over 3 stable runs, 13-15s each.
This commit is contained in:
parent
d05ac52829
commit
61b41f0c55
5 changed files with 201 additions and 6 deletions
|
|
@ -42,6 +42,17 @@ public sealed class WebServerFixture : WebHostFixture
|
|||
{
|
||||
private static readonly int _httpsPort = GetAvailableLoopbackPort();
|
||||
|
||||
// One in-memory database name for the whole process: WebHostFixture
|
||||
// is a per-process singleton (see _app, _isInitialized, _sharedServices
|
||||
// in the base class), so every WebServerFixture instance shares the
|
||||
// same backing store. That is intentional — the "Yavsc Server" test
|
||||
// collection groups tests that should see the same seeded state, and
|
||||
// re-initialising the store per fixture would just regress the
|
||||
// order-dependence we are trying to eliminate. The GUID still matters
|
||||
// because TestWebApplicationFactory and WebServerFixture must not
|
||||
// collide in the in-memory store; see InMemoryDatabaseName.
|
||||
private static readonly string _fixtureId = Guid.NewGuid().ToString("N");
|
||||
|
||||
protected override int HttpsPort => _httpsPort;
|
||||
|
||||
private static IConfiguration? _sharedConfiguration;
|
||||
|
|
@ -80,7 +91,7 @@ public sealed class WebServerFixture : WebHostFixture
|
|||
// that plus the in-memory overrides below.
|
||||
builder.AddConfiguration(null).AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
[$"ConnectionStrings:{Yavsc.Constants.YavscConnectionStringName}"] = "InMemory",
|
||||
[$"ConnectionStrings:{Yavsc.Constants.YavscConnectionStringName}"] = InMemoryDatabaseName.For(_fixtureId),
|
||||
// SMTP test config: UserName non-null so MailSender
|
||||
// exercises the Authenticate branch — the
|
||||
// RecordingSmtpClient captures it.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue