コード例 #1
0
        public void Log4NetConfigurationIsCorrect()
        {
            IHealthCheckResult result = GetHealthCheckResult(Log4NetConfigurationKeys.Correct);

            Assert.IsTrue(result.Passed);
            StringAssert.Contains(result.Message, "The log4net configuration is correct.");
        }
コード例 #2
0
        public void DateParameterMissing()
        {
            IHealthCheckResult result = GetHealthCheckResult(Log4NetConfigurationKeys.DateParameterMissing);

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The definition of the @Date parameter is missing.");
        }
コード例 #3
0
        public void ExceptionThrownInHealthCheckImplementationIsReportedAsFailedHealthCheckResult()
        {
            IHealthCheck         firstHealthCheck     = m_Repository.StrictMock <IHealthCheck>();
            HealthCheckException healthCheckException = m_Repository.StrictMock <HealthCheckException>();

            using (m_Repository.Record())
            {
                Expect.Call(firstHealthCheck.Execute()).Throw(healthCheckException);
                Expect.Call(healthCheckException.Message).Return("Sample Exception");
                m_Repository.ReplayAll();

                HealthCheckResultCollection resultCollection =
                    HealthCheckRunner.RunHealthChecks(new HealthChecklistCollection(new[] { firstHealthCheck }));

                Assert.AreEqual(1, resultCollection.Count);

                IHealthCheckResult result = resultCollection[0];

                Assert.IsFalse(result.Passed);
                StringAssert.StartsWith(result.Message,
                                        "An exception was thrown during the execution of the Health Check");
                StringAssert.Contains(result.Message, "Sample Exception");

                m_Repository.VerifyAll();
            }
        }
コード例 #4
0
        public async ValueTask <IHealthCheckResult> CheckAsync(CancellationToken cancellationToken)
        {
            while (_cacheExpiration <= UtcNow)
            {
                // Can't use a standard lock here because of async, so we'll use this flag to determine when we should write a value,
                // and the waiters who aren't allowed to write will just spin wait for the new value.
                if (Interlocked.Exchange(ref _writerCount, 1) != 0)
                {
                    await Task.Delay(5, cancellationToken).ConfigureAwait(false);

                    continue;
                }

                try
                {
                    _cachedResult = await Check(cancellationToken).ConfigureAwait(false);

                    _cacheExpiration = UtcNow + CacheDuration;
                    break;
                }
                finally
                {
                    _writerCount = 0;
                }
            }

            return(_cachedResult);
        }
コード例 #5
0
        public void NoResultsReturnedFromSproc()
        {
            IHealthCheck       healthCheck = GetHealthCheck(null, false);
            IHealthCheckResult result      = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The IsHealthy_ObjectExistenceCheck sproc produced no result row.");
        }
コード例 #6
0
        public void CategoryNotDefined()
        {
            IHealthCheckResult result = BuildHealthCheckAndSetExpectations(false, BuildAllTrueArray());

            m_MockRepository.VerifyAll();
            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The performance counter category has not been registered.");
        }
コード例 #7
0
        public void ServiceIsNotRunning()
        {
            IHealthCheckResult result = RunHealthCheck(false);

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The Feature Store service is not running.");
            m_MockRepository.VerifyAll();
        }
コード例 #8
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;
        }
コード例 #9
0
        public void CategoryDefinedMissingTwoCounters()
        {
            IHealthCheckResult result = BuildHealthCheckAndSetExpectations(true, BuildAlternatingBoolArray());

            m_MockRepository.VerifyAll();
            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "category has been registered, but counter(s) are missing");
            StringAssert.Contains(result.Message, m_PerformanceCounterDefinitions[1].CounterName);
        }
コード例 #10
0
        public void PerformanceCountersDefinedHealthCheck()
        {
            IHealthCheckResult result = BuildHealthCheckAndSetExpectations(true, BuildAllTrueArray());

            m_MockRepository.VerifyAll();
            Assert.IsTrue(result.Passed);
            StringAssert.Contains(result.Message,
                                  "The performance counter category and all counters have been registered.");
        }
コード例 #11
0
        public void UserIsNotFoundInRole()
        {
            IHealthCheck       healthCheck = BuildHealthCheckAndSetExpectations(false, true);
            IHealthCheckResult result      = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "not found in the necessary role.");

            m_MockRepository.VerifyAll();
        }
コード例 #12
0
        public void NoRowsReturnedFromRoleMemberQuery()
        {
            IHealthCheck       healthCheck = BuildHealthCheckAndSetExpectations(true, false);
            IHealthCheckResult result      = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message,
                                  "The call to check if the user was in the necessary role returned no row.");

            m_MockRepository.VerifyAll();
        }
コード例 #13
0
        public void Add(string name, IHealthCheckResult checkResult)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (checkResult == null)
            {
                throw new ArgumentNullException(nameof(checkResult));
            }

            _results.Add(name, checkResult);
        }
コード例 #14
0
        public void ServiceIsNotInstalled()
        {
            IServiceStateInquisitor serviceStateInquisitor = m_MockRepository.StrictMock <IServiceStateInquisitor>();
            IHealthCheck            healthCheck            = FeatureStoreServiceStateHealthCheck.Create(serviceStateInquisitor);

            using (m_MockRepository.Record())
            {
                Expect.Call(serviceStateInquisitor.ServiceIsRunning()).Throw(new ServiceNotInstalledException());
                m_MockRepository.ReplayAll();
            }

            IHealthCheckResult result = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "The Feature Store service is not installed.");
        }
        public void SqlServerStorageContainerConnectivityHealthCheckPasses()
        {
            DbConnection connection  = m_MockRepository.StrictMock <DbConnection>();
            IHealthCheck healthCheck = SqlServerStorageContainerConnectivityHealthCheck.Create(connection);

            using (m_MockRepository.Record())
            {
                Expect.Call(connection.Open);
                Expect.Call(connection.ConnectionString).Return("SampleConnectionString");
                m_MockRepository.ReplayAll();

                IHealthCheckResult result = healthCheck.Execute();

                Assert.IsTrue(result.Passed);
                StringAssert.Contains(result.Message, "Connection to SqlServer database succeeded");
                StringAssert.Contains(result.Message, "SampleConnectionString");

                m_MockRepository.VerifyAll();
            }
        }
コード例 #16
0
        public void HealthCheckRunnerExecutesListOfHealthChecksAndAllSucceed()
        {
            IHealthCheck firstHealthCheck  = m_Repository.StrictMock <IHealthCheck>();
            IHealthCheck secondHealthCheck = m_Repository.StrictMock <IHealthCheck>();
            IHealthCheck thirdHealthCheck  = m_Repository.StrictMock <IHealthCheck>();

            IHealthCheckResult firstHealthCheckResult  = m_Repository.StrictMock <IHealthCheckResult>();
            IHealthCheckResult secondHealthCheckResult = m_Repository.StrictMock <IHealthCheckResult>();
            IHealthCheckResult thirdHealthCheckResult  = m_Repository.StrictMock <IHealthCheckResult>();

            using (m_Repository.Record())
            {
                Expect.Call(firstHealthCheck.Execute()).Return(firstHealthCheckResult);
                Expect.Call(secondHealthCheck.Execute()).Return(secondHealthCheckResult);
                Expect.Call(thirdHealthCheck.Execute()).Return(thirdHealthCheckResult);

                Expect.Call(firstHealthCheckResult.Passed).Return(true);
                Expect.Call(firstHealthCheckResult.Message).Return("The Health Check was successful.");

                Expect.Call(secondHealthCheckResult.Passed).Return(true);
                Expect.Call(secondHealthCheckResult.Message).Return("The Health Check was successful.");

                Expect.Call(thirdHealthCheckResult.Passed).Return(true);
                Expect.Call(thirdHealthCheckResult.Message).Return("The Health Check was successful.");

                m_Repository.ReplayAll();

                HealthCheckResultCollection resultCollection =
                    HealthCheckRunner.RunHealthChecks(
                        new HealthChecklistCollection(new[] { firstHealthCheck, secondHealthCheck, thirdHealthCheck }));

                Assert.AreEqual(3, resultCollection.Count);
                foreach (IHealthCheckResult healthCheckResult in resultCollection)
                {
                    Assert.IsTrue(healthCheckResult.Passed);
                    StringAssert.Contains(healthCheckResult.Message, "success");
                }

                m_Repository.VerifyAll();
            }
        }
コード例 #17
0
        public async ValueTask <IHealthCheckResult> CheckAsync(
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IHealthCheckResult result = null;

            if (_store == null)
            {
                result = HealthCheckResult.Unhealthy("IHealthCheckStore is null, DI did not work");
            }
            var health = await _store.IsHealthyAsync();

            if (health.Health == false)
            {
                var unhealthyKeys = string.Join(".", health.UnHealthyKeys);
                result = HealthCheckResult.Unhealthy($"the following aggregate keys are unhealthy:[{unhealthyKeys}]");
            }
            else
            {
                result = HealthCheckResult.Healthy("Everything in Aggregate is healthy");
            }
            return(result);
        }
コード例 #18
0
        public void OneObjectDoesNotExist()
        {
            var results = new[]
            {
                new Tuple <bool, string>(true, "Role1Exists"),
                new Tuple <bool, string>(true, "Role2Exists"),
                new Tuple <bool, string>(false, "Table1Exists"),
                new Tuple <bool, string>(true, "Table2Exists"),
                new Tuple <bool, string>(true, "Table3Exists"),
                new Tuple <bool, string>(true, "Sproc1Exists"),
                new Tuple <bool, string>(true, "Sproc2Exists")
            };

            IHealthCheck healthCheck = GetHealthCheck(results, true);

            IHealthCheckResult result = healthCheck.Execute();

            Assert.IsFalse(result.Passed);

            StringAssert.Contains(result.Message, "Failed to find 'Table1Exists' object in database.");

            m_MockRepository.VerifyAll();
        }
コード例 #19
0
        public void AllObjectsExist()
        {
            var results = new[]
            {
                new Tuple <bool, string>(true, "Role1Exists"),
                new Tuple <bool, string>(true, "Role2Exists"),
                new Tuple <bool, string>(true, "Table1Exists"),
                new Tuple <bool, string>(true, "Table2Exists"),
                new Tuple <bool, string>(true, "Table3Exists"),
                new Tuple <bool, string>(true, "Sproc1Exists"),
                new Tuple <bool, string>(true, "Sproc2Exists")
            };

            IHealthCheck healthCheck = GetHealthCheck(results, true);

            IHealthCheckResult result = healthCheck.Execute();

            Assert.IsTrue(result.Passed);

            Console.WriteLine(result.Message);

            m_MockRepository.VerifyAll();
        }
        private void AssertExceptionHandled <T>(string exceptionMessage) where T : Exception
        {
            DbConnection connection  = m_MockRepository.StrictMock <DbConnection>();
            IHealthCheck healthCheck = SqlServerStorageContainerConnectivityHealthCheck.Create(connection);
            T            exception   = m_MockRepository.StrictMock <T>();

            using (m_MockRepository.Record())
            {
                Expect.Call(connection.Open).Throw(exception);
                Expect.Call(connection.ConnectionString).Return("SampleConnectionString");
                Expect.Call(exception.Message).Return(exceptionMessage);

                m_MockRepository.ReplayAll();
            }

            IHealthCheckResult result = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "An exception occured opening the connection to the database:");
            StringAssert.Contains(result.Message, exceptionMessage);

            m_MockRepository.VerifyAll();
        }
コード例 #21
0
        public void InvalidOperationExceptionIsHandledCorrectly()
        {
            const string exceptionMessage       = "An invalid operation was attempted.";
            DbConnection connection             = m_MockRepository.StrictMock <DbConnection>();
            IHealthCheck healthCheck            = SqlServerStorageContainerObjectExistenceHealthCheck.Create(connection);
            InvalidOperationException exception = m_MockRepository.StrictMock <InvalidOperationException>();

            using (m_MockRepository.Record())
            {
                Expect.Call(connection.CreateCommand()).Throw(exception);
                Expect.Call(exception.Message).Return(exceptionMessage);
                Expect.Call(connection.ConnectionString).Return("SampleConnectionString");

                m_MockRepository.ReplayAll();
            }

            IHealthCheckResult result = healthCheck.Execute();

            Assert.IsFalse(result.Passed);
            StringAssert.Contains(result.Message, "An exception occured opening the connection to the database:");
            StringAssert.Contains(result.Message, exceptionMessage);

            m_MockRepository.VerifyAll();
        }
コード例 #22
0
 public void AddResult(string name, IHealthCheckResult result)
 {
     _results.Add(name, result);
 }
コード例 #23
0
 public void Add(IHealthCheckResult checkResult)
 {
     _results[checkResult.GetType().Name] = checkResult ?? throw new ArgumentNullException(nameof(checkResult));
 }
コード例 #24
0
 public NamedCheckResult(string name, IHealthCheckResult result)
 {
     Name   = name;
     Result = result;
 }
コード例 #25
0
 private static string ToDescriptiveString(IHealthCheckResult result)
 => $"{result.CheckStatus} ({result.Description})";