diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/Helpers/TestDbContextOptionsBuilderExtensions.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/Helpers/TestDbContextOptionsBuilderExtensions.cs new file mode 100644 index 00000000000..40a7679e432 --- /dev/null +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/Helpers/TestDbContextOptionsBuilderExtensions.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; + +namespace Mocha.EntityFrameworkCore.Postgres.Tests.Helpers; + +internal static class TestDbContextOptionsBuilderExtensions +{ + // Each test builds its own service provider, so EF accumulates more than twenty + // internal service providers across a run. Ignore the resulting warning instead of + // letting it throw. + public static DbContextOptionsBuilder UseTestNpgsql( + this DbContextOptionsBuilder optionsBuilder, + string connectionString) + => optionsBuilder.UseNpgsql(connectionString) + .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)); + + public static DbContextOptionsBuilder UseTestNpgsql( + this DbContextOptionsBuilder optionsBuilder, + string connectionString) + where TContext : DbContext + => optionsBuilder.UseNpgsql(connectionString) + .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)); +} diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/InboxServiceRegistrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/InboxServiceRegistrationTests.cs index b680f28fc7d..4fee7e45636 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/InboxServiceRegistrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/InboxServiceRegistrationTests.cs @@ -1,4 +1,3 @@ -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -112,7 +111,7 @@ private static ServiceProvider BuildProvider(Action? configure = n { var services = new ServiceCollection(); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(ConnectionString)); + services.AddDbContext(o => o.UseTestNpgsql(ConnectionString)); // Use a resilient signal to prevent ObjectDisposedException when // EF Core shares the internal service provider (and interceptors) diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxEntityConfigurationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxEntityConfigurationTests.cs index 96094216703..c1635a3e3fb 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxEntityConfigurationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxEntityConfigurationTests.cs @@ -77,7 +77,7 @@ private static IEntityType GetOutboxMessageEntityType() private static IModel CreateModel() { - var options = new DbContextOptionsBuilder().UseNpgsql("Host=localhost").Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql("Host=localhost").Options; using var context = new TestDbContext(options); return context.GetService().Model; } diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxServiceRegistrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxServiceRegistrationTests.cs index 7d34daf436d..7f436bd2b8a 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxServiceRegistrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxServiceRegistrationTests.cs @@ -1,5 +1,3 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -94,8 +92,7 @@ private static ServiceProvider BuildProvider() { var services = new ServiceCollection(); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(ConnectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(ConnectionString)); // Use a resilient signal to prevent ObjectDisposedException when // EF Core shares the internal service provider (and interceptors) diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageInboxTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageInboxTests.cs index 8b2cd915d42..1a3298337e7 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageInboxTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageInboxTests.cs @@ -276,7 +276,7 @@ public async Task CleanupAsync_Should_DeleteMessages_When_TimeProviderAdvancedPa { var connectionString = await _fixture.CreateDatabaseAsync(); var options = new DbContextOptionsBuilder() - .UseNpgsql(connectionString) + .UseTestNpgsql(connectionString) .Options; var context = new TestDbContext(options); await context.Database.EnsureCreatedAsync(); diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageOutboxTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageOutboxTests.cs index ff2d66046e3..4d5d1a648db 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageOutboxTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresMessageOutboxTests.cs @@ -113,7 +113,7 @@ public async Task PersistAsync_Should_SerializeEnvelopeAsJson_When_Called() public void Dispose_Should_NotThrow_When_Called() { // Arrange - var options = new DbContextOptionsBuilder().UseNpgsql("Host=localhost;Database=test").Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql("Host=localhost;Database=test").Options; using var context = new TestDbContext(options); var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo()); var signal = new StubOutboxSignal(); @@ -130,7 +130,7 @@ public void Dispose_Should_NotThrow_When_Called() StubOutboxSignal Signal)> CreateOutboxAsync() { var connectionString = await _fixture.CreateDatabaseAsync(); - var options = new DbContextOptionsBuilder().UseNpgsql(connectionString).Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql(connectionString).Options; var context = new TestDbContext(options); await context.Database.EnsureCreatedAsync(); var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo()); diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresOutboxIntegrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresOutboxIntegrationTests.cs index 7d0b9080e78..20ccb4d3e64 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresOutboxIntegrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresOutboxIntegrationTests.cs @@ -1,6 +1,5 @@ using System.Collections.Concurrent; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers; @@ -103,8 +102,7 @@ public async Task Outbox_Should_ProcessPendingMessages_When_WorkerStartsAfterPer var services = new ServiceCollection(); services.AddSingleton(recorder); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(connectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(connectionString)); services.AddSingleton(); var builder = services.AddMessageBus(); @@ -180,8 +178,7 @@ public async Task Outbox_Should_ResumeProcessing_When_WorkerRestartedAfterInterr var services = new ServiceCollection(); services.AddSingleton(recorder); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(connectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(connectionString)); services.AddSingleton(); var builder = services.AddMessageBus(); @@ -336,8 +333,7 @@ private async Task CreateBusWithOutboxAsync(MessageRecorder rec var services = new ServiceCollection(); services.AddSingleton(recorder); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(connectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(connectionString)); // Register the resilient signal BEFORE UsePostgresOutbox() so that // TryAddSingleton in AddOutboxCore() is a no-op. diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSagaStoreTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSagaStoreTests.cs index 3496674730d..c854ae3c35a 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSagaStoreTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSagaStoreTests.cs @@ -202,7 +202,7 @@ public ValueTask DisposeAsync() { connectionString ??= await _fixture.CreateDatabaseAsync(); - var options = new DbContextOptionsBuilder().UseNpgsql(connectionString).Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql(connectionString).Options; var context = new TestDbContext(options); await context.Database.EnsureCreatedAsync(); diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSchedulingIntegrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSchedulingIntegrationTests.cs index 3ddb71233eb..5ecadfa63a5 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSchedulingIntegrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/PostgresSchedulingIntegrationTests.cs @@ -1,7 +1,6 @@ using System.Collections.Concurrent; using System.Text.Json; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers; @@ -80,8 +79,7 @@ public async Task Scheduler_Should_ProcessPendingMessages_When_WorkerStartsAfter var services = new ServiceCollection(); services.AddSingleton(recorder); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(connectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(connectionString)); services.AddSingleton(new ResilientSchedulerSignal()); var builder = services.AddMessageBus(); @@ -497,8 +495,7 @@ private async Task CreateBusWithSchedulingAsync( var services = new ServiceCollection(); services.AddSingleton(recorder); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(connectionString) - .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + services.AddDbContext(o => o.UseTestNpgsql(connectionString)); // Register the resilient signal BEFORE UsePostgresScheduling() so that // TryAddSingleton in UseSchedulerCore() is a no-op. diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaServiceRegistrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaServiceRegistrationTests.cs index e89bc070bb1..66b2d9b71a3 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaServiceRegistrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaServiceRegistrationTests.cs @@ -16,7 +16,7 @@ public sealed class SagaServiceRegistrationTests public void Create_Should_ReturnSagaStore_When_ValidDependencies() { // Arrange - var options = new DbContextOptionsBuilder().UseNpgsql(ConnectionString).Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql(ConnectionString).Options; using var context = new TestDbContext(options); var queries = PostgresSagaStoreQueries.From(new SagaStateTableInfo()); @@ -86,7 +86,7 @@ private static ServiceProvider BuildProvider() var services = new ServiceCollection(); services.AddLogging(); services.AddSingleton(TimeProvider.System); - services.AddDbContext(o => o.UseNpgsql(ConnectionString)); + services.AddDbContext(o => o.UseTestNpgsql(ConnectionString)); var builder = services.AddMessageBus(); builder.AddEntityFramework(ef => ef.AddPostgresSagas()); diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaStateEntityConfigurationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaStateEntityConfigurationTests.cs index afa4337e560..ba1f87341bd 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaStateEntityConfigurationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SagaStateEntityConfigurationTests.cs @@ -77,7 +77,7 @@ private static IEntityType GetSagaStateEntityType() private static IModel CreateModel() { - var options = new DbContextOptionsBuilder().UseNpgsql("Host=localhost").Options; + var options = new DbContextOptionsBuilder().UseTestNpgsql("Host=localhost").Options; using var context = new TestDbContext(options); return context.GetService().Model; } diff --git a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SchedulingServiceRegistrationTests.cs b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SchedulingServiceRegistrationTests.cs index 1c9db3accd5..72e34186e31 100644 --- a/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SchedulingServiceRegistrationTests.cs +++ b/src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SchedulingServiceRegistrationTests.cs @@ -1,4 +1,3 @@ -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers; @@ -45,7 +44,7 @@ private static ServiceProvider BuildProvider() { var services = new ServiceCollection(); services.AddLogging(); - services.AddDbContext(o => o.UseNpgsql(ConnectionString)); + services.AddDbContext(o => o.UseTestNpgsql(ConnectionString)); var builder = services.AddMessageBus(); builder.AddEntityFramework(ef => ef.UsePostgresScheduling());