Skip to content

Commit

Permalink
fix: fix issue in registration persist message background service in …
Browse files Browse the repository at this point in the history
…test base
  • Loading branch information
meysamhadeli committed Dec 19, 2024
1 parent b9aa18a commit 321b7ce
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/BuildingBlocks/EFCore/IDataSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public interface IDataSeeder
Task SeedAllAsync();
}

public interface ITestDataSeeder : IDataSeeder
public interface ITestDataSeeder
{
Task SeedAllAsync();
}
}
6 changes: 3 additions & 3 deletions src/BuildingBlocks/EFCore/SeedManagers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task ExecuteSeedAsync()

if (!env.IsEnvironment("test"))
{
foreach (var seeder in dataSeeders.Where(x => x is not ITestDataSeeder))
foreach (var seeder in dataSeeders)
{
logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name);
await seeder.SeedAllAsync();
Expand All @@ -30,9 +30,9 @@ public async Task ExecuteSeedAsync()
public async Task ExecuteTestSeedAsync()
{
await using var scope = serviceProvider.CreateAsyncScope();
var dataSeeders = scope.ServiceProvider.GetServices<IDataSeeder>();
var dataSeeders = scope.ServiceProvider.GetServices<ITestDataSeeder>();

foreach (var seeder in dataSeeders.Where(x => x is ITestDataSeeder))
foreach (var seeder in dataSeeders)
{
logger.LogInformation("Seed {SeederName} is started.", seeder.GetType().Name);
await seeder.SeedAllAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/BuildingBlocks/MassTransit/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace BuildingBlocks.MassTransit;
public static class Extensions
{
public static IServiceCollection AddCustomMassTransit(this IServiceCollection services,
IWebHostEnvironment env, Assembly assembly)
IWebHostEnvironment env, params Assembly[] assembly)
{
services.AddValidateOptions<RabbitMqOptions>();

Expand All @@ -32,7 +32,7 @@ public static IServiceCollection AddCustomMassTransit(this IServiceCollection se
}

private static void SetupMasstransitConfigurations(IServiceCollection services,
IBusRegistrationConfigurator configure, Assembly assembly)
IBusRegistrationConfigurator configure, params Assembly[] assembly)
{
configure.AddConsumers(assembly);
configure.AddSagaStateMachines(assembly);
Expand Down
5 changes: 1 addition & 4 deletions src/BuildingBlocks/PersistMessageProcessor/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ namespace BuildingBlocks.PersistMessageProcessor;

public static class Extensions
{
public static IServiceCollection AddPersistMessageProcessor(
this IServiceCollection services,
IWebHostEnvironment env
)
public static IServiceCollection AddPersistMessageProcessor(this IServiceCollection services)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
logger.LogInformation("PersistMessage Background Service Start");

await ProcessAsync(stoppingToken);

}

public override Task StopAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace BuildingBlocks.PersistMessageProcessor;

using Microsoft.EntityFrameworkCore;
using Polly;

public class PersistMessageProcessor : IPersistMessageProcessor
{
Expand Down
10 changes: 8 additions & 2 deletions src/BuildingBlocks/TestBase/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System.Globalization;
using System.Net;
using System.Reflection;
using System.Security.Claims;
using Ardalis.GuardClauses;
using BuildingBlocks.Core.Event;
using BuildingBlocks.Core.Model;
using BuildingBlocks.EFCore;
using BuildingBlocks.MassTransit;
using BuildingBlocks.Mongo;
using BuildingBlocks.PersistMessageProcessor;
using BuildingBlocks.Web;
using EasyNetQ.Management.Client;
using Grpc.Net.Client;
using MassTransit;
using MassTransit.Testing;
using MediatR;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -96,8 +99,11 @@ protected TestFixture()
{
TestRegistrationServices?.Invoke(services);
services.ReplaceSingleton(AddHttpContextAccessorMock);
services.RemoveAll<IHostedService>();
services.AddSingleton<PersistMessageBackgroundService>();

services.RemoveHostedService<PersistMessageBackgroundService>();
services.AddSingleton<PersistMessageBackgroundService>(); // Register as a singleton
services.AddHostedService(provider => provider.GetRequiredService<PersistMessageBackgroundService>()); // Use the same instance for hosted service


// Register all ITestDataSeeder implementations dynamically
services.Scan(scan => scan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder
}));
});

builder.Services.AddPersistMessageProcessor(env);
builder.Services.AddPersistMessageProcessor();
builder.Services.AddMongoDbContext<BookingReadDbContext>(configuration);

builder.Services.AddEndpointsApiExplorer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder
builder.Services.AddCustomDbContext<FlightDbContext>();
builder.Services.AddScoped<IDataSeeder, FlightDataSeeder>();
builder.Services.AddMongoDbContext<FlightReadDbContext>(configuration);
builder.Services.AddPersistMessageProcessor(env);
builder.Services.AddPersistMessageProcessor();

builder.Services.AddEndpointsApiExplorer();
builder.AddCustomSerilog(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddControllers();
builder.Services.AddPersistMessageProcessor(env);
builder.Services.AddPersistMessageProcessor();
builder.Services.AddCustomDbContext<IdentityContext>();
builder.Services.AddScoped<IDataSeeder, IdentityDataSeeder>();
builder.AddCustomSerilog(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder
}));
});

builder.Services.AddPersistMessageProcessor(env);
builder.Services.AddPersistMessageProcessor();
builder.Services.AddCustomDbContext<PassengerDbContext>();
builder.Services.AddMongoDbContext<PassengerReadDbContext>(configuration);

Expand Down

0 comments on commit 321b7ce

Please sign in to comment.