From f24c3c9c4f4f681638301c62de20434746fbaafc Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 9 May 2023 01:29:10 +0330 Subject: [PATCH] fix: Fix problem run persist-message background job service in test-base --- .editorconfig | 5 ++ src/BuildingBlocks/BuildingBlocks.csproj | 1 - ....cs => 20230508215131_initial.Designer.cs} | 2 +- ...3_initial.cs => 20230508215131_initial.cs} | 0 .../PersistMessageProcessor/Extensions.cs | 22 +++--- src/BuildingBlocks/TestBase/TestBase.cs | 67 ++++++++++++------- .../InfrastructureExtensions.cs | 2 +- .../InfrastructureExtensions.cs | 2 +- .../Aircraft/Features/CreateAircraftTests.cs | 2 - .../Airport/Features/CreateAirportTests.cs | 2 - .../InfrastructureExtensions.cs | 2 +- .../InfrastructureExtensions.cs | 2 +- 12 files changed, 65 insertions(+), 44 deletions(-) rename src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/{20230331173133_initial.Designer.cs => 20230508215131_initial.Designer.cs} (98%) rename src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/{20230331173133_initial.cs => 20230508215131_initial.cs} (100%) diff --git a/.editorconfig b/.editorconfig index 406cd49..78c58c6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -373,6 +373,11 @@ dotnet_naming_rule.sanity_check_uncovered_field_case_rule.symbols = sanity_chec dotnet_naming_rule.sanity_check_uncovered_field_case_rule.style = internal_error_style dotnet_naming_rule.sanity_check_uncovered_field_case_rule.severity = error +########################################## +# VSThread +########################################## +dotnet_diagnostic.VSTHRD111.severity = none + ########################################## # Other Naming Rules diff --git a/src/BuildingBlocks/BuildingBlocks.csproj b/src/BuildingBlocks/BuildingBlocks.csproj index 88a969d..81ab599 100644 --- a/src/BuildingBlocks/BuildingBlocks.csproj +++ b/src/BuildingBlocks/BuildingBlocks.csproj @@ -159,7 +159,6 @@ - diff --git a/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230331173133_initial.Designer.cs b/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230508215131_initial.Designer.cs similarity index 98% rename from src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230331173133_initial.Designer.cs rename to src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230508215131_initial.Designer.cs index 1200bdf..847117a 100644 --- a/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230331173133_initial.Designer.cs +++ b/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230508215131_initial.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace BuildingBlocks.PersistMessageProcessor.Data.Migrations { [DbContext(typeof(PersistMessageDbContext))] - [Migration("20230331173133_initial")] + [Migration("20230508215131_initial")] partial class initial { /// diff --git a/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230331173133_initial.cs b/src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230508215131_initial.cs similarity index 100% rename from src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230331173133_initial.cs rename to src/BuildingBlocks/PersistMessageProcessor/Data/Migrations/20230508215131_initial.cs diff --git a/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs b/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs index bfa6eb3..c20aa9a 100644 --- a/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs +++ b/src/BuildingBlocks/PersistMessageProcessor/Extensions.cs @@ -10,7 +10,8 @@ using Microsoft.AspNetCore.Hosting; public static class Extensions { - public static IServiceCollection AddPersistMessageProcessor(this IServiceCollection services) + public static IServiceCollection AddPersistMessageProcessor(this IServiceCollection services, + IWebHostEnvironment env) { AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); @@ -21,23 +22,28 @@ public static class Extensions var persistMessageOptions = sp.GetRequiredService(); options.UseNpgsql(persistMessageOptions.ConnectionString, - dbOptions => - { - dbOptions.MigrationsAssembly(typeof(PersistMessageDbContext).Assembly.GetName().Name); - }) + dbOptions => + { + dbOptions.MigrationsAssembly(typeof(PersistMessageDbContext).Assembly.GetName().Name); + }) // https://github.com/efcore/EFCore.NamingConventions - .UseSnakeCaseNamingConvention();; + .UseSnakeCaseNamingConvention(); }); services.AddScoped(provider => provider.GetService()); services.AddScoped(); - services.AddHostedService(); + + if (env.EnvironmentName != "test") + { + services.AddHostedService(); + } return services; } - public static IApplicationBuilder UseMigrationPersistMessage(this IApplicationBuilder app, IWebHostEnvironment env) + public static IApplicationBuilder UseMigrationPersistMessage(this IApplicationBuilder app, + IWebHostEnvironment env) where TContext : DbContext, IPersistMessageDbContext { using var scope = app.ApplicationServices.CreateScope(); diff --git a/src/BuildingBlocks/TestBase/TestBase.cs b/src/BuildingBlocks/TestBase/TestBase.cs index 5b51f46..178fce4 100644 --- a/src/BuildingBlocks/TestBase/TestBase.cs +++ b/src/BuildingBlocks/TestBase/TestBase.cs @@ -5,7 +5,6 @@ using BuildingBlocks.EFCore; using BuildingBlocks.Mongo; using BuildingBlocks.PersistMessageProcessor; using BuildingBlocks.Web; -using DotNet.Testcontainers.Containers; using EasyNetQ.Management.Client; using Grpc.Net.Client; using MassTransit; @@ -49,6 +48,10 @@ public class TestFixture : IAsyncLifetime public RabbitMqContainer RabbitMqTestContainer; public MongoDbContainer MongoDbTestContainer; public EventStoreDbContainer EventStoreDbTestContainer; + public CancellationTokenSource CancellationTokenSource; + + public PersistMessageBackgroundService PersistMessageBackgroundService => + ServiceProvider.GetRequiredService(); public HttpClient HttpClient { @@ -85,6 +88,12 @@ public class TestFixture : IAsyncLifetime TestRegistrationServices?.Invoke(services); services.ReplaceSingleton(AddHttpContextAccessorMock); + services.AddSingleton(); + + // // remove persist-message processor background service + // var descriptor = services.Single(s => s.ImplementationType == typeof(PersistMessageBackgroundService)); + // services.Remove(descriptor); + // add authentication using a fake jwt bearer - we can use SetAdminUser method to set authenticate user to existing HttContextAccessor // https://github.com/webmotions/fake-authentication-jwtbearer // https://github.com/webmotions/fake-authentication-jwtbearer/issues/14 @@ -99,6 +108,7 @@ public class TestFixture : IAsyncLifetime public async Task InitializeAsync() { + CancellationTokenSource = new CancellationTokenSource(); await StartTestContainerAsync(); } @@ -106,6 +116,7 @@ public class TestFixture : IAsyncLifetime { await StopTestContainerAsync(); await _factory.DisposeAsync(); + CancellationTokenSource.Cancel(); } public virtual void RegisterServices(Action services) @@ -141,6 +152,7 @@ public class TestFixture : IAsyncLifetime return result; } + public Task SendAsync(IRequest request) { return ExecuteScopeAsync(sp => @@ -173,7 +185,6 @@ public class TestFixture : IAsyncLifetime { var published = await TestHarness.Published.Any(cancellationToken); var faulty = await TestHarness.Published.Any>(cancellationToken); - return published && faulty == false; }); return result; @@ -193,6 +204,30 @@ public class TestFixture : IAsyncLifetime return result; } + public async Task ShouldProcessedPersistInternalCommand( + CancellationToken cancellationToken = default) + where TInternalCommand : class, IInternalCommand + { + var result = await WaitUntilConditionMet(async () => + { + return await ExecuteScopeAsync(async sp => + { + var persistMessageProcessor = sp.GetService(); + Guard.Against.Null(persistMessageProcessor, nameof(persistMessageProcessor)); + + var filter = await persistMessageProcessor.GetByFilterAsync(x => + x.DeliveryType == MessageDeliveryType.Internal && + typeof(TInternalCommand).ToString() == x.DataType); + + var res = filter.Any(x => x.MessageStatus == MessageStatus.Processed); + + return res; + }); + }); + + return result; + } + // Ref: https://tech.energyhelpline.com/in-memory-testing-with-masstransit/ private async Task WaitUntilConditionMet(Func> conditionToMet, int? timeoutSecond = null) { @@ -216,30 +251,6 @@ public class TestFixture : IAsyncLifetime return true; } - public async Task ShouldProcessedPersistInternalCommand() - where TInternalCommand : class, IInternalCommand - { - var result = await WaitUntilConditionMet(async () => - { - return await ExecuteScopeAsync(async sp => - { - var persistMessageProcessor = sp.GetService(); - Guard.Against.Null(persistMessageProcessor, nameof(persistMessageProcessor)); - - var filter = await persistMessageProcessor.GetByFilterAsync(x => - x.DeliveryType == MessageDeliveryType.Internal && - typeof(TInternalCommand).ToString() == x.DataType); - - var res = filter.Any(x => x.MessageStatus == MessageStatus.Processed); - - return res; - }); - }); - - return result; - } - - private async Task StartTestContainerAsync() { PostgresTestcontainer = TestContainers.PostgresTestContainer(); @@ -496,6 +507,8 @@ public class TestFixtureCore : IAsyncLifetime if (!string.IsNullOrEmpty(persistOptions?.ConnectionString)) { + await Fixture.PersistMessageBackgroundService.StartAsync(Fixture.CancellationTokenSource.Token); + PersistDbConnection = new NpgsqlConnection(persistOptions.ConnectionString); await PersistDbConnection.OpenAsync(); @@ -520,6 +533,8 @@ public class TestFixtureCore : IAsyncLifetime if (PersistDbConnection is not null) { await _reSpawnerPersistDb.ResetAsync(PersistDbConnection); + + await Fixture.PersistMessageBackgroundService.StopAsync(Fixture.CancellationTokenSource.Token); } if (DefaultDbConnection is not null) diff --git a/src/Services/Booking/src/Booking/Extensions/Infrastructure/InfrastructureExtensions.cs b/src/Services/Booking/src/Booking/Extensions/Infrastructure/InfrastructureExtensions.cs index c0aa996..2094ad5 100644 --- a/src/Services/Booking/src/Booking/Extensions/Infrastructure/InfrastructureExtensions.cs +++ b/src/Services/Booking/src/Booking/Extensions/Infrastructure/InfrastructureExtensions.cs @@ -57,7 +57,7 @@ public static class InfrastructureExtensions })); }); - builder.Services.AddPersistMessageProcessor(); + builder.Services.AddPersistMessageProcessor(env); builder.Services.AddMongoDbContext(configuration); builder.AddCustomSerilog(env); diff --git a/src/Services/Flight/src/Flight/Extensions/Infrastructure/InfrastructureExtensions.cs b/src/Services/Flight/src/Flight/Extensions/Infrastructure/InfrastructureExtensions.cs index 04cc878..6729aea 100644 --- a/src/Services/Flight/src/Flight/Extensions/Infrastructure/InfrastructureExtensions.cs +++ b/src/Services/Flight/src/Flight/Extensions/Infrastructure/InfrastructureExtensions.cs @@ -65,7 +65,7 @@ public static class InfrastructureExtensions builder.Services.AddCustomDbContext(); builder.Services.AddScoped(); builder.Services.AddMongoDbContext(configuration); - builder.Services.AddPersistMessageProcessor(); + builder.Services.AddPersistMessageProcessor(env); builder.AddCustomSerilog(env); builder.Services.AddJwt(); diff --git a/src/Services/Flight/tests/IntegrationTest/Aircraft/Features/CreateAircraftTests.cs b/src/Services/Flight/tests/IntegrationTest/Aircraft/Features/CreateAircraftTests.cs index a17b8fd..352c9a7 100644 --- a/src/Services/Flight/tests/IntegrationTest/Aircraft/Features/CreateAircraftTests.cs +++ b/src/Services/Flight/tests/IntegrationTest/Aircraft/Features/CreateAircraftTests.cs @@ -9,8 +9,6 @@ using Xunit; namespace Integration.Test.Aircraft.Features; -using global::Flight.Aircrafts.Features.CreatingAircraft.V1; - public class CreateAircraftTests : FlightIntegrationTestBase { public CreateAircraftTests( diff --git a/src/Services/Flight/tests/IntegrationTest/Airport/Features/CreateAirportTests.cs b/src/Services/Flight/tests/IntegrationTest/Airport/Features/CreateAirportTests.cs index efe9f84..90e4fe6 100644 --- a/src/Services/Flight/tests/IntegrationTest/Airport/Features/CreateAirportTests.cs +++ b/src/Services/Flight/tests/IntegrationTest/Airport/Features/CreateAirportTests.cs @@ -9,8 +9,6 @@ using Xunit; namespace Integration.Test.Airport.Features; -using global::Flight.Airports.Features.CreatingAirport.V1; - public class CreateAirportTests : FlightIntegrationTestBase { public CreateAirportTests( diff --git a/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs b/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs index dda6493..3525b27 100644 --- a/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs +++ b/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs @@ -59,7 +59,7 @@ public static class InfrastructureExtensions }); builder.Services.AddControllers(); - builder.Services.AddPersistMessageProcessor(); + builder.Services.AddPersistMessageProcessor(env); builder.Services.AddCustomDbContext(); builder.Services.AddScoped(); builder.AddCustomSerilog(env); diff --git a/src/Services/Passenger/src/Passenger/Extensions/Infrastructure/InfrastructureExtensions.cs b/src/Services/Passenger/src/Passenger/Extensions/Infrastructure/InfrastructureExtensions.cs index baac1d9..26ed274 100644 --- a/src/Services/Passenger/src/Passenger/Extensions/Infrastructure/InfrastructureExtensions.cs +++ b/src/Services/Passenger/src/Passenger/Extensions/Infrastructure/InfrastructureExtensions.cs @@ -58,7 +58,7 @@ public static class InfrastructureExtensions })); }); - builder.Services.AddPersistMessageProcessor(); + builder.Services.AddPersistMessageProcessor(env); builder.Services.AddCustomDbContext(); builder.Services.AddMongoDbContext(configuration);