Exemplo n.º 1
0
        public async Task <CompositeHealthCheckResult> CheckHealthAsync(CheckStatus partiallyHealthyStatus, CancellationToken cancellationToken)
        {
            var logMessage = new StringBuilder();
            var result     = new CompositeHealthCheckResult(partiallyHealthyStatus);

            foreach (var check in _checks)
            {
                try
                {
                    var healthCheckResult = await check.Value.CheckAsync().ConfigureAwait(false);

                    logMessage.AppendLine($"HealthCheck: {check.Key} : {healthCheckResult.CheckStatus}");
                    result.Add(check.Key, healthCheckResult);
                }
                catch (Exception ex)
                {
                    logMessage.AppendLine($"HealthCheck: {check.Key} : Exception {ex.GetType().FullName} thrown");
                    result.Add(check.Key, CheckStatus.Unhealthy, $"Exception during check: {ex.GetType().FullName}");
                }
            }

            if (logMessage.Length == 0)
            {
                logMessage.AppendLine("HealthCheck: No checks have been registered");
            }

            _logger.Log((result.CheckStatus == CheckStatus.Healthy ? LogLevel.Information : LogLevel.Error), 0, logMessage.ToString(), null, MessageFormatter);
            return(result);
        }
        public void DataIsCompositeDictionary()
        {
            var classUnderTest = new CompositeHealthCheckResult();

            classUnderTest.Add("name1", CheckStatus.Healthy, "With data", new Dictionary <string, object> {
                { "Hello", "world" }
            });
            classUnderTest.Add("name2", CheckStatus.Healthy, "With data", new Dictionary <string, object> {
                { "The answer", 42 }
            });

            var returnedData = classUnderTest.Data;

            Assert.Collection(returnedData.OrderBy(x => x.Key),
                              kvp =>
            {
                Assert.Equal("name1", kvp.Key);
                var dict = Assert.IsAssignableFrom <IReadOnlyDictionary <string, object> >(kvp.Value);
                var elem = Assert.Single(dict);
                Assert.Equal("Hello", elem.Key);
                Assert.Equal("world", elem.Value);
            },
                              kvp =>
            {
                Assert.Equal("name2", kvp.Key);
                var dict = Assert.IsAssignableFrom <IReadOnlyDictionary <string, object> >(kvp.Value);
                var elem = Assert.Single(dict);
                Assert.Equal("The answer", elem.Key);
                Assert.Equal(42, elem.Value);
            }
                              );
        }
        public void NamesMustBeUnique()
        {
            var classUnderTest = new CompositeHealthCheckResult();

            classUnderTest.Add("name", HealthCheckResult.Healthy("healthy"));

            Assert.Throws <ArgumentException>(() => classUnderTest.Add("name", HealthCheckResult.Healthy("healthy")));
        }
        public void MultipleDifferentValuesWithoutHealthy_CheckStatusIsUnhealthy()
        {
            var classUnderTest = new CompositeHealthCheckResult(partiallyHealthyStatus: CheckStatus.Healthy);

            classUnderTest.Add("name1", CheckStatus.Warning, "Warning");
            classUnderTest.Add("name2", CheckStatus.Unhealthy, "Unhealthy");

            Assert.Equal(CheckStatus.Unhealthy, classUnderTest.CheckStatus);
        }
        public void MultipleValuesOfSameStatus_CheckStatusIsStatus_DescriptionIsComposite(CheckStatus status)
        {
            var classUnderTest = new CompositeHealthCheckResult();

            classUnderTest.Add("name1", status, "Description 1");
            classUnderTest.Add("name2", status, "Description 2");

            Assert.Equal(status, classUnderTest.CheckStatus);
            Assert.Contains($"name1: Description 1", classUnderTest.Description);
            Assert.Contains($"name2: Description 2", classUnderTest.Description);
        }
        public void SingleValue_CheckStatusIsSetStatus_DescriptionIsDescription(CheckStatus status)
        {
            var classUnderTest = new CompositeHealthCheckResult();
            var description    = $"Description for {status}";

            classUnderTest.Add("name", status, description);

            Assert.Equal(status, classUnderTest.CheckStatus);
            Assert.Equal($"name: {description}", classUnderTest.Description);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Uses the provided service provider and executes the checks in the given group.
        /// </summary>
        public async Task<CompositeHealthCheckResult> RunGroupAsync(IServiceProvider serviceProvider,
            HealthCheckGroup group, CancellationToken cancellationToken = default(CancellationToken)) {
            var result = new CompositeHealthCheckResult(group.PartiallyHealthyStatus);
            var checkTasks = group.Checks.Select(check =>
                new {Check = check, Task = check.RunAsync(serviceProvider, cancellationToken).AsTask()}).ToList();
            await Task.WhenAll(checkTasks.Select(checkTask => checkTask.Task));

            foreach (var checkTask in checkTasks) result.Add(checkTask.Check.Name, checkTask.Task.Result);

            return result;
        }
        public void CollectsStatusesToMakeThemAvailable()
        {
            var classUnderTest = new CompositeHealthCheckResult(partiallyHealthyStatus: CheckStatus.Healthy);
            var warningData    = new Dictionary <string, object> {
                { "Hello", "world" }
            };
            var unhealthyData = new Dictionary <string, object> {
                { "The answer", 42 }
            };

            classUnderTest.Add("0", CheckStatus.Warning, "Warning", warningData);
            classUnderTest.Add("1", CheckStatus.Unhealthy, "Unhealthy", unhealthyData);

            var results = classUnderTest.Results;

            Assert.Collection(results.OrderBy(kvp => kvp.Key),
                              kvp =>
            {
                Assert.Equal("0", kvp.Key);
                Assert.Equal(CheckStatus.Warning, kvp.Value.CheckStatus);
                Assert.Equal("Warning", kvp.Value.Description);
                var innerKvp = Assert.Single(kvp.Value.Data);
                Assert.Equal("Hello", innerKvp.Key);
                Assert.Equal("world", innerKvp.Value);
            },
                              kvp =>
            {
                Assert.Equal("1", kvp.Key);
                Assert.Equal(CheckStatus.Unhealthy, kvp.Value.CheckStatus);
                Assert.Equal("Unhealthy", kvp.Value.Description);
                var innerKvp = Assert.Single(kvp.Value.Data);
                Assert.Equal("The answer", innerKvp.Key);
                Assert.Equal(42, innerKvp.Value);
            }
                              );
        }
        public void GuardClauses()
        {
            var classUnderTest = new CompositeHealthCheckResult();

            // Add(string, CheckStatus, string)
            Assert.Throws <ArgumentNullException>("name", () => classUnderTest.Add(null, CheckStatus.Healthy, "?"));
            Assert.Throws <ArgumentException>("name", () => classUnderTest.Add("", CheckStatus.Healthy, "?"));
            Assert.Throws <ArgumentException>("status", () => classUnderTest.Add("name", CheckStatus.Unknown, "?"));
            Assert.Throws <ArgumentNullException>("description", () => classUnderTest.Add("name", CheckStatus.Healthy, null));
            Assert.Throws <ArgumentException>("description", () => classUnderTest.Add("name", CheckStatus.Unhealthy, ""));

            // Add(string, IHealthCheckResult)
            var checkResult = HealthCheckResult.Healthy("Hello");

            Assert.Throws <ArgumentNullException>("name", () => classUnderTest.Add(null, checkResult));
            Assert.Throws <ArgumentException>("name", () => classUnderTest.Add("", checkResult));
            Assert.Throws <ArgumentNullException>("checkResult", () => classUnderTest.Add("name", null));
        }