public async Task DownloadAsync_FailsOnFirstTryWhenExceptionIsNotHandledByPolicy()
        {
            // Expect the parameters to be passed to the method of the inner rest client.
            // We'll throw an exception from within the callback method to trigger retries.
            this.mockRestClient
            .Setup(c => c.DownloadAsync(
                       It.Is <EndpointName>(e => e.Equals(this.endpointName)),
                       It.Is <Dictionary <string, string> >(j => j.Equals(this.inputFilter)),
                       It.Is <LogContext>(l => l.Equals(this.logContext)),
                       It.IsAny <CancellationToken>()))
            .Callback((EndpointName en, Dictionary <string, string> fi, LogContext lc, CancellationToken ct)
                      => throw new BadRequestException("Bad request."));

            try
            {
                IRestClient restClient = GetRestClientWithRetries();
                await restClient.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default);

                Assert.Fail("Expected exception to be thrown.");
            }
            catch (BadRequestException)
            {
            }

            // Verify our inner rest client was called exactly once.
            this.mockRestClient.Verify(m => m.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default),
                                       Times.Once);
        }
        public async Task DownloadAsync_ExecutesRetryPolicyAsExpected()
        {
            // Expect the parameters to be passed to the method of the inner rest client.
            // We'll throw an exception from within the callback method to trigger retries.
            this.mockRestClient
            .Setup(c => c.DownloadAsync(
                       It.Is <EndpointName>(e => e.Equals(this.endpointName)),
                       It.Is <Dictionary <string, string> >(j => j.Equals(this.inputFilter)),
                       It.Is <LogContext>(l => l.Equals(this.logContext)),
                       It.IsAny <CancellationToken>()))
            .Callback((EndpointName en, Dictionary <string, string> fi, LogContext lc, CancellationToken ct)
                      => throw new ServiceUnavailableException("Something went wrong."));

            try
            {
                IRestClient restClient = GetRestClientWithRetries();
                await restClient.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default);

                Assert.Fail("Expected exception to be thrown.");
            }
            catch (ServiceUnavailableException)
            {
            }

            // Verify our inner rest client was called 4 times (original invocation + retries)
            this.mockRestClient.Verify(m => m.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default),
                                       Times.Exactly(RetryCount + 1));
        }
Beispiel #3
0
        public async Task <PluginInformation> InstallPluginAsync(Uri uri, CancellationToken cancellationToken)
        {
            var filename = uri.GetFilename();

            if (!uri.ToString().ToLowerInvariant().EndsWith(".zip"))
            {
                throw new ArgumentException($"Only understands ZIP files: {uri} ({filename})");
            }

            var pluginId = PluginId.With(Path.GetFileNameWithoutExtension(filename).ToLowerInvariant());

            if (_plugins.ContainsKey(pluginId))
            {
                await _pluginInstaller.UninstallPluginAsync(pluginId, cancellationToken).ConfigureAwait(false);
            }

            using (var tempFile = await _restClient.DownloadAsync(uri, cancellationToken).ConfigureAwait(false))
            {
                var pluginPath = await _pluginInstaller.InstallPluginAsync(
                    pluginId,
                    tempFile,
                    PluginPackageType.Zip,
                    cancellationToken)
                                 .ConfigureAwait(false);

                return(await LoadPluginAsync(
                           pluginPath,
                           cancellationToken)
                       .ConfigureAwait(false));
            }
        }
        public async Task DownloadAsync_SucceedsOnFirstTryWhenNoExceptionIsThrown()
        {
            // Expect the parameters to be passed to the method of the inner rest client.
            // We'll throw an exception from within the callback method to trigger retries.
            this.mockRestClient
            .Setup(c => c.DownloadAsync(
                       It.Is <EndpointName>(e => e.Equals(this.endpointName)),
                       It.Is <Dictionary <string, string> >(j => j.Equals(this.inputFilter)),
                       It.Is <LogContext>(l => l.Equals(this.logContext)),
                       It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(Encoding.UTF8.GetBytes(SuccessResult)));

            IRestClient restClient = GetRestClientWithRetries();

            byte[] result = await restClient.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default);

            string resultString = Encoding.UTF8.GetString(result);

            Assert.AreEqual(SuccessResult, resultString, $"Expected result: '{SuccessResult}'.");

            // Verify our inner rest client was called only once.
            this.mockRestClient.Verify(m => m.DownloadAsync(this.endpointName, this.inputFilter, this.logContext, default),
                                       Times.Once);
        }