Exemple #1
0
        public void ReportFailure_AllHostFails_ShouldNotBeHealthy()
        {
            CreatePool("host1, host2, host3");

            //two time Failure = unreachable host
            for (int i = 0; i < 6; i++)
            {
                var host = Pool.GetNextHost();
                host.ReportFailure();
            }

            var healthResult = GetHealthResult();

            healthResult.IsHealthy.ShouldBeFalse(healthResult.Message);
            healthResult.Message.ShouldContain("All of the 3 hosts are unreachable");

            var healthData = GetHealthData();
            var healthDataUnreachableHosts = healthData["UnreachableHosts"];

            healthDataUnreachableHosts.ShouldContain("host1");
            healthDataUnreachableHosts.ShouldContain("host2");
            healthDataUnreachableHosts.ShouldContain("host3");
            healthData["ReachableHosts"].ShouldBe(string.Empty);
            HealthChecks.UnregisterAllHealthChecks();
        }
        public void InfluxReport_CanAddRecords_ForHealthCheck()
        {
            var config      = new InfluxConfig("localhost", "testdb");
            var writer      = new InfluxdbTestWriter(config); config.Writer = writer;
            var report      = new InfluxdbHttpReport(config);
            var context     = new DefaultMetricsContext("TestContext");
            var precision   = config.Precision ?? InfluxConfig.Default.Precision;
            var metricsData = context.DataProvider.CurrentMetricsData;

            HealthChecks.UnregisterAllHealthChecks();
            HealthChecks.RegisterHealthCheck("Health Check 1", () => HealthCheckResult.Healthy($"Healthy check!"));
            HealthChecks.RegisterHealthCheck("Health Check 2", () => HealthCheckResult.Unhealthy($"Unhealthy check!"));
            HealthChecks.RegisterHealthCheck("Health Check 3,tag3=key3", () => HealthCheckResult.Healthy($"Healthy check!"));
            HealthChecks.RegisterHealthCheck("Health Check 4,tag 4=key 4", () => HealthCheckResult.Healthy($"Healthy check!"));
            HealthChecks.RegisterHealthCheck("Name=Health Check 5,tag5=key5", () => HealthCheckResult.Healthy($"Healthy check!"));

            metricsData = context.DataProvider.CurrentMetricsData;
            report.RunReport(metricsData, () => HealthChecks.GetStatus(), CancellationToken.None);
            HealthChecks.UnregisterAllHealthChecks();             // unreg first in case something below throws
            writer.LastBatch.Should().HaveCount(5);

            var expTime = InfluxLineProtocol.FormatTimestamp(metricsData.Timestamp, precision);

            writer.LastBatch[0].ToLineProtocol(precision).Should().Be($@"health_checks,name=health_check_1 ishealthy=True,message=""Healthy check!"" {expTime}");
            writer.LastBatch[1].ToLineProtocol(precision).Should().Be($@"health_checks,name=health_check_2 ishealthy=False,message=""Unhealthy check!"" {expTime}");
            writer.LastBatch[2].ToLineProtocol(precision).Should().Be($@"health_checks,name=health_check_3,tag3=key3 ishealthy=True,message=""Healthy check!"" {expTime}");
            writer.LastBatch[3].ToLineProtocol(precision).Should().Be($@"health_checks,name=health_check_4,tag_4=key\ 4 ishealthy=True,message=""Healthy check!"" {expTime}");
            writer.LastBatch[4].ToLineProtocol(precision).Should().Be($@"health_checks,name=health\ check\ 5,tag5=key5 ishealthy=True,message=""Healthy check!"" {expTime}");
        }
        public void HealthCheck_RegistryDoesNotThrowOnDuplicateRegistration()
        {
            HealthChecks.UnregisterAllHealthChecks();

            HealthChecks.RegisterHealthCheck(new HealthCheck("test", () => { }));

            Action action = () => HealthChecks.RegisterHealthCheck(new HealthCheck("test", () => { }));

            action.ShouldNotThrow <InvalidOperationException>();
        }
        public void HealthCheck_RegistryStatusIsHealthyIfAllChecksAreHealthy()
        {
            HealthChecks.UnregisterAllHealthChecks();

            HealthChecks.RegisterHealthCheck(new HealthCheck("ok", () => { }));
            HealthChecks.RegisterHealthCheck(new HealthCheck("another", () => HealthCheckResult.Healthy()));

            var status = HealthChecks.GetStatus();

            status.IsHealthy.Should().BeTrue();
            status.Results.Length.Should().Be(2);
        }
        public void HealthCheck_RegistryStatusIsFailedIfOneCheckFails()
        {
            HealthChecks.UnregisterAllHealthChecks();

            HealthChecks.RegisterHealthCheck(new HealthCheck("ok", () => { }));
            HealthChecks.RegisterHealthCheck(new HealthCheck("bad", () => HealthCheckResult.Unhealthy()));

            var status = HealthChecks.GetStatus();

            status.IsHealthy.Should().BeFalse();
            status.Results.Length.Should().Be(2);
        }
        public void HealthCheck_RegistryExecutesCheckOnEachGetStatus()
        {
            HealthChecks.UnregisterAllHealthChecks();
            int count = 0;

            HealthChecks.RegisterHealthCheck(new HealthCheck("test", () => { count++; }));

            count.Should().Be(0);

            HealthChecks.GetStatus();

            count.Should().Be(1);

            HealthChecks.GetStatus();

            count.Should().Be(2);
        }
Exemple #7
0
 public void Dispose()
 {
     _componentMonitors.Clear();
     HealthChecks.UnregisterAllHealthChecks();
 }