Beispiel #1
0
        public async Task GivenHealthCheckReturnsHealthy_WhenCheckingForHealth_ThenOkResultShouldBeReturned()
        {
            _healthCheck.CheckAsync(Arg.Any <CancellationToken>()).Returns(HealthCheckResult.Healthy("Healthy."));

            IActionResult result = await _controller.Check();

            Assert.IsType <OkObjectResult>(result);
        }
Beispiel #2
0
        public void OnGet()
        {
            var timedTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
            var stopwatch        = Stopwatch.StartNew();
            var checkResult      = healthCheck.CheckAsync(timedTokenSource.Token).GetAwaiter().GetResult();

            ExecutionTime     = stopwatch.Elapsed;
            HealthCheckResult = checkResult;
        }
        public async Task <IActionResult> Check()
        {
            HealthCheckResult result = await _healthCheck.CheckAsync(HttpContext.RequestAborted);

            if (result.HealthState == HealthState.Healthy)
            {
                return(Ok(result));
            }

            return(StatusCode((int)HttpStatusCode.ServiceUnavailable, result));
        }
 public static async Task <HealthCheckResult> CheckSafeAsync([NotNull] this IHealthCheck check, CancellationToken cancellationToken, HealthStatus statusOnException = HealthStatus.Failing)
 {
     try
     {
         return(await check.CheckAsync(cancellationToken).ConfigureAwait(false));
     }
     catch (Exception error)
     {
         return(new HealthCheckResult(statusOnException, $"Exception of type '{error.GetType().Name} has occurred: '{error.Message}'."));
     }
 }
        public async Task can_connect_to_sql_database_returns_health_status()
        {
            //arrange
            var fakeTenancy = Fake.UniversalHousing.GenerateFakeTenancy();

            TestDataHelper.InsertTenancy(fakeTenancy, _db);
            //act
            var result = await _classUnderTest.CheckAsync(CancellationToken.None).ConfigureAwait(false);

            //assert
            result.Should().NotBeNull();
            result.CheckStatus.Should().Be(CheckStatus.Healthy);
        }
        public async Task cannot_connect_to_sql_database_and_return_record_returns_unhealthy_status()
        {
            //arrange
            var connection    = DotNetEnv.Env.GetString("test");
            var loggerFactory = new LoggerFactory();

            _sqlConnectionFactory = new SqlConnectionFactory(connection, loggerFactory.CreateLogger <SqlConnectionFactory>());
            var logger = loggerFactory.CreateLogger <SqlConnectionHealthCheck>();

            _classUnderTest = new SqlConnectionHealthCheck(_sqlConnectionFactory, logger);
            //act
            var result = await _classUnderTest.CheckAsync(CancellationToken.None).ConfigureAwait(false);

            //assert
            result.Should().NotBeNull();
            result.CheckStatus.Should().Be(CheckStatus.Unhealthy);
        }
Beispiel #7
0
        public async Task <HealthCheckResultItem> RunCheckAsync(IHealthCheck check, CancellationToken cancellationToken = default)
        {
            if (check == null)
            {
                throw new ArgumentNullException(nameof(check));
            }

            await _checkSlim.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                var result = await check.CheckAsync(cancellationToken).ConfigureAwait(false);

                return(new HealthCheckResultItem(check.Name, result));
            }
            finally
            {
                _checkSlim.Release();
            }
        }
Beispiel #8
0
 private static async Task <IReadOnlyList <HealthCheckResult> > TryCheckAsync(IHealthCheck check)
 {
     try { return(await check.CheckAsync().ConfigureAwait(false)); }
     catch (Exception ex) { return(GetExceptionHealthCheckResult(check, ex)); }
 }
Beispiel #9
0
        public static ValueTask <IHealthCheckResult> CheckAsync(this IHealthCheck healthCheck)
        {
            Guard.ArgumentNotNull(nameof(healthCheck), healthCheck);

            return(healthCheck.CheckAsync(CancellationToken.None));
        }