Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Commit

Permalink
#2 only register types deriving HealthCheck as health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alhardy committed Feb 12, 2018
1 parent 74e2c23 commit 527b0e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public IEnumerable<TypeInfo> HealthCheckTypes
return false;
}

if (!typeInfo.Name.EndsWith(HealthCheckTypeName, StringComparison.OrdinalIgnoreCase) &&
!DerivesFromHealthCheck(typeInfo, candidateAssemblies))
if (!DerivesFromHealthCheck(typeInfo, candidateAssemblies))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ public void Should_scan_assembly_and_add_health_checks_and_ignore_obsolete_check
health.Checks.Skip(1).First().Name.Should().Be("Referenced Health Check");
}

[Fact]
public void Should_only_register_class_deriving_health_check()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<IDatabase, Database>();

// Act
var unused = new HealthBuilder()
.HealthChecks.RegisterFromAssembly(services, _fixture.DependencyContext)
.BuildAndAddTo(services);
var provider = services.BuildServiceProvider();
var health = provider.GetRequiredService<IHealth>();

// Assert
health.Checks.FirstOrDefault(h => h.Name == nameof(DontRegisterHealthCheck)).Should().BeNull();
}

[Fact]
public void Should_append_resolved_health_checks_to_those_explicitly_registered()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <copyright file="DontRegisterHealthCheck.cs" company="Allan Hardy">
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using System;
using System.Threading;
using System.Threading.Tasks;
using App.Metrics.Health;

namespace Health.Extensions.DependencyInjection.Facts.TestHelpers
{
public class DontRegisterHealthCheck
{
public ValueTask<HealthCheckResult> CheckAsync(CancellationToken cancellationToken = default)
{
if (DateTime.UtcNow.Second <= 20)
{
return new ValueTask<HealthCheckResult>(HealthCheckResult.Degraded());
}

if (DateTime.UtcNow.Second >= 40)
{
return new ValueTask<HealthCheckResult>(HealthCheckResult.Unhealthy());
}

return new ValueTask<HealthCheckResult>(HealthCheckResult.Healthy());
}
}
}

0 comments on commit 527b0e4

Please sign in to comment.