コード例 #1
0
        PackagePhysicalFileMetadata DownloadPackage(
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff)
        {
            Log.Info("Downloading NuGet package {0} v{1} from feed: '{2}'", packageId, version, feedUri);
            Log.VerboseFormat("Downloaded package will be stored in: '{0}'", cacheDirectory);
            freeSpaceChecker.EnsureDiskHasEnoughFreeSpace(cacheDirectory);

            var fullPathToDownloadTo = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, ".nupkg"));

            var downloader = new InternalNuGetPackageDownloader(fileSystem);

            downloader.DownloadPackage(packageId,
                                       version,
                                       feedUri,
                                       feedCredentials,
                                       fullPathToDownloadTo,
                                       maxDownloadAttempts,
                                       downloadAttemptBackoff);

            var pkg = PackagePhysicalFileMetadata.Build(fullPathToDownloadTo);

            if (pkg == null)
            {
                throw new CommandException($"Package metadata for {packageId}, version {version}, could not be determined");
            }

            CheckWhetherThePackageHasDependencies(pkg);
            return(pkg);
        }
コード例 #2
0
        public int AttemptsTheRightNumberOfTimesOnError(int maxDownloadAttempts)
        {
            var packageId       = "FakePackageId";
            var version         = VersionFactory.CreateSemanticVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";
            var filesystem      = Substitute.For <ICalamariFileSystem>();
            var variables       = new CalamariVariables();

            var calledCount = 0;

            Assert.Throws <Exception>(() =>
            {
                var downloader = new InternalNuGetPackageDownloader(filesystem, variables);
                downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: maxDownloadAttempts, downloadAttemptBackoff: TimeSpan.Zero,
                                           action: (arg1, arg2, arg3, arg4, arg5) =>
                {
                    calledCount++;
                    throw new Exception("Expected exception from test: simulate download failing");
                });
            });

            return(calledCount);
        }
コード例 #3
0
        void RunNugetV3TimeoutTest(string timeoutInVariables, TimeSpan serverResponseTime, TimeSpan estimatedTimeout)
        {
            using (var server = new TestHttpServer(9001, serverResponseTime))
            {
                var packageId       = "FakePackageId";
                var version         = VersionFactory.CreateSemanticVersion(1, 2, 3);
                var feedCredentials = new CredentialCache();
                var targetFilePath  = "FakeTargetFilePath";
                var filesystem      = Substitute.For <ICalamariFileSystem>();
                var v3NugetUri      = new Uri(server.BaseUrl + "/index.json");
                var variables       = new CalamariVariables();

                if (timeoutInVariables != null)
                {
                    variables[KnownVariables.NugetHttpTimeout] = timeoutInVariables;
                }

                var downloader = new InternalNuGetPackageDownloader(filesystem, variables);

                var stopwatch = new Stopwatch();

                Action invocation = () =>
                {
                    stopwatch.Start();
                    try
                    {
                        downloader.DownloadPackage(
                            packageId,
                            version,
                            v3NugetUri,
                            feedCredentials,
                            targetFilePath,
                            maxDownloadAttempts: 1,
                            downloadAttemptBackoff: TimeSpan.Zero
                            );
                    }
                    finally
                    {
                        stopwatch.Stop();
                    }
                };

                invocation.Should()
                .ThrowExactly <Exception>();

                stopwatch.Elapsed
                .Should()
                .BeCloseTo(estimatedTimeout, TimeSpan.FromSeconds(0.5));
            }
        }
コード例 #4
0
        public void AttemptsOnlyOnceIfSuccessful()
        {
            var packageId       = "FakePackageId";
            var version         = VersionFactory.CreateSemanticVersion(1, 2, 3);
            var feedUri         = new Uri("http://www.myget.org");
            var feedCredentials = new CredentialCache();
            var targetFilePath  = "FakeTargetFilePath";
            var filesystem      = Substitute.For <ICalamariFileSystem>();

            var calledCount = 0;
            var downloader  = new InternalNuGetPackageDownloader(filesystem);

            downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, targetFilePath, maxDownloadAttempts: 5, downloadAttemptBackoff: TimeSpan.Zero, action: (arg1, arg2, arg3, arg4, arg5) =>
            {
                calledCount++;
            });

            Assert.That(calledCount, Is.EqualTo(1));
        }