Пример #1
0
        public async Task PublishAsync_Check_MapStatuses()
        {
            // Arrange
            var healthService = new HealthServiceImpl();
            var publisher     = new GrpcHealthChecksPublisher(healthService);

            // Act
            HealthCheckResponse response;

            var report = new HealthReport(
                new Dictionary <string, HealthReportEntry>
            {
                [nameof(HealthStatus.Healthy)]   = new HealthReportEntry(HealthStatus.Healthy, "Description!", TimeSpan.Zero, exception: null, data: null),
                [nameof(HealthStatus.Degraded)]  = new HealthReportEntry(HealthStatus.Degraded, "Description!", TimeSpan.Zero, exception: null, data: null),
                [nameof(HealthStatus.Unhealthy)] = new HealthReportEntry(HealthStatus.Unhealthy, "Description!", TimeSpan.Zero, exception: null, data: null)
            },
                TimeSpan.Zero);
            await publisher.PublishAsync(report, CancellationToken.None);

            // Assert
            response = await healthService.Check(new HealthCheckRequest { Service = nameof(HealthStatus.Healthy) }, context : null);

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);

            response = await healthService.Check(new HealthCheckRequest { Service = nameof(HealthStatus.Degraded) }, context : null);

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);

            response = await healthService.Check(new HealthCheckRequest { Service = nameof(HealthStatus.Unhealthy) }, context : null);

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NotServing, response.Status);
        }
Пример #2
0
        public async Task PublishAsync_Check_ChangingStatus()
        {
            // Arrange
            var healthService = new HealthServiceImpl();
            var publisher     = new GrpcHealthChecksPublisher(healthService);

            HealthCheckResponse response;

            // Act 1
            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => healthService.Check(new HealthCheckRequest {
                Service = ""
            }, context: null));

            // Assert 1
            Assert.AreEqual(StatusCode.NotFound, ex.StatusCode);

            // Act 2
            var report = CreateSimpleHealthReport(HealthStatus.Healthy);
            await publisher.PublishAsync(report, CancellationToken.None);

            response = await healthService.Check(new HealthCheckRequest { Service = "" }, context : null);

            // Assert 2
            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);

            // Act 3
            report = CreateSimpleHealthReport(HealthStatus.Unhealthy);
            await publisher.PublishAsync(report, CancellationToken.None);

            response = await healthService.Check(new HealthCheckRequest { Service = "" }, context : null);

            // Act 3
            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NotServing, response.Status);
        }
Пример #3
0
        public async Task PublishAsync_CheckWithFilter_ChangingStatusBasedOnFilter()
        {
            // Arrange
            var healthService = new HealthServiceImpl();
            var publisher     = CreatePublisher(
                healthService,
                o =>
            {
                o.Services.MapService("", result => !result.Tags.Contains("exclude"));
            });

            HealthCheckResponse response;

            // Act 1
            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => healthService.Check(new HealthCheckRequest {
                Service = ""
            }, context: null));

            // Assert 1
            Assert.AreEqual(StatusCode.NotFound, ex.StatusCode);

            // Act 2
            var report = CreateSimpleHealthReport(
                new HealthResult("", HealthStatus.Healthy),
                new HealthResult("other", HealthStatus.Healthy, new[] { "exclude" }));
            await publisher.PublishAsync(report, CancellationToken.None);

            response = await healthService.Check(new HealthCheckRequest { Service = "" }, context : null);

            // Assert 2
            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);

            // Act 3
            report = CreateSimpleHealthReport(
                new HealthResult("", HealthStatus.Healthy),
                new HealthResult("other", HealthStatus.Unhealthy, new[] { "exclude" }));
            await publisher.PublishAsync(report, CancellationToken.None);

            response = await healthService.Check(new HealthCheckRequest { Service = "" }, context : null);

            // Act 3
            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);

            // Act 4
            report = CreateSimpleHealthReport(
                new HealthResult("", HealthStatus.Unhealthy),
                new HealthResult("other", HealthStatus.Unhealthy, new[] { "exclude" }));
            await publisher.PublishAsync(report, CancellationToken.None);

            response = await healthService.Check(new HealthCheckRequest { Service = "" }, context : null);

            // Act 4
            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NotServing, response.Status);
        }
Пример #4
0
        public void CanSetServiceHealthStatus()
        {
            HealthServiceImpl healthImpl = new HealthServiceImpl();

            IServiceHealth health = new ServiceHealth(healthImpl);

            var serviceName = "RemoveOverlapsGrpc";

            health.SetStatus(serviceName, true);

            Task <HealthCheckResponse> response = healthImpl.Check(
                new HealthCheckRequest()
            {
                Service = serviceName
            },
                null);

            Assert.IsFalse(health.IsAnyServiceUnhealthy());

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving,
                            response.Result.Status);

            // Set the status through the type
            health.SetStatus(typeof(RemoveOverlapsGrpcImpl), false);

            response = healthImpl.Check(
                new HealthCheckRequest()
            {
                Service = serviceName
            },
                null);

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NotServing,
                            response.Result.Status);

            Assert.IsTrue(health.IsAnyServiceUnhealthy());
        }
Пример #5
0
        public void CanSetEmptyServiceNameHealth()
        {
            HealthServiceImpl healthImpl = new HealthServiceImpl();

            IServiceHealth health = new ServiceHealth(healthImpl);

            health.SetStatus(string.Empty, true);

            Task <HealthCheckResponse> response = healthImpl.Check(
                new HealthCheckRequest(), null);

            Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving,
                            response.Result.Status);

            Assert.IsFalse(health.IsAnyServiceUnhealthy());
        }
Пример #6
0
 private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string host, string service)
 {
     return(impl.Check(new HealthCheckRequest {
         Host = host, Service = service
     }, null).Result.Status);
 }
Пример #7
0
 private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string service)
 {
     return impl.Check(new HealthCheckRequest { Service = service }, null).Result.Status;
 }
Пример #8
0
 private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string host, string service)
 {
     return impl.Check(null, HealthCheckRequest.CreateBuilder().SetHost(host).SetService(service).Build()).Result.Status;
 }
Пример #9
0
 private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string host, string service)
 {
     return(impl.Check(HealthCheckRequest.CreateBuilder().SetHost(host).SetService(service).Build(), null).Result.Status);
 }