Exemplo n.º 1
0
        public async Task ProcessWhenHasExternalUrlThenRegionsNotMarkedAsHealthy(string externalUrl, bool pathHasExternalUrl)
        {
            // Arrange
            const string expectedRegionEndpoint = "https://expectedHost/regionEndpoint";
            var          listOfPaths            = new List <AppRegistryModel>
            {
                new AppRegistryModel
                {
                    Path        = "Path1",
                    ExternalURL = new Uri(externalUrl, UriKind.RelativeOrAbsolute),
                    Regions     = new List <RegionModel>()
                    {
                        new RegionModel()
                        {
                            RegionEndpoint      = expectedRegionEndpoint,
                            HealthCheckRequired = true,
                            IsHealthy           = false,
                            PageRegion          = PageRegion.Body,
                        },
                    },
                    AjaxRequests = new List <AjaxRequestModel>()
                    {
                        new AjaxRequestModel()
                        {
                            AjaxEndpoint        = expectedRegionEndpoint,
                            HealthCheckRequired = true,
                            IsHealthy           = false,
                            Name = "ajax-1",
                        },
                    },
                },
            };

            A.CallTo(() => appRegistryService.GetPathsAndRegions()).Returns(listOfPaths);
            A.CallTo(() => healthCheckerService.IsHealthy(A <Uri> .Ignored, A <bool> .Ignored, A <string> .Ignored)).Returns(true);

            // Act
            await healthMonitoringProcessor.Process().ConfigureAwait(false);

            // Assert
            if (pathHasExternalUrl)
            {
                A.CallTo(() => appRegistryService.MarkRegionAsHealthy(A <string> .Ignored, A <PageRegion> .Ignored)).MustNotHaveHappened();
                A.CallTo(() => healthCheckerService.IsHealthy(A <Uri> .Ignored, A <bool> .Ignored, A <string> .Ignored)).MustNotHaveHappened();
            }
            else
            {
                var expectedRegionUri = new Uri(expectedRegionEndpoint);
                var firstItem         = listOfPaths.First();
                var pageRegion        = firstItem.Regions.First();
                var ajaxRequest       = firstItem.AjaxRequests.First();
                A.CallTo(() => healthCheckerService.IsHealthy(expectedRegionUri, A <bool> .Ignored, A <string> .Ignored)).MustHaveHappenedTwiceExactly();
                A.CallTo(() => appRegistryService.MarkRegionAsHealthy(firstItem.Path, pageRegion.PageRegion)).MustHaveHappenedOnceExactly();
                A.CallTo(() => appRegistryService.MarkAjaxRequestAsHealthy(firstItem.Path, ajaxRequest.Name)).MustHaveHappenedOnceExactly();
            }
        }
        private async Task ProcessAjaxRequests(string path, IEnumerable <AjaxRequestModel> ajaxRequests)
        {
            foreach (var ajaxRequest in ajaxRequests)
            {
                var ajaxRequestHealthEndpoint = new Uri(ajaxRequest.AjaxEndpoint.Replace("{0}", $"{nameof(HealthMonitoringProcessor)}.{nameof(ProcessAjaxRequests)}.{Guid.NewGuid().ToString()}", StringComparison.OrdinalIgnoreCase));

                var isHealthy = await healthCheckerService.IsHealthy(ajaxRequestHealthEndpoint, true, MediaTypeNames.Application.Json).ConfigureAwait(false);

                if (isHealthy)
                {
                    logger.LogInformation($"Starting to mark {ajaxRequestHealthEndpoint} as healthy");
                    await appRegistryService.MarkAjaxRequestAsHealthy(path, ajaxRequest.Name).ConfigureAwait(false);

                    logger.LogInformation($"Completed marking {ajaxRequestHealthEndpoint} as healthy");
                }
            }
        }