Skip to content

Commit

Permalink
Merge pull request #60 from LBHackney-IT/feature/let-multiple-dynamod…
Browse files Browse the repository at this point in the history
…b-healthchecks

Let APIs have multiple DynamoDB healthchecks
  • Loading branch information
humulla authored Mar 13, 2024
2 parents 3b4b3fd + 1be6807 commit 159f2d6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -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<TestModelDb>();

services.IsServiceRegistered<IHealthCheck, DynamoDbHealthCheck<TestModelDb>>().Should().BeTrue();
}

[Fact]
public void ServiceCollectionAddDynamoDbHealthCheckTest()
{
var services = new ServiceCollection();
_ = services.AddDynamoDbHealthCheck<TestModelDb>();

services.IsServiceRegistered<IHealthCheck, DynamoDbHealthCheck<TestModelDb>>().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<IHealthChecksBuilder>();
_ = mockBuilder.Object.AddDynamoDbHealthCheck<TestModelDb>();

mockBuilder.Verify(x => x.Add(It.Is<HealthCheckRegistration>(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<TestModelDb>();

services.IsServiceRegistered<IHealthCheck, DynamoDbHealthCheck<TestModelDb>>().Should().BeTrue();
}

[Fact]
public void ServiceCollectionAddDynamoDbHealthCheckTest()
{
var services = new ServiceCollection();
_ = services.AddDynamoDbHealthCheck<TestModelDb>();

services.IsServiceRegistered<IHealthCheck, DynamoDbHealthCheck<TestModelDb>>().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<IHealthChecksBuilder>();
_ = mockBuilder.Object.AddDynamoDbHealthCheck<TestModelDb>();

mockBuilder.Verify(x => x.Add(It.Is<HealthCheckRegistration>(hcr => hcr.Name == "DynamoDb_" + typeof(TestModelDb).Name
&& hcr.Factory != null)), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<T>(this IServiceCollection services) where T : class
{
return services.AddSingleton<IHealthCheck, DynamoDbHealthCheck<T>>();
}

public static IHealthChecksBuilder AddDynamoDbHealthCheck<T>(this IHealthChecksBuilder builder) where T : class
{
return builder.AddCheck<DynamoDbHealthCheck<T>>(Name);
}

/// <summary>
/// Adds a health check to verify connectivity to the DynamoDb table used by T.
/// </summary>
/// <typeparam name="T">The database model class used to determine the DynamoDb table name.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddDynamoDbHealthCheck<T>(this IServiceCollection services) where T : class
{
services.RegisterDynamoDbHealthCheck<T>();
services.AddHealthChecks()
.AddDynamoDbHealthCheck<T>();
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<T>(this IServiceCollection services) where T : class
{
return services.AddSingleton<IHealthCheck, DynamoDbHealthCheck<T>>();
}

public static IHealthChecksBuilder AddDynamoDbHealthCheck<T>(this IHealthChecksBuilder builder) where T : class
{
return builder.AddCheck<DynamoDbHealthCheck<T>>(Name + typeof(T).Name);
}

/// <summary>
/// Adds a health check to verify connectivity to the DynamoDb table used by T.
/// </summary>
/// <typeparam name="T">The database model class used to determine the DynamoDb table name.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddDynamoDbHealthCheck<T>(this IServiceCollection services) where T : class
{
services.RegisterDynamoDbHealthCheck<T>();
services.AddHealthChecks()
.AddDynamoDbHealthCheck<T>();
return services;
}
}
}

0 comments on commit 159f2d6

Please sign in to comment.