Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<TContext> UseTestNpgsql<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder,
string connectionString)
where TContext : DbContext
=> optionsBuilder.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -112,7 +111,7 @@ private static ServiceProvider BuildProvider(Action<InboxOptions>? configure = n
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(ConnectionString));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(ConnectionString));

// Use a resilient signal to prevent ObjectDisposedException when
// EF Core shares the internal service provider (and interceptors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static IEntityType GetOutboxMessageEntityType()

private static IModel CreateModel()
{
var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql("Host=localhost").Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql("Host=localhost").Options;
using var context = new TestDbContext(options);
return context.GetService<IDesignTimeModel>().Model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -94,8 +92,7 @@ private static ServiceProvider BuildProvider()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(ConnectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(ConnectionString));

// Use a resilient signal to prevent ObjectDisposedException when
// EF Core shares the internal service provider (and interceptors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public async Task CleanupAsync_Should_DeleteMessages_When_TimeProviderAdvancedPa
{
var connectionString = await _fixture.CreateDatabaseAsync();
var options = new DbContextOptionsBuilder<TestDbContext>()
.UseNpgsql(connectionString)
.UseTestNpgsql(connectionString)
.Options;
var context = new TestDbContext(options);
await context.Database.EnsureCreatedAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public async Task PersistAsync_Should_SerializeEnvelopeAsJson_When_Called()
public void Dispose_Should_NotThrow_When_Called()
{
// Arrange
var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql("Host=localhost;Database=test").Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql("Host=localhost;Database=test").Options;
using var context = new TestDbContext(options);
var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo());
var signal = new StubOutboxSignal();
Expand All @@ -130,7 +130,7 @@ public void Dispose_Should_NotThrow_When_Called()
StubOutboxSignal Signal)> CreateOutboxAsync()
{
var connectionString = await _fixture.CreateDatabaseAsync();
var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql(connectionString).Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql(connectionString).Options;
var context = new TestDbContext(options);
await context.Database.EnsureCreatedAsync();
var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -103,8 +102,7 @@ public async Task Outbox_Should_ProcessPendingMessages_When_WorkerStartsAfterPer
var services = new ServiceCollection();
services.AddSingleton(recorder);
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(connectionString));
services.AddSingleton<IOutboxSignal, ResilientOutboxSignal>();

var builder = services.AddMessageBus();
Expand Down Expand Up @@ -180,8 +178,7 @@ public async Task Outbox_Should_ResumeProcessing_When_WorkerRestartedAfterInterr
var services = new ServiceCollection();
services.AddSingleton(recorder);
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(connectionString));
services.AddSingleton<IOutboxSignal, ResilientOutboxSignal>();

var builder = services.AddMessageBus();
Expand Down Expand Up @@ -336,8 +333,7 @@ private async Task<TestEnvironment> CreateBusWithOutboxAsync(MessageRecorder rec
var services = new ServiceCollection();
services.AddSingleton(recorder);
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(connectionString));

// Register the resilient signal BEFORE UsePostgresOutbox() so that
// TryAddSingleton<IOutboxSignal> in AddOutboxCore() is a no-op.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public ValueTask DisposeAsync()
{
connectionString ??= await _fixture.CreateDatabaseAsync();

var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql(connectionString).Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql(connectionString).Options;

var context = new TestDbContext(options);
await context.Database.EnsureCreatedAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,8 +79,7 @@ public async Task Scheduler_Should_ProcessPendingMessages_When_WorkerStartsAfter
var services = new ServiceCollection();
services.AddSingleton(recorder);
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(connectionString));
services.AddSingleton<ISchedulerSignal>(new ResilientSchedulerSignal());

var builder = services.AddMessageBus();
Expand Down Expand Up @@ -497,8 +495,7 @@ private async Task<TestEnvironment> CreateBusWithSchedulingAsync(
var services = new ServiceCollection();
services.AddSingleton(recorder);
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(connectionString)
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(connectionString));

// Register the resilient signal BEFORE UsePostgresScheduling() so that
// TryAddSingleton<ISchedulerSignal> in UseSchedulerCore() is a no-op.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class SagaServiceRegistrationTests
public void Create_Should_ReturnSagaStore_When_ValidDependencies()
{
// Arrange
var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql(ConnectionString).Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql(ConnectionString).Options;

using var context = new TestDbContext(options);
var queries = PostgresSagaStoreQueries.From(new SagaStateTableInfo());
Expand Down Expand Up @@ -86,7 +86,7 @@ private static ServiceProvider BuildProvider()
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton(TimeProvider.System);
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(ConnectionString));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(ConnectionString));

var builder = services.AddMessageBus();
builder.AddEntityFramework<TestDbContext>(ef => ef.AddPostgresSagas());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static IEntityType GetSagaStateEntityType()

private static IModel CreateModel()
{
var options = new DbContextOptionsBuilder<TestDbContext>().UseNpgsql("Host=localhost").Options;
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql("Host=localhost").Options;
using var context = new TestDbContext(options);
return context.GetService<IDesignTimeModel>().Model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers;
Expand Down Expand Up @@ -45,7 +44,7 @@ private static ServiceProvider BuildProvider()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<TestDbContext>(o => o.UseNpgsql(ConnectionString));
services.AddDbContext<TestDbContext>(o => o.UseTestNpgsql(ConnectionString));

var builder = services.AddMessageBus();
builder.AddEntityFramework<TestDbContext>(ef => ef.UsePostgresScheduling());
Expand Down
Loading