From 48146dce77c4a03abbe5e77dafe02f470d2fe35b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Apr 2026 16:58:12 +0000 Subject: [PATCH] Fix flaky Finds_service_properties_in_hierarchy test The test was flaky because ServiceProviderCacheTest.Service_provider_cache_can_be_cleared called Clear() on the global ServiceProviderCache.Instance singleton, which could race with the hierarchy test when running in parallel. When the cache was cleared between the two using blocks, the second context got a new service provider with an empty in-memory database, resulting in 0 loaded entries. Two fixes applied: 1. Changed Service_provider_cache_can_be_cleared to use a local ServiceProviderCache instance instead of the global singleton (root cause fix) 2. Added a shared InMemoryDatabaseRoot to ServicePropertiesContext so the in-memory database survives service provider changes (defensive fix) Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/c7ac9a3c-4847-4884-8223-c969d57ac007 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com> --- .../Conventions/ServicePropertyDiscoveryConventionTest.cs | 4 +++- test/EFCore.Tests/ServiceProviderCacheTest.cs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/EFCore.Tests/Metadata/Conventions/ServicePropertyDiscoveryConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/ServicePropertyDiscoveryConventionTest.cs index c74015a1bad..5e1d009f1c1 100644 --- a/test/EFCore.Tests/Metadata/Conventions/ServicePropertyDiscoveryConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/ServicePropertyDiscoveryConventionTest.cs @@ -192,8 +192,10 @@ private abstract class Blog private class ServicePropertiesContext : DbContext { + private static readonly InMemoryDatabaseRoot _databaseRoot = new(); + protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseInMemoryDatabase(GetType().Name); + => optionsBuilder.UseInMemoryDatabase(GetType().Name, _databaseRoot); public DbSet PrivateUnmappedBaseSupers => Set(); diff --git a/test/EFCore.Tests/ServiceProviderCacheTest.cs b/test/EFCore.Tests/ServiceProviderCacheTest.cs index d2cac086b41..ce1c3877cfe 100644 --- a/test/EFCore.Tests/ServiceProviderCacheTest.cs +++ b/test/EFCore.Tests/ServiceProviderCacheTest.cs @@ -332,7 +332,7 @@ public override void PopulateDebugInfo(IDictionary debugInfo) [ConditionalFact] public void Service_provider_cache_can_be_cleared() { - var cache = ServiceProviderCache.Instance; + var cache = new ServiceProviderCache(); var options = new DbContextOptionsBuilder().UseInMemoryDatabase("TestDB").Options; var provider1 = cache.GetOrAdd(options, providerRequired: false);