Skip to content

Commit

Permalink
Nats :: Add overload for NatsOptions with IServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
cieciurm committed Sep 4, 2023
1 parent c000704 commit 074bcea
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,44 @@ public static IHealthChecksBuilder AddNats(
tags,
timeout));
}

/// <summary>
/// Add a health check for Nats.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="setup">The factory to configure the Nats setup.</param>
/// <param name="name">
/// The health check name.
/// Optional.
/// If <see langword="null"/> the type name 'nats' will be used for the name.
/// </param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails.
/// Optional.
/// If <see langword="null"/> then the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional System.TimeSpan representing the timeout of the check.</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddNats(
this IHealthChecksBuilder builder,
Action<IServiceProvider, NatsOptions>? setup,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ public void add_health_check_when_demo_instance_properly_configured() =>
setup => setup.Url = DemoConnectionString,
check => check.ShouldBeOfType<NatsHealthCheck>());

[Fact]
public void add_health_check_with_service_provider()
{
var services = new ServiceCollection();

services
.AddSingleton<IDependency, Dependency>()
.AddHealthChecks()
.AddNats((sp, setup) => setup.Url = sp.GetRequiredService<IDependency>().ConnectionString);

var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
registration.Name.ShouldBe(NatsName);

var check = registration.Factory(serviceProvider);

check.ShouldBeOfType<NatsHealthCheck>();
}

private void RegistrationFact(Action<NatsOptions> setup, Action<IHealthCheck> assert, string? name = null)
{
var services = new ServiceCollection();
Expand All @@ -37,4 +58,14 @@ private void RegistrationFact(Action<NatsOptions> setup, Action<IHealthCheck> as
var check = registration.Factory(serviceProvider);
assert(check);
}

private interface IDependency
{
string ConnectionString { get; }
}

private class Dependency : IDependency
{
public string ConnectionString => DemoConnectionString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HealthChecks.Nats.NatsOptions>? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNats(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Action<System.IServiceProvider, HealthChecks.Nats.NatsOptions>? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
}
}

0 comments on commit 074bcea

Please sign in to comment.