コード例 #1
0
        public void ItReturnsOnSuccess()
        {
            var retryCount = 0;
            Func <Task <string> > action = () => {
                retryCount++;
                return(Task.FromResult("done"));
            };
            var res = ExponentialRetry.ExecuteWithRetryOnFailure <string>(action).Result;

            retryCount.Should().Be(1);
        }
コード例 #2
0
        public async Task <string> DownloadPackageAsync(PackageId packageId,
                                                        NuGetVersion packageVersion = null,
                                                        PackageSourceLocation packageSourceLocation = null,
                                                        bool includePreview          = false,
                                                        DirectoryPath?downloadFolder = null)
        {
            CancellationToken cancellationToken = CancellationToken.None;

            (var source, var resolvedPackageVersion) = await GetPackageSourceAndVerion(packageId, packageVersion,
                                                                                       packageSourceLocation, includePreview);

            FindPackageByIdResource resource   = null;
            SourceRepository        repository = GetSourceRepository(source);

            resource = await repository.GetResourceAsync <FindPackageByIdResource>(cancellationToken)
                       .ConfigureAwait(false);

            if (resource == null)
            {
                throw new NuGetPackageNotFoundException(
                          string.Format(LocalizableStrings.FailedToLoadNuGetSource, source.Source));
            }

            string nupkgPath = downloadFolder == null || !downloadFolder.HasValue
                ? Path.Combine(_packageInstallDir.Value, packageId.ToString(),
                               resolvedPackageVersion.ToNormalizedString(),
                               $"{packageId}.{resolvedPackageVersion.ToNormalizedString()}.nupkg")
                : Path.Combine(downloadFolder.Value.Value,
                               $"{packageId}.{resolvedPackageVersion.ToNormalizedString()}.nupkg");

            Directory.CreateDirectory(Path.GetDirectoryName(nupkgPath));
            using FileStream destinationStream = File.Create(nupkgPath);
            bool success = await ExponentialRetry.ExecuteWithRetryOnFailure(async() => await resource.CopyNupkgToStreamAsync(
                                                                                packageId.ToString(),
                                                                                resolvedPackageVersion,
                                                                                destinationStream,
                                                                                _cacheSettings,
                                                                                _verboseLogger,
                                                                                cancellationToken));

            destinationStream.Close();

            if (!success)
            {
                throw new NuGetPackageInstallerException(
                          string.Format("Downloading {0} version {1} failed", packageId,
                                        packageVersion.ToNormalizedString()));
            }

            VerifySigning(nupkgPath);

            return(nupkgPath);
        }
コード例 #3
0
        public void ItRetriesOnError()
        {
            var retryCount = 0;
            Func <Task <string> > action = () => {
                retryCount++;
                throw new Exception();
            };

            Assert.Throws <AggregateException>(() => ExponentialRetry.ExecuteWithRetryOnFailure <string>(action, 2, timer: () => ExponentialRetry.Timer(ExponentialRetry.TestingIntervals)).Result);

            retryCount.Should().Be(2);
        }