From 133fd979217b8cbce07ea3ca99423eda3fb6f37f Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Fri, 13 Dec 2024 19:50:44 +0330 Subject: [PATCH] Address PR feedback --- src/HealthChecks.Nats/NatsHealthCheck.cs | 2 ++ .../Functional/NatsHealthCheckTests.cs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/HealthChecks.Nats/NatsHealthCheck.cs b/src/HealthChecks.Nats/NatsHealthCheck.cs index 370ebe8905..b10a12b647 100644 --- a/src/HealthChecks.Nats/NatsHealthCheck.cs +++ b/src/HealthChecks.Nats/NatsHealthCheck.cs @@ -10,6 +10,8 @@ public sealed class NatsHealthCheck(INatsConnection connection) : IHealthCheck { public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { + await TryConnectAsync(connection).ConfigureAwait(false); + var result = connection.ConnectionState switch { NatsConnectionState.Open => HealthCheckResult.Healthy(), diff --git a/test/HealthChecks.Nats.Tests/Functional/NatsHealthCheckTests.cs b/test/HealthChecks.Nats.Tests/Functional/NatsHealthCheckTests.cs index c851e6cb5d..5aeb63d843 100644 --- a/test/HealthChecks.Nats.Tests/Functional/NatsHealthCheckTests.cs +++ b/test/HealthChecks.Nats.Tests/Functional/NatsHealthCheckTests.cs @@ -1,5 +1,6 @@ using System.Net; using NATS.Client.Core; +using NATS.Client.Hosting; using static HealthChecks.Nats.Tests.Defines; namespace HealthChecks.Nats.Tests.Functional; @@ -12,17 +13,16 @@ public async Task be_healthy_when_nats_is_available_using_client_factory() var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { + var options = NatsOpts.Default with + { + Url = DefaultLocalConnectionString, + }; + var natsConnection = new NatsConnection(options); + services .AddHealthChecks() .AddNats( - clientFactory: sp => - { - var options = NatsOpts.Default with - { - Url = DefaultLocalConnectionString, - }; - return new NatsConnection(options); - }, tags: new string[] { "nats" }); + clientFactory: sp => natsConnection, tags: new string[] { "nats" }); }) .Configure(app => {