Пример #1
0
        public async Task DoesNotSkipPackagesWhenExceptionOccurs(string catalogUri, string expectedCursorBeforeRetry)
        {
            // Arrange
            SharedInit(useLegacy: true, useSemVer2: false);

            var catalogStorage = Catalogs.CreateTestCatalogWithCommitThenTwoPackageCommit();
            await _mockServer.AddStorageAsync(catalogStorage);

            // Make the first request for a catalog leaf node fail. This will cause the registration collector
            // to fail the first time but pass the second time.
            FailFirstRequest(catalogUri);

            expectedCursorBeforeRetry = expectedCursorBeforeRetry ?? MemoryCursor.MinValue.ToString("O");

            ReadWriteCursor front = new DurableCursor(
                _legacyStorage.ResolveUri("cursor.json"),
                _legacyStorage,
                MemoryCursor.MinValue);
            ReadCursor back = MemoryCursor.CreateMax();

            // Act
            await Assert.ThrowsAsync <BatchProcessingException>(
                () => _target.RunAsync(front, back, CancellationToken.None));

            var cursorBeforeRetry = front.Value;
            await _target.RunAsync(front, back, CancellationToken.None);

            var cursorAfterRetry = front.Value;

            // Assert
            var unlistedPackage100 = _legacyStorage
                                     .Content
                                     .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/unlistedpackage/1.0.0.json"));

            Assert.NotNull(unlistedPackage100.Key);

            var listedPackage101 = _legacyStorage
                                   .Content
                                   .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/listedpackage/1.0.1.json"));

            Assert.NotNull(listedPackage101.Key);

            var anotherPackage100 = _legacyStorage
                                    .Content
                                    .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/anotherpackage/1.0.0.json"));

            Assert.NotNull(anotherPackage100.Key);

            Assert.Equal(MemoryCursor.MinValue, cursorBeforeRetry);
            Assert.Equal(DateTime.Parse("2015-10-12T10:08:55.3335317Z").ToUniversalTime(), cursorAfterRetry);
        }
        public async Task GraphsAreBatchedById()
        {
            // Arrange
            Initialize();

            var catalogStorage = Catalogs.CreateTestCatalogWithCommitThenTwoPackageCommit();
            await _mockServer.AddStorageAsync(catalogStorage);

            // Act
            var result = await _collector.RunAsync(CancellationToken.None);

            // Assert
            Assert.Equal(
                new[] { "AnotherPackage", "ListedPackage", "UnlistedPackage" },
                _collector.AllSortedGraphs.Select(x => x.Key).OrderBy(x => x).ToArray());
        }
        public async Task AllGraphsAreReadOnly()
        {
            // Arrange
            Initialize();

            var catalogStorage = Catalogs.CreateTestCatalogWithCommitThenTwoPackageCommit();
            await _mockServer.AddStorageAsync(catalogStorage);

            // Act
            var result = await _collector.RunAsync(CancellationToken.None);

            // Assert
            foreach (var graphs in _collector.AllSortedGraphs)
            {
                foreach (var pair in graphs.Value)
                {
                    Assert.IsType <ReadOnlyGraph>(pair.Value);
                }
            }

            Assert.True(result, "The result of running the test collector should be true.");
        }
Пример #4
0
        public async Task DoesNotSkipPackagesWhenExceptionOccurs(string catalogUri, string expectedCursorBeforeRetry)
        {
            // Arrange
            var storage        = new MemoryStorage();
            var storageFactory = new TestStorageFactory(name => storage.WithName(name));

            MockServerHttpClientHandler mockServer;

            mockServer = new MockServerHttpClientHandler();
            mockServer.SetAction("/", request => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));

            var catalogStorage = Catalogs.CreateTestCatalogWithCommitThenTwoPackageCommit();
            await mockServer.AddStorageAsync(catalogStorage);

            // Make the first request for a catalog leaf node fail. This will cause the registration collector
            // to fail the first time but pass the second time.
            FailFirstRequest(mockServer, catalogUri);

            expectedCursorBeforeRetry = expectedCursorBeforeRetry ?? MemoryCursor.MinValue.ToString("O");

            ReadWriteCursor front = new DurableCursor(
                storage.ResolveUri("cursor.json"),
                storage,
                MemoryCursor.MinValue);
            ReadCursor back = MemoryCursor.CreateMax();

            using (var testDirectory = TestDirectory.Create())
            {
                var luceneDirectory = new SimpleFSDirectory(new DirectoryInfo(testDirectory));
                using (var indexWriter = Catalog2LuceneJob.CreateIndexWriter(luceneDirectory))
                {
                    var target = new SearchIndexFromCatalogCollector(
                        new Uri("http://tempuri.org/index.json"),
                        indexWriter,
                        commitEachBatch: true,
                        baseAddress: null,
                        telemetryService: new Mock <ITelemetryService>().Object,
                        logger: new TestLogger(),
                        handlerFunc: () => mockServer);

                    // Act
                    await Assert.ThrowsAsync <Exception>(() => target.RunAsync(front, back, CancellationToken.None));

                    var cursorBeforeRetry = front.Value;
                    await target.RunAsync(front, back, CancellationToken.None);

                    var cursorAfterRetry = front.Value;

                    // Assert
                    var reader    = indexWriter.GetReader();
                    var documents = Enumerable
                                    .Range(0, reader.NumDeletedDocs + reader.NumDocs())
                                    .Where(i => !reader.IsDeleted(i))
                                    .Select(i => reader.Document(i))
                                    .ToList();
                    Assert.Equal(4, documents.Count);

                    var documentsByType = documents
                                          .ToLookup(doc => doc
                                                    .fields_ForNUnit
                                                    .FirstOrDefault(f => f.Name == "@type")?
                                                    .StringValue);
                    var commitDocuments  = documentsByType[Schema.DataTypes.CatalogInfastructure.AbsoluteUri.ToString()].ToList();
                    var packageDocuments = documentsByType[null].ToList();
                    Assert.Equal(1, commitDocuments.Count);
                    Assert.Equal(3, packageDocuments.Count);

                    Assert.Equal(
                        "UnlistedPackage",
                        packageDocuments[0].fields_ForNUnit.FirstOrDefault(x => x.Name == Constants.LucenePropertyId)?.StringValue);
                    Assert.Equal(
                        "ListedPackage",
                        packageDocuments[1].fields_ForNUnit.FirstOrDefault(x => x.Name == Constants.LucenePropertyId)?.StringValue);
                    Assert.Equal(
                        "AnotherPackage",
                        packageDocuments[2].fields_ForNUnit.FirstOrDefault(x => x.Name == Constants.LucenePropertyId)?.StringValue);

                    Assert.Equal(DateTime.Parse(expectedCursorBeforeRetry).ToUniversalTime(), cursorBeforeRetry);
                    Assert.Equal(DateTime.Parse("2015-10-12T10:08:55.3335317Z").ToUniversalTime(), cursorAfterRetry);
                }
            }
        }
        public async Task RunAsync_WhenExceptionOccurs_DoesNotSkipPackage(string catalogUri, string expectedCursorBeforeRetry)
        {
            // Arrange
            var catalogStorage = Catalogs.CreateTestCatalogWithCommitThenTwoPackageCommit();
            await _mockServer.AddStorageAsync(catalogStorage);

            _mockServer.SetAction(
                "/packages/unlistedpackage.1.0.0.nupkg",
                request => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(File.OpenRead("Packages\\UnlistedPackage.1.0.0.zip"))
            }));
            _mockServer.SetAction(
                "/packages/listedpackage.1.0.1.nupkg",
                request => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(File.OpenRead("Packages\\ListedPackage.1.0.1.zip"))
            }));
            _mockServer.SetAction(
                "/packages/anotherpackage.1.0.0.nupkg",
                request => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(File.OpenRead("Packages\\ListedPackage.1.0.0.zip"))
            }));

            // Make the first request for a catalog leaf node fail. This will cause the registration collector
            // to fail the first time but pass the second time.
            FailFirstRequest(catalogUri);

            expectedCursorBeforeRetry = expectedCursorBeforeRetry ?? MemoryCursor.MinValue.ToString("O");

            var front = new DurableCursor(
                _cursorJsonUri,
                _catalogToDnxStorage,
                MemoryCursor.MinValue);
            ReadCursor back = MemoryCursor.CreateMax();

            // Act
            await Assert.ThrowsAsync <HttpRequestException>(() => _target.RunAsync(front, back, CancellationToken.None));

            var cursorBeforeRetry = front.Value;
            await _target.RunAsync(front, back, CancellationToken.None);

            var cursorAfterRetry = front.Value;

            // Assert
            var unlistedPackage100 = _catalogToDnxStorage
                                     .Content
                                     .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/unlistedpackage/1.0.0/unlistedpackage.1.0.0.nupkg"));

            Assert.NotNull(unlistedPackage100.Key);

            var listedPackage101 = _catalogToDnxStorage
                                   .Content
                                   .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/listedpackage/1.0.1/listedpackage.1.0.1.nupkg"));

            Assert.NotNull(listedPackage101.Key);

            var anotherPackage100 = _catalogToDnxStorage
                                    .Content
                                    .FirstOrDefault(pair => pair.Key.PathAndQuery.EndsWith("/anotherpackage/1.0.0/anotherpackage.1.0.0.nupkg"));

            Assert.NotNull(anotherPackage100.Key);

            Assert.Equal(DateTime.Parse(expectedCursorBeforeRetry).ToUniversalTime(), cursorBeforeRetry);
            Assert.Equal(DateTime.Parse("2015-10-12T10:08:55.3335317Z").ToUniversalTime(), cursorAfterRetry);
        }