public Facts(ITestOutputHelper output)
            {
                TelemetryService = new Mock <IAzureSearchTelemetryService>();
                Options          = new Mock <IOptionsSnapshot <Auxiliary2AzureSearchConfiguration> >();
                Logger           = output.GetLogger <DownloadSetComparer>();

                Config = new Auxiliary2AzureSearchConfiguration();
                Options.Setup(x => x.Value).Returns(() => Config);

                Target = new DownloadSetComparer(
                    TelemetryService.Object,
                    Options.Object,
                    Logger);
            }
            public async Task AlwaysAppliesDownloadOverrides()
            {
                DownloadSetComparer
                .Setup(c => c.Compare(It.IsAny <DownloadData>(), It.IsAny <DownloadData>()))
                .Returns <DownloadData, DownloadData>((oldData, newData) =>
                {
                    var config    = new Auxiliary2AzureSearchConfiguration();
                    var telemetry = Mock.Of <IAzureSearchTelemetryService>();
                    var logger    = Mock.Of <ILogger <DownloadSetComparer> >();
                    var options   = new Mock <IOptionsSnapshot <Auxiliary2AzureSearchConfiguration> >();

                    options.Setup(o => o.Value).Returns(config);

                    return(new DownloadSetComparer(telemetry, options.Object, logger)
                           .Compare(oldData, newData));
                });

                // Download override should be applied even if the package's downloads haven't changed.
                OldDownloadData.SetDownloadCount("A", "1.0.0", 1);
                NewDownloadData.SetDownloadCount("A", "1.0.0", 1);
                DownloadOverrides["A"] = 2;

                await Target.ExecuteAsync();

                // Documents should have new data with overriden downloads.
                SearchDocumentBuilder
                .Verify(
                    b => b.UpdateDownloadCount("A", SearchFilters.IncludePrereleaseAndSemVer2, 2),
                    Times.Once);

                // Downloads auxiliary file should have new data without overriden downloads.
                DownloadDataClient.Verify(
                    c => c.ReplaceLatestIndexedAsync(
                        It.Is <DownloadData>(d =>
                                             d["A"].Total == 1 &&
                                             d["A"]["1.0.0"] == 1),
                        It.IsAny <IAccessCondition>()),
                    Times.Once);
            }
Esempio n. 3
0
            public Facts(ITestOutputHelper output)
            {
                AuxiliaryFileClient          = new Mock <IAuxiliaryFileClient>();
                DatabaseFetcher              = new Mock <IDatabaseAuxiliaryDataFetcher>();
                DownloadDataClient           = new Mock <IDownloadDataClient>();
                DownloadSetComparer          = new Mock <IDownloadSetComparer>();
                DownloadTransferrer          = new Mock <IDownloadTransferrer>();
                PopularityTransferDataClient = new Mock <IPopularityTransferDataClient>();
                SearchDocumentBuilder        = new Mock <ISearchDocumentBuilder>();
                IndexActionBuilder           = new Mock <ISearchIndexActionBuilder>();
                BatchPusher      = new Mock <IBatchPusher>();
                SystemTime       = new Mock <ISystemTime>();
                FeatureFlags     = new Mock <IFeatureFlagService>();
                Options          = new Mock <IOptionsSnapshot <Auxiliary2AzureSearchConfiguration> >();
                TelemetryService = new Mock <IAzureSearchTelemetryService>();
                Logger           = output.GetLogger <Auxiliary2AzureSearchCommand>();

                Config = new Auxiliary2AzureSearchConfiguration
                {
                    AzureSearchBatchSize            = 10,
                    MaxConcurrentBatches            = 1,
                    MaxConcurrentVersionListWriters = 1,
                    EnablePopularityTransfers       = true,
                    MinPushPeriod = TimeSpan.FromSeconds(5),
                };
                Options.Setup(x => x.Value).Returns(() => Config);

                OldDownloadData   = new DownloadData();
                OldDownloadResult = Data.GetAuxiliaryFileResult(OldDownloadData, "download-data-etag");
                DownloadDataClient
                .Setup(x => x.ReadLatestIndexedAsync(It.IsAny <IAccessCondition>(), It.IsAny <StringCache>()))
                .ReturnsAsync(() => OldDownloadResult);
                NewDownloadData = new DownloadData();
                AuxiliaryFileClient.Setup(x => x.LoadDownloadDataAsync()).ReturnsAsync(() => NewDownloadData);

                Changes = new SortedDictionary <string, long>();
                DownloadSetComparer
                .Setup(x => x.Compare(It.IsAny <DownloadData>(), It.IsAny <DownloadData>()))
                .Returns(() => Changes);

                OldTransfers      = new PopularityTransferData();
                OldTransferResult = new AuxiliaryFileResult <PopularityTransferData>(
                    modified: true,
                    data: OldTransfers,
                    metadata: new AuxiliaryFileMetadata(
                        DateTimeOffset.UtcNow,
                        TimeSpan.Zero,
                        fileSize: 0,
                        etag: "etag"));
                PopularityTransferDataClient
                .Setup(x => x.ReadLatestIndexedAsync(It.IsAny <IAccessCondition>(), It.IsAny <StringCache>()))
                .ReturnsAsync(OldTransferResult);

                NewTransfers = new PopularityTransferData();
                DatabaseFetcher
                .Setup(x => x.GetPopularityTransfersAsync())
                .ReturnsAsync(NewTransfers);

                TransferChanges = new SortedDictionary <string, long>(StringComparer.OrdinalIgnoreCase);
                DownloadTransferrer
                .Setup(x => x.UpdateDownloadTransfers(
                           It.IsAny <DownloadData>(),
                           It.IsAny <SortedDictionary <string, long> >(),
                           It.IsAny <PopularityTransferData>(),
                           It.IsAny <PopularityTransferData>()))
                .Returns(TransferChanges);

                IndexActions = new IndexActions(
                    new List <IndexAction <KeyedDocument> > {
                    IndexAction.Merge(new KeyedDocument())
                },
                    new List <IndexAction <KeyedDocument> >(),
                    new ResultAndAccessCondition <VersionListData>(
                        new VersionListData(new Dictionary <string, VersionPropertiesData>()),
                        Mock.Of <IAccessCondition>()));
                ProcessedIds = new ConcurrentBag <string>();
                IndexActionBuilder
                .Setup(x => x.UpdateAsync(It.IsAny <string>(), It.IsAny <Func <SearchFilters, KeyedDocument> >()))
                .ReturnsAsync(() => IndexActions)
                .Callback <string, Func <SearchFilters, KeyedDocument> >((id, b) =>
                {
                    ProcessedIds.Add(id);
                    b(SearchFilters.IncludePrereleaseAndSemVer2);
                });

                // When pushing, delay for a little bit of time so the stopwatch has some measurable duration.
                PushedIds       = new ConcurrentBag <string>();
                CurrentBatch    = new ConcurrentBag <IndexActions>();
                FinishedBatches = new ConcurrentBag <List <IndexActions> >();
                BatchPusher
                .Setup(x => x.EnqueueIndexActions(It.IsAny <string>(), It.IsAny <IndexActions>()))
                .Callback <string, IndexActions>((id, indexActions) =>
                {
                    CurrentBatch.Add(indexActions);
                    PushedIds.Add(id);
                });
                BatchPusher
                .Setup(x => x.TryFinishAsync())
                .Returns(async() =>
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(1));
                    return(new BatchPusherResult());
                })
                .Callback(() =>
                {
                    FinishedBatches.Add(CurrentBatch.ToList());
                    CurrentBatch = new ConcurrentBag <IndexActions>();
                });

                FeatureFlags.Setup(x => x.IsPopularityTransferEnabled()).Returns(true);

                Target = new UpdateDownloadsCommand(
                    AuxiliaryFileClient.Object,
                    DatabaseFetcher.Object,
                    DownloadDataClient.Object,
                    DownloadSetComparer.Object,
                    DownloadTransferrer.Object,
                    PopularityTransferDataClient.Object,
                    SearchDocumentBuilder.Object,
                    IndexActionBuilder.Object,
                    () => BatchPusher.Object,
                    SystemTime.Object,
                    FeatureFlags.Object,
                    Options.Object,
                    TelemetryService.Object,
                    Logger);
            }