Skip to content

Commit 8060f7f

Browse files
Spacemonkaysamtrion
authored andcommitted
fix: Add IDisposable pattern to ElasticsearcheHealth (#1080)
1 parent cd92740 commit 8060f7f

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/NetEvolve.HealthChecks.Elasticsearch/ElasticsearchHealthCheck.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
using System;
44
using System.Collections.Concurrent;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Threading.Tasks;
67
using Elastic.Clients.Elasticsearch;
8+
using Elastic.Clients.Elasticsearch.Graph;
79
using Elastic.Transport;
810
using Microsoft.Extensions.DependencyInjection;
911
using Microsoft.Extensions.Diagnostics.HealthChecks;
1012
using NetEvolve.Extensions.Tasks;
1113
using SourceGenerator.Attributes;
1214

1315
[ConfigurableHealthCheck(typeof(ElasticsearchOptions))]
14-
internal sealed partial class ElasticsearchHealthCheck
16+
internal sealed partial class ElasticsearchHealthCheck : IDisposable
1517
{
1618
private ConcurrentDictionary<string, ElasticsearchClient>? _clients;
19+
private bool _disposedValue;
1720

1821
private async ValueTask<HealthCheckResult> ExecuteHealthCheckAsync(
1922
string name,
@@ -79,4 +82,27 @@ private static ElasticsearchClient CreateClient(ElasticsearchOptions options)
7982

8083
return new ElasticsearchClient(settings);
8184
}
85+
86+
[SuppressMessage(
87+
"Blocker Code Smell",
88+
"S2953:Methods named \"Dispose\" should implement \"IDisposable.Dispose\"",
89+
Justification = "As designed."
90+
)]
91+
private void Dispose(bool disposing)
92+
{
93+
if (!_disposedValue)
94+
{
95+
if (disposing && _clients is not null)
96+
{
97+
_clients.Clear();
98+
}
99+
_disposedValue = true;
100+
}
101+
}
102+
103+
void IDisposable.Dispose()
104+
{
105+
Dispose(disposing: true);
106+
GC.SuppressFinalize(this);
107+
}
82108
}

tests/NetEvolve.HealthChecks.Tests.Unit/Elasticsearch/ElasticsearchHealthCheckTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task CheckHealthAsync_WhenContextNull_ThrowArgumentNullException()
2020
// Arrange
2121
var serviceProvider = Substitute.For<IServiceProvider>();
2222
var optionsMonitor = Substitute.For<IOptionsMonitor<ElasticsearchOptions>>();
23-
var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
23+
using var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
2424

2525
// Act
2626
async Task Act() => _ = await check.CheckHealthAsync(null!, default);
@@ -37,7 +37,7 @@ public async Task CheckHealthAsync_WhenCancellationTokenIsCancelled_ShouldReturn
3737
var serviceProvider = Substitute.For<IServiceProvider>();
3838
var optionsMonitor = Substitute.For<IOptionsMonitor<ElasticsearchOptions>>();
3939

40-
var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
40+
using var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
4141
var context = new HealthCheckContext
4242
{
4343
Registration = new HealthCheckRegistration(testName, check, null, null),
@@ -65,7 +65,7 @@ public async Task CheckHealthAsync_WhenOptionsAreNull_ShouldReturnUnhealthy()
6565
var serviceProvider = Substitute.For<IServiceProvider>();
6666
var optionsMonitor = Substitute.For<IOptionsMonitor<ElasticsearchOptions>>();
6767

68-
var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
68+
using var check = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
6969
var context = new HealthCheckContext
7070
{
7171
Registration = new HealthCheckRegistration(testName, check, null, null),
@@ -112,7 +112,7 @@ public async Task CheckHealthAsync_WithKeyedService_ShouldUseKeyedService()
112112

113113
var serviceProvider = new ServiceCollection().AddKeyedSingleton(serviceKey, client).BuildServiceProvider();
114114

115-
var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
115+
using var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
116116
var context = new HealthCheckContext
117117
{
118118
Registration = new HealthCheckRegistration(
@@ -163,7 +163,7 @@ public async Task CheckHealthAsync_WithoutKeyedService_ShouldUseDefaultService()
163163

164164
var serviceProvider = new ServiceCollection().AddSingleton(client).BuildServiceProvider();
165165

166-
var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
166+
using var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
167167
var context = new HealthCheckContext
168168
{
169169
Registration = new HealthCheckRegistration(
@@ -214,7 +214,7 @@ public async Task CheckHealthAsync_WhenConnectionFails_ShouldReturnUnhealthy()
214214

215215
var serviceProvider = new ServiceCollection().AddSingleton(client).BuildServiceProvider();
216216

217-
var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
217+
using var healthCheck = new ElasticsearchHealthCheck(serviceProvider, optionsMonitor);
218218
var context = new HealthCheckContext
219219
{
220220
Registration = new HealthCheckRegistration(

0 commit comments

Comments
 (0)