From ed93de8393b029b1fb4199e96d0d12c94459287b Mon Sep 17 00:00:00 2001 From: AtiyyahRawat Date: Mon, 11 Mar 2024 10:16:37 +0000 Subject: [PATCH 1/2] Let multiple instances of DynamoDbHealthCheck be registered --- .../DynamoDbHealthCheckExtensions.cs | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Hackney.Core/Hackney.Core.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensions.cs b/Hackney.Core/Hackney.Core.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensions.cs index 9d7b58c..fa4807c 100644 --- a/Hackney.Core/Hackney.Core.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensions.cs +++ b/Hackney.Core/Hackney.Core.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensions.cs @@ -1,34 +1,34 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Diagnostics.HealthChecks; - -namespace Hackney.Core.DynamoDb.HealthCheck -{ - public static class DynamoDbHealthCheckExtensions - { - private const string Name = "DynamoDb"; - - public static IServiceCollection RegisterDynamoDbHealthCheck(this IServiceCollection services) where T : class - { - return services.AddSingleton>(); - } - - public static IHealthChecksBuilder AddDynamoDbHealthCheck(this IHealthChecksBuilder builder) where T : class - { - return builder.AddCheck>(Name); - } - - /// - /// Adds a health check to verify connectivity to the DynamoDb table used by T. - /// - /// The database model class used to determine the DynamoDb table name. - /// The . - /// The . - public static IServiceCollection AddDynamoDbHealthCheck(this IServiceCollection services) where T : class - { - services.RegisterDynamoDbHealthCheck(); - services.AddHealthChecks() - .AddDynamoDbHealthCheck(); - return services; - } - } -} +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace Hackney.Core.DynamoDb.HealthCheck +{ + public static class DynamoDbHealthCheckExtensions + { + private const string Name = "DynamoDb_"; + + public static IServiceCollection RegisterDynamoDbHealthCheck(this IServiceCollection services) where T : class + { + return services.AddSingleton>(); + } + + public static IHealthChecksBuilder AddDynamoDbHealthCheck(this IHealthChecksBuilder builder) where T : class + { + return builder.AddCheck>(Name + typeof(T).Name); + } + + /// + /// Adds a health check to verify connectivity to the DynamoDb table used by T. + /// + /// The database model class used to determine the DynamoDb table name. + /// The . + /// The . + public static IServiceCollection AddDynamoDbHealthCheck(this IServiceCollection services) where T : class + { + services.RegisterDynamoDbHealthCheck(); + services.AddHealthChecks() + .AddDynamoDbHealthCheck(); + return services; + } + } +} From 1be680797f09b424d5d166a925169635def86e49 Mon Sep 17 00:00:00 2001 From: AtiyyahRawat Date: Mon, 11 Mar 2024 10:20:38 +0000 Subject: [PATCH 2/2] Fix tests --- .../DynamoDbHealthCheckExtensionsTests.cs | 90 +++++++++---------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/Hackney.Core.Tests/Hackney.Core.Tests.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensionsTests.cs b/Hackney.Core.Tests/Hackney.Core.Tests.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensionsTests.cs index 3dc76ef..423ba00 100644 --- a/Hackney.Core.Tests/Hackney.Core.Tests.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensionsTests.cs +++ b/Hackney.Core.Tests/Hackney.Core.Tests.DynamoDb/HealthCheck/DynamoDbHealthCheckExtensionsTests.cs @@ -1,45 +1,45 @@ -using FluentAssertions; -using Hackney.Core.DynamoDb.HealthCheck; -using Hackney.Core.Testing.Shared; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Diagnostics.HealthChecks; -using Moq; -using Xunit; - -namespace Hackney.Core.Tests.DynamoDb.HealthCheck -{ - public class DynamoDbHealthCheckExtensionsTests - { - - [Fact] - public void RegisterDynamoDbHealthCheckTest() - { - var services = new ServiceCollection(); - _ = services.RegisterDynamoDbHealthCheck(); - - services.IsServiceRegistered>().Should().BeTrue(); - } - - [Fact] - public void ServiceCollectionAddDynamoDbHealthCheckTest() - { - var services = new ServiceCollection(); - _ = services.AddDynamoDbHealthCheck(); - - services.IsServiceRegistered>().Should().BeTrue(); - - // We can't explicitly verify the Healthcheck builder reigstration here as it is not accessible. - // We have to reply on the test below to do that for us. - } - - [Fact] - public void HealthChecksBuilderAddDynamoDbHealthCheckTest() - { - var mockBuilder = new Mock(); - _ = mockBuilder.Object.AddDynamoDbHealthCheck(); - - mockBuilder.Verify(x => x.Add(It.Is(hcr => hcr.Name == "DynamoDb" - && hcr.Factory != null)), Times.Once); - } - } -} +using FluentAssertions; +using Hackney.Core.DynamoDb.HealthCheck; +using Hackney.Core.Testing.Shared; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Moq; +using Xunit; + +namespace Hackney.Core.Tests.DynamoDb.HealthCheck +{ + public class DynamoDbHealthCheckExtensionsTests + { + + [Fact] + public void RegisterDynamoDbHealthCheckTest() + { + var services = new ServiceCollection(); + _ = services.RegisterDynamoDbHealthCheck(); + + services.IsServiceRegistered>().Should().BeTrue(); + } + + [Fact] + public void ServiceCollectionAddDynamoDbHealthCheckTest() + { + var services = new ServiceCollection(); + _ = services.AddDynamoDbHealthCheck(); + + services.IsServiceRegistered>().Should().BeTrue(); + + // We can't explicitly verify the Healthcheck builder reigstration here as it is not accessible. + // We have to reply on the test below to do that for us. + } + + [Fact] + public void HealthChecksBuilderAddDynamoDbHealthCheckTest() + { + var mockBuilder = new Mock(); + _ = mockBuilder.Object.AddDynamoDbHealthCheck(); + + mockBuilder.Verify(x => x.Add(It.Is(hcr => hcr.Name == "DynamoDb_" + typeof(TestModelDb).Name + && hcr.Factory != null)), Times.Once); + } + } +}