public void UsesSemVerLevelToIndicateSemVer2()
            {
                _search
                .Setup(x => x.FullFromDb(
                           It.IsAny <string>(),
                           It.IsAny <SearchFilters>(),
                           It.IsAny <string[]>(),
                           It.IsAny <bool>(),
                           It.IsAny <bool>(),
                           It.IsAny <string>(),
                           It.IsAny <Package>(),
                           It.IsAny <string[]>(),
                           It.IsAny <long>(),
                           It.IsAny <bool>()))
                .Returns(() => new SearchDocument.Full());
                var input = new NewPackageRegistration(
                    "NuGet.Versioning",
                    1001,
                    new string[0],
                    new[] { new TestPackage("1.0.0")
                            {
                                SemVerLevelKey = SemVerLevelKey.SemVer2
                            } },
                    false);

                var actions = _target.AddNewPackageRegistration(input);

                Assert.Equal(4, actions.Search.Count);
                Assert.Equal(IndexActionType.Delete, actions.Search[0].ActionType); // SearchFilters.Default
                Assert.Equal(IndexActionType.Delete, actions.Search[1].ActionType); // SearchFilters.IncludePrerelease
                Assert.Equal(IndexActionType.Upload, actions.Search[2].ActionType); // SearchFilters.IncludeSemVer2
                Assert.IsType <SearchDocument.Full>(actions.Search[2].Document);
                Assert.Equal(IndexActionType.Upload, actions.Search[3].ActionType); // SearchFilters.IncludePrereleaseAndSemVer2
                Assert.IsType <SearchDocument.Full>(actions.Search[3].Document);
            }
            public void PassesIsExcludedByDefaultValueCorrectly(bool shouldBeExcluded)
            {
                var input = new NewPackageRegistration(
                    "NuGet.Versioning",
                    1001,
                    new string[0],
                    new[] { new TestPackage("1.0.0")
                            {
                                SemVerLevelKey = SemVerLevelKey.SemVer2
                            } },
                    isExcludedByDefault: shouldBeExcluded);

                var actions = _target.AddNewPackageRegistration(input);

                _search.Verify(
                    x => x.FullFromDb(
                        input.PackageId,
                        SearchFilters.IncludePrereleaseAndSemVer2,
                        It.IsAny <string[]>(),
                        It.IsAny <bool>(),
                        It.IsAny <bool>(),
                        It.IsAny <string>(),
                        input.Packages[0],
                        input.Owners,
                        input.TotalDownloadCount,
                        shouldBeExcluded),
                    Times.Once);
            }
            public void UsesLatestVersionMetadataForSearchIndex()
            {
                _search
                .Setup(x => x.FullFromDb(
                           It.IsAny <string>(),
                           It.IsAny <SearchFilters>(),
                           It.IsAny <string[]>(),
                           It.IsAny <bool>(),
                           It.IsAny <bool>(),
                           It.IsAny <string>(),
                           It.IsAny <Package>(),
                           It.IsAny <string[]>(),
                           It.IsAny <long>(),
                           It.IsAny <bool>()))
                .Returns <string, SearchFilters, string[], bool, bool, string, Package, string[], long, bool>(
                    (i, sf, v, ls, l, fv, p, o, d, ie) => new SearchDocument.Full {
                    OriginalVersion = p.Version
                });
                var package1 = new TestPackage("1.0.0")
                {
                    Description = "This is version 1.0.0."
                };
                var package2 = new TestPackage("2.0.0-alpha")
                {
                    Description = "This is version 2.0.0."
                };
                var input = new NewPackageRegistration(
                    "NuGet.Versioning",
                    1001,
                    new string[0],
                    new[] { package1, package2 },
                    false);

                var actions = _target.AddNewPackageRegistration(input);

                Assert.Equal(4, actions.Search.Count);
                Assert.Equal(IndexActionType.Upload, actions.Search[0].ActionType); // SearchFilters.Default
                Assert.Equal(IndexActionType.Upload, actions.Search[1].ActionType); // SearchFilters.IncludePrerelease
                Assert.Equal(IndexActionType.Upload, actions.Search[2].ActionType); // SearchFilters.IncludeSemVer2
                Assert.Equal(IndexActionType.Upload, actions.Search[3].ActionType); // SearchFilters.IncludePrereleaseAndSemVer2
                var doc0 = Assert.IsType <SearchDocument.Full>(actions.Search[0].Document);

                Assert.Equal(package1.Version, doc0.OriginalVersion);
                var doc1 = Assert.IsType <SearchDocument.Full>(actions.Search[1].Document);

                Assert.Equal(package2.Version, doc1.OriginalVersion);
                var doc2 = Assert.IsType <SearchDocument.Full>(actions.Search[2].Document);

                Assert.Equal(package1.Version, doc2.OriginalVersion);
                var doc3 = Assert.IsType <SearchDocument.Full>(actions.Search[3].Document);

                Assert.Equal(package2.Version, doc3.OriginalVersion);
            }
Ejemplo n.º 4
0
        private async Task ConsumeWorkAsync(
            ConcurrentBag <NewPackageRegistration> allWork,
            CancellationToken produceWorkToken,
            CancellationToken cancellationToken)
        {
            await Task.Yield();

            var batchPusher = _batchPusherFactory();

            NewPackageRegistration work = null;

            try
            {
                while ((allWork.TryTake(out work) || !produceWorkToken.IsCancellationRequested) &&
                       !cancellationToken.IsCancellationRequested)
                {
                    // If there's no work to do, wait a bit before checking again.
                    if (work == null)
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(100));

                        continue;
                    }

                    var indexActions = _indexActionBuilder.AddNewPackageRegistration(work);

                    // There can be an empty set of index actions if there were no packages associated with this
                    // package registration.
                    if (!indexActions.IsEmpty)
                    {
                        batchPusher.EnqueueIndexActions(work.PackageId, indexActions);
                        var fullBatchesResult = await batchPusher.TryPushFullBatchesAsync();

                        fullBatchesResult.EnsureSuccess();
                    }
                }

                var finishResult = await batchPusher.TryFinishAsync();

                finishResult.EnsureSuccess();
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    0,
                    ex,
                    "An exception was thrown while processing package ID {PackageId}.",
                    work?.PackageId ?? "(last batch...)");
                throw;
            }
        }
Ejemplo n.º 5
0
        private IndexAction <KeyedDocument> GetSearchIndexAction(
            NewPackageRegistration packageRegistration,
            IReadOnlyDictionary <NuGetVersion, Package> versionToPackage,
            VersionLists versionLists,
            SearchFilters searchFilters,
            SearchIndexChangeType changeType)
        {
            if (changeType == SearchIndexChangeType.Delete)
            {
                return(IndexAction.Delete(_search.Keyed(
                                              packageRegistration.PackageId,
                                              searchFilters)));
            }

            if (changeType != SearchIndexChangeType.AddFirst)
            {
                throw new ArgumentException(
                          $"The only change types supported are {nameof(SearchIndexChangeType.AddFirst)} and " +
                          $"{nameof(SearchIndexChangeType.Delete)}.",
                          nameof(changeType));
            }

            var latestFlags = _search.LatestFlagsOrNull(versionLists, searchFilters);
            var package     = versionToPackage[latestFlags.LatestVersionInfo.ParsedVersion];
            var owners      = packageRegistration
                              .Owners
                              .OrderBy(u => u, StringComparer.InvariantCultureIgnoreCase)
                              .ToArray();

            VerifyConsistency(packageRegistration.PackageId, package);

            return(IndexAction.Upload <KeyedDocument>(_search.FullFromDb(
                                                          packageRegistration.PackageId,
                                                          searchFilters,
                                                          latestFlags.LatestVersionInfo.ListedFullVersions,
                                                          latestFlags.IsLatestStable,
                                                          latestFlags.IsLatest,
                                                          latestFlags.LatestVersionInfo.FullVersion,
                                                          package,
                                                          owners,
                                                          packageRegistration.TotalDownloadCount,
                                                          packageRegistration.IsExcludedByDefault)));
        }
            public void ReturnsDeleteSearchActionsForAllUnlisted()
            {
                var input = new NewPackageRegistration(
                    "NuGet.Versioning",
                    1001,
                    new string[0],
                    new[] { new TestPackage("1.0.0")
                            {
                                Listed = false
                            } },
                    false);

                var actions = _target.AddNewPackageRegistration(input);

                Assert.Equal(4, actions.Search.Count);
                Assert.Equal(IndexActionType.Delete, actions.Search[0].ActionType); // SearchFilters.Default
                Assert.Equal(IndexActionType.Delete, actions.Search[1].ActionType); // SearchFilters.IncludePrerelease
                Assert.Equal(IndexActionType.Delete, actions.Search[2].ActionType); // SearchFilters.IncludeSemVer2
                Assert.Equal(IndexActionType.Delete, actions.Search[3].ActionType); // SearchFilters.IncludePrereleaseAndSemVer2
            }
Ejemplo n.º 7
0
        public IndexActions AddNewPackageRegistration(NewPackageRegistration packageRegistration)
        {
            var versionProperties = new Dictionary <string, VersionPropertiesData>();
            var versionListData   = new VersionListData(versionProperties);
            var versionLists      = new VersionLists(versionListData);

            var changes = packageRegistration
                          .Packages
                          .Select(GetVersionListChange)
                          .ToList();
            var indexChanges = versionLists.ApplyChanges(changes);

            var versionToPackage = packageRegistration
                                   .Packages
                                   .ToDictionary(p => NuGetVersion.Parse(p.Version));

            var search = indexChanges
                         .Search
                         .Select(p => GetSearchIndexAction(
                                     packageRegistration,
                                     versionToPackage,
                                     versionLists,
                                     p.Key,
                                     p.Value))
                         .ToList();

            var hijack = indexChanges
                         .Hijack
                         .Select(p => GetHijackIndexAction(
                                     packageRegistration.PackageId,
                                     versionToPackage[p.Key],
                                     p.Value))
                         .ToList();

            return(new IndexActions(
                       search,
                       hijack,
                       new ResultAndAccessCondition <VersionListData>(
                           versionLists.GetVersionListData(),
                           AccessConditionWrapper.GenerateEmptyCondition())));
        }
            public void UsesProperVersionForBuilder(string version, string fullVersion)
            {
                var input = new NewPackageRegistration(
                    "NuGet.Versioning",
                    1001,
                    new string[0],
                    new[] { new TestPackage(version)
                            {
                                SemVerLevelKey = SemVerLevelKey.SemVer2
                            } },
                    false);

                var actions = _target.AddNewPackageRegistration(input);

                _search.Verify(
                    x => x.Keyed(input.PackageId, SearchFilters.Default),
                    Times.Once);
                _search.Verify(
                    x => x.Keyed(input.PackageId, SearchFilters.IncludePrerelease),
                    Times.Once);
                _search.Verify(
                    x => x.Keyed(input.PackageId, SearchFilters.IncludeSemVer2),
                    Times.Once);
                _search.Verify(
                    x => x.FullFromDb(
                        input.PackageId,
                        SearchFilters.IncludePrereleaseAndSemVer2,
                        It.IsAny <string[]>(),
                        It.IsAny <bool>(),
                        It.IsAny <bool>(),
                        fullVersion,
                        input.Packages[0],
                        input.Owners,
                        input.TotalDownloadCount,
                        It.IsAny <bool>()),
                    Times.Once);
            }