private async Task <AuxiliaryFileResult <T> > LoadAsync <T>(
            AuxiliaryFileResult <T> previousResult,
            Func <IAccessCondition, StringCache, Task <AuxiliaryFileResult <T> > > getResult) where T : class
        {
            await Task.Yield();

            IAccessCondition accessCondition;

            if (previousResult == null)
            {
                accessCondition = AccessConditionWrapper.GenerateEmptyCondition();
            }
            else
            {
                accessCondition = AccessConditionWrapper.GenerateIfNoneMatchCondition(previousResult.Metadata.ETag);
            }

            var newResult = await getResult(accessCondition, _stringCache);

            if (newResult.Modified)
            {
                return(newResult);
            }
            else
            {
                return(previousResult);
            }
        }
Example #2
0
            public BaseFacts(ITestOutputHelper output)
            {
                _downloadDataClient           = new Mock <IDownloadDataClient>();
                _verifiedPackagesDataClient   = new Mock <IVerifiedPackagesDataClient>();
                _popularityTransferDataClient = new Mock <IPopularityTransferDataClient>();
                _telemetryService             = new Mock <IAzureSearchTelemetryService>();
                _logger = output.GetLogger <AuxiliaryDataCache>();

                _token               = CancellationToken.None;
                _downloadData        = Data.GetAuxiliaryFileResult(new DownloadData(), "downloads-etag");
                _verifiedPackages    = Data.GetAuxiliaryFileResult(new HashSet <string>(StringComparer.OrdinalIgnoreCase), "verified-packages-etag");
                _popularityTransfers = Data.GetAuxiliaryFileResult(new PopularityTransferData(), "popularity-transfer-etag");

                _downloadDataClient
                .Setup(x => x.ReadLatestIndexedAsync(It.IsAny <IAccessCondition>(), It.IsAny <StringCache>()))
                .ReturnsAsync(() => _downloadData);
                _verifiedPackagesDataClient
                .Setup(x => x.ReadLatestAsync(It.IsAny <IAccessCondition>(), It.IsAny <StringCache>()))
                .ReturnsAsync(() => _verifiedPackages);
                _popularityTransferDataClient
                .Setup(x => x.ReadLatestIndexedAsync(It.IsAny <IAccessCondition>(), It.IsAny <StringCache>()))
                .ReturnsAsync(() => _popularityTransfers);

                _target = new AuxiliaryDataCache(
                    _downloadDataClient.Object,
                    _verifiedPackagesDataClient.Object,
                    _popularityTransferDataClient.Object,
                    _telemetryService.Object,
                    _logger);
            }
Example #3
0
 public AuxiliaryData(
     DateTimeOffset loaded,
     AuxiliaryFileResult <DownloadData> downloads,
     AuxiliaryFileResult <HashSet <string> > verifiedPackages)
 {
     Downloads        = downloads ?? throw new ArgumentNullException(nameof(downloads));
     VerifiedPackages = verifiedPackages ?? throw new ArgumentNullException(nameof(verifiedPackages));
     Metadata         = new AuxiliaryFilesMetadata(
         loaded,
         Downloads.Metadata,
         VerifiedPackages.Metadata);
 }
Example #4
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);
            }