Skip to content

Commit

Permalink
add new tests,fix previes test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Jun 24, 2024
1 parent e8971ec commit 7b374ae
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,14 @@ public static IHealthChecksBuilder AddElasticsearch(
TimeSpan? timeout = default)
{

var options = new ElasticsearchOptions();
setup?.Invoke(options);

options.RequestTimeout ??= timeout;

return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp =>
{
var options = new ElasticsearchOptions();
options.RequestTimeout ??= timeout;
options.Client ??= clientFactory?.Invoke(sp) ?? sp.GetRequiredService<Elastic.Clients.Elasticsearch.ElasticsearchClient>();

setup?.Invoke(options);
return new ElasticsearchHealthCheck(options);
},
failureStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ public void add_named_health_check_when_properly_configured()
[Fact]
public void create_client_with_user_configured_request_timeout()
{
var connectionString = @"https://localhost:9200";

var services = new ServiceCollection();
var settings = new ElasticsearchOptions();
services.AddHealthChecks().AddElasticsearch(setup =>
{
setup = settings;
setup.UseServer(connectionString);
setup.RequestTimeout = new TimeSpan(0, 0, 6);
settings = setup;
});

//Ensure no further modifications were carried by extension method
Expand All @@ -55,9 +58,15 @@ public void create_client_with_user_configured_request_timeout()
[Fact]
public void create_client_with_configured_healthcheck_timeout_when_no_request_timeout_is_configured()
{
var connectionString = @"https://localhost:9200";
var services = new ServiceCollection();
var settings = new ElasticsearchOptions();
services.AddHealthChecks().AddElasticsearch(setup => settings = setup, timeout: new TimeSpan(0, 0, 7));

services.AddHealthChecks().AddElasticsearch(setup =>
{
setup.UseServer(connectionString);
settings = setup;
}, timeout: new TimeSpan(0, 0, 7));

settings.RequestTimeout.ShouldNotBeNull();
settings.RequestTimeout.ShouldBe(new TimeSpan(0, 0, 7));
Expand All @@ -66,10 +75,86 @@ public void create_client_with_configured_healthcheck_timeout_when_no_request_ti
[Fact]
public void create_client_with_no_timeout_when_no_option_is_configured()
{
var connectionString = @"https://localhost:9200";

var services = new ServiceCollection();
var settings = new ElasticsearchOptions();
services.AddHealthChecks().AddElasticsearch(setup => settings = setup);

services.AddHealthChecks().AddElasticsearch(setup =>
{
setup.UseServer(connectionString);
settings = setup;
});

settings.RequestTimeout.ShouldBeNull();
}
[Fact]
public void throw_exception_when_create_client_without_using_elasic_cloud_or_server()
{

var services = new ServiceCollection();
var settings = new ElasticsearchOptions();

Assert.Throws<InvalidOperationException>(() => services.AddHealthChecks().AddElasticsearch(setup => settings = setup));
}
[Fact]
public void create_client_when_using_elasic_cloud()
{

var services = new ServiceCollection();
var settings = new ElasticsearchOptions();

services.AddHealthChecks().AddElasticsearch(setup =>
{
setup.UseElasticCloud("cloudId", "cloudApiKey");
settings = setup;
});

settings.AuthenticateWithElasticCloud.ShouldBeTrue();
settings.CloudApiKey.ShouldNotBeNull();
settings.CloudId.ShouldNotBeNull();
}

[Fact]
public void client_should_resolve_from_di()
{
var client = new Elastic.Clients.Elasticsearch.ElasticsearchClient();
var services = new ServiceCollection();
var settings = new ElasticsearchOptions();
services.AddSingleton(client);

services.AddHealthChecks().AddElasticsearch(clientFactory: null, setup: (setup) => settings = setup);

using var serviceProvider = services.BuildServiceProvider();

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

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

check.ShouldBeOfType<ElasticsearchHealthCheck>();
settings.Client.ShouldNotBeNull();
settings.Client.ShouldBe(client);
}
[Fact]
public void use_client_factory_should_use_same_client()
{
var client = new Elastic.Clients.Elasticsearch.ElasticsearchClient();
var services = new ServiceCollection();
var settings = new ElasticsearchOptions();

services.AddHealthChecks().AddElasticsearch(clientFactory: (sp => client), setup: (setup) => settings = setup);

using var serviceProvider = services.BuildServiceProvider();

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

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

check.ShouldBeOfType<ElasticsearchHealthCheck>();

settings.Client.ShouldNotBeNull();
settings.Client.ShouldBe(client);
}
}

0 comments on commit 7b374ae

Please sign in to comment.