private async Task <(MockClient, HealthCheckResult)> RunHealthCheckAsync(HttpHealthCheckParameters parameters, HttpResponseMessage response)
        {
            Accessor <ServiceContext> accessor    = new Accessor <ServiceContext>();
            Mock <IHttpClientFactory> factoryMock = new Mock <IHttpClientFactory>();
            MockClient clientMock = new MockClient(response);

            factoryMock.Setup(f => f.CreateClient(HttpEndpointHealthCheck.HttpClientLogicalName))
            .Returns(clientMock);

            HttpEndpointHealthCheck healthCheck = new HttpEndpointHealthCheck(
                parameters,
                factoryMock.Object,
                accessor,
                new NullLogger <HttpEndpointHealthCheck>(),
                new SimpleScopeProvider());

            HealthCheckContext checkContext = HealthCheckContextHelper.CreateCheckContext();

            HealthCheckResult uninitializedResult = await healthCheck.CheckHealthAsync(checkContext);

            Assert.AreEqual(HealthStatus.Degraded, uninitializedResult.Status,
                            FormattableString.Invariant($"Should return {HealthStatus.Degraded} if accessor not set"));

            Mock <ICodePackageActivationContext> mock = new Mock <ICodePackageActivationContext>();

            mock.Setup(c => c.GetEndpoint(parameters.EndpointName)).Returns(new System.Fabric.Description.EndpointResourceDescription());
            ServiceContext context = MockStatelessServiceContextFactory.Create(mock.Object, "", new Uri("http://localhost"), Guid.Empty, 0);

            (accessor as IAccessorSetter <ServiceContext>).SetValue(context);

            HealthCheckResult result = await healthCheck.CheckHealthAsync(checkContext);

            return(clientMock, result);
        }
Beispiel #2
0
        public async Task AbstractHealthCheck_MarksActivityWithHealthCheckFlag()
        {
            Activity?activity = null;

            HealthCheckResult actualResult = await new TestHealthCheck((c, t) =>
            {
                activity = Activity.Current;
                return(HealthCheckResult.Healthy());
            }).CheckHealthAsync(HealthCheckContextHelper.CreateCheckContext());

            Assert.AreEqual(actualResult.Status, HealthStatus.Healthy);
            Assert.IsNotNull(activity);
            Assert.IsTrue(activity !.IsHealthCheck(), "Activity should be marked as HealthCheck activity");
            activity !.AssertResult(TimedScopeResult.Success);
        }
        public async Task AbstractHealthCheck_PropagatesParametersAndResult()
        {
            HealthCheckResult  expectedResult  = HealthCheckResult.Healthy("test");
            HealthCheckContext expectedContext = HealthCheckContextHelper.CreateCheckContext();
            CancellationToken  expectedToken   = new CancellationTokenSource().Token;

            HealthCheckContext?actualContext = default;
            CancellationToken  actualToken   = default;

            HealthCheckResult actualResult = await new TestHealthCheck((context, token) =>
            {
                actualContext = context;
                actualToken   = token;
                return(expectedResult);
            }).CheckHealthAsync(expectedContext, expectedToken);

            Assert.AreEqual(expectedResult, actualResult);
            Assert.AreEqual(expectedContext, actualContext);
            Assert.AreEqual(expectedToken, actualToken);
        }
        public async Task AbstractHealthCheck_WhenExceptionThrown_ReturnsUnhealtyState()
        {
            using TestActivityListener listener = new TestActivityListener();
            Activity? activity  = null;
            Exception exception = new ArrayTypeMismatchException();

            HealthCheckResult actualResult = await new TestHealthCheck((c, t) =>
            {
                activity = Activity.Current;
                throw exception;
            }
                                                                       ).CheckHealthAsync(HealthCheckContextHelper.CreateCheckContext());

            Assert.AreEqual(actualResult.Exception, exception);
            Assert.AreEqual(actualResult.Status, HealthStatus.Unhealthy);
            CollectionAssert.AreEquivalent(actualResult.Data.ToArray(), TestHealthCheck.TestParameters);
            NullableAssert.IsNotNull(activity);
            Assert.IsTrue(activity.IsHealthCheck(), "Activity should be marked as HealthCheck activity");
            activity.AssertResult(ActivityResult.SystemError);
        }
Beispiel #5
0
        public async Task AbstractHealthCheck_WhenExceptionThrown_ReturnsUnhealtyState()
        {
            Activity? activity  = null;
            Exception exception = new ArrayTypeMismatchException();

            HealthCheckResult actualResult = await new TestHealthCheck((c, t) =>
            {
                activity = Activity.Current;
                throw exception;
            }
                                                                       ).CheckHealthAsync(HealthCheckContextHelper.CreateCheckContext());

            Assert.AreEqual(actualResult.Exception, exception);
            Assert.AreEqual(actualResult.Status, HealthStatus.Unhealthy);
            Assert.IsNotNull(activity);
            Assert.IsTrue(activity !.IsHealthCheck(), "Activity should be marked as HealthCheck activity");
            activity !.AssertResult(TimedScopeResult.SystemError);
        }