public PackageLagCatalogLeafProcessorFacts(ITestOutputHelper output)
        {
            _output = output;
            _searchServiceClient = new Mock <ISearchServiceClient>();;
            _telemetryService    = new Mock <IPackageLagTelemetryService>();
            _configurationMock   = new Mock <IOptionsSnapshot <PackageLagMonitorConfiguration> >();
            _configuration       = new PackageLagMonitorConfiguration
            {
                ServiceIndexUrl    = "http://localhost:801/search/diag",
                RegionInformations = new List <RegionInformation>
                {
                    new RegionInformation
                    {
                        Region        = _region,
                        ResourceGroup = _resourceGroup,
                        ServiceName   = _serviceName,
                    },
                }
            };

            _logger = new LoggerFactory()
                      .AddXunit(_output)
                      .CreateLogger <PackageLagCatalogLeafProcessor>();

            _instances = new List <Instance>
            {
                new Instance(
                    _slot,
                    0,
                    "http://localhost:801/search/diag",
                    "http://localhost:801/query",
                    _region,
                    ServiceType.AzureSearch),
                new Instance(
                    _slot,
                    1,
                    "http://localhost:802/search/diag",
                    "http://localhost:802/query",
                    _region,
                    ServiceType.AzureSearch)
            };

            _configurationMock
            .Setup(x => x.Value)
            .Returns(() => _configuration);
            _searchServiceClient
            .Setup(x => x.GetSearchEndpoints(It.IsAny <RegionInformation>()))
            .Returns(() => _instances);

            var regionInformations = _configuration.RegionInformations;
            var instances          = new List <Instance>();

            foreach (var regionInformation in regionInformations)
            {
                instances.AddRange(_searchServiceClient.Object.GetSearchEndpoints(regionInformation));
            }

            _target = new PackageLagCatalogLeafProcessor(instances, _searchServiceClient.Object, _telemetryService.Object, _logger);
            _target.WaitBetweenPolls = TimeSpan.FromSeconds(5);
        }
        public async Task NoInstancesDoesNotLog()
        {
            var currentTime = DateTimeOffset.UtcNow;
            PackageDetailsCatalogLeaf listPackageLeaf = new PackageDetailsCatalogLeaf
            {
                PackageId      = "Test",
                PackageVersion = "1.0.0",
                Created        = currentTime,
                LastEdited     = currentTime,
                Listed         = true
            };

            var emptyInstances = new List <Instance>();
            var newTarget      = new PackageLagCatalogLeafProcessor(emptyInstances, _searchServiceClient.Object, _telemetryService.Object, _logger);

            _searchServiceClient.SetupSequence(ssc => ssc.GetResultForPackageIdVersion(It.IsAny <Instance>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Unexpectd call to get search result"));

            _searchServiceClient.Setup(ssc => ssc.GetIndexLastReloadTimeAsync(It.IsAny <Instance>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Unexpected call to get reload time"));

            _telemetryService
            .Setup(ts => ts.TrackPackageCreationLag(It.IsAny <DateTimeOffset>(), It.IsAny <Instance>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TimeSpan>()))
            .Throws(new Exception("Unexpected Logging"));

            _telemetryService
            .Setup(ts => ts.TrackV3Lag(It.IsAny <DateTimeOffset>(), It.IsAny <Instance>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <TimeSpan>()))
            .Throws(new Exception("Unexpected Logging"));

            try
            {
                var lag = await _target.ProcessPackageLagDetailsAsync(listPackageLeaf, listPackageLeaf.Created, listPackageLeaf.LastEdited, expectListed : true, isDelete : false);

                Assert.Null(lag);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public PackageLagCatalogLeafProcessorFacts(ITestOutputHelper output)
        {
            _output = output;
            _searchServiceClient       = new Mock <ISearchServiceClient>();
            _azureManagementAPIWrapper = new Mock <IAzureManagementAPIWrapper>();
            _telemetryService          = new Mock <IPackageLagTelemetryService>();
            _configurationMock         = new Mock <IOptionsSnapshot <PackageLagMonitorConfiguration> >();
            _httpClientMock            = new Mock <IHttpClientWrapper>();
            _configuration             = new PackageLagMonitorConfiguration
            {
                InstancePortMinimum = 801,
                ServiceIndexUrl     = "http://localhost:801/search/diag",
                Subscription        = _subscription,
                RegionInformations  = new List <RegionInformation>
                {
                    new RegionInformation
                    {
                        Region        = _region,
                        ResourceGroup = _resourceGroup,
                        ServiceName   = _serviceName,
                    },
                }
            };

            _logger = new LoggerFactory()
                      .AddXunit(_output)
                      .CreateLogger <PackageLagCatalogLeafProcessor>();

            _token     = CancellationToken.None;
            _instances = new List <Instance>
            {
                new Instance(
                    _slot,
                    0,
                    "http://localhost:801/search/diag",
                    "http://localhost:801/query",
                    _region),
                new Instance(
                    _slot,
                    1,
                    "http://localhost:802/search/diag",
                    "http://localhost:802/query",
                    _region)
            };
            _feedTimestamp = new DateTimeOffset(2018, 1, 1, 8, 0, 0, TimeSpan.Zero);

            _configurationMock
            .Setup(x => x.Value)
            .Returns(() => _configuration);
            _searchServiceClient
            .Setup(x => x.GetSearchEndpointsAsync(It.IsAny <RegionInformation>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _instances);

            var regionInformations = _configuration.RegionInformations;
            var instances          = new List <Instance>();

            foreach (var regionInformation in regionInformations)
            {
                instances.AddRange(_searchServiceClient.Object.GetSearchEndpointsAsync(regionInformation, _token).Result);
            }

            _target = new PackageLagCatalogLeafProcessor(instances, _searchServiceClient.Object, _telemetryService.Object, _logger);
            _target.WaitBetweenPolls = TimeSpan.FromSeconds(5);
        }