From 074bcea6bf03a8b9e3eb185c063a34de89636cf5 Mon Sep 17 00:00:00 2001 From: Mateusz Cieciura Date: Mon, 4 Sep 2023 15:23:43 +0200 Subject: [PATCH] Nats :: Add overload for NatsOptions with IServiceProvider --- .../NatsHealthCheckBuilderExtensions.cs | 40 +++++++++++++++++++ .../DependencyInjection/RegistrationTests.cs | 31 ++++++++++++++ .../HealthChecks.Nats.approved.txt | 1 + 3 files changed, 72 insertions(+) diff --git a/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs b/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs index 402c313b6a..c3a0511889 100644 --- a/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs +++ b/src/HealthChecks.Nats/DependencyInjection/NatsHealthCheckBuilderExtensions.cs @@ -48,4 +48,44 @@ public static IHealthChecksBuilder AddNats( tags, timeout)); } + + /// + /// Add a health check for Nats. + /// + /// The . + /// The factory to configure the Nats setup. + /// + /// The health check name. + /// Optional. + /// If the type name 'nats' will be used for the name. + /// + /// + /// The that should be reported when the health check fails. + /// Optional. + /// If then the default status of will be reported. + /// + /// A list of tags that can be used to filter sets of health checks. Optional. + /// An optional System.TimeSpan representing the timeout of the check. + /// The . + public static IHealthChecksBuilder AddNats( + this IHealthChecksBuilder builder, + Action? setup, + string? name = default, + HealthStatus? failureStatus = default, + IEnumerable? tags = default, + TimeSpan? timeout = default) + { + return builder.Add(new HealthCheckRegistration( + name ?? NAME, + sp => + { + var options = new NatsOptions(); + setup?.Invoke(sp, options); + + return new NatsHealthCheck(options); + }, + failureStatus, + tags, + timeout)); + } } diff --git a/test/HealthChecks.Nats.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.Nats.Tests/DependencyInjection/RegistrationTests.cs index eb15ec85d2..659ed35758 100644 --- a/test/HealthChecks.Nats.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.Nats.Tests/DependencyInjection/RegistrationTests.cs @@ -23,6 +23,27 @@ public void add_health_check_when_demo_instance_properly_configured() => setup => setup.Url = DemoConnectionString, check => check.ShouldBeOfType()); + [Fact] + public void add_health_check_with_service_provider() + { + var services = new ServiceCollection(); + + services + .AddSingleton() + .AddHealthChecks() + .AddNats((sp, setup) => setup.Url = sp.GetRequiredService().ConnectionString); + + var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + registration.Name.ShouldBe(NatsName); + + var check = registration.Factory(serviceProvider); + + check.ShouldBeOfType(); + } + private void RegistrationFact(Action setup, Action assert, string? name = null) { var services = new ServiceCollection(); @@ -37,4 +58,14 @@ private void RegistrationFact(Action setup, Action as var check = registration.Factory(serviceProvider); assert(check); } + + private interface IDependency + { + string ConnectionString { get; } + } + + private class Dependency : IDependency + { + public string ConnectionString => DemoConnectionString; + } } diff --git a/test/HealthChecks.Nats.Tests/HealthChecks.Nats.approved.txt b/test/HealthChecks.Nats.Tests/HealthChecks.Nats.approved.txt index e4a78e367f..a27f9084c3 100644 --- a/test/HealthChecks.Nats.Tests/HealthChecks.Nats.approved.txt +++ b/test/HealthChecks.Nats.Tests/HealthChecks.Nats.approved.txt @@ -20,5 +20,6 @@ namespace Microsoft.Extensions.DependencyInjection public static class NatsHealthCheckBuilderExtensions { public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNats(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Action? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } + public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNats(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Action? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable? tags = null, System.TimeSpan? timeout = default) { } } } \ No newline at end of file