Esempio n. 1
0
        public async Task <TestCaseRest> GetById(Guid caseId)
        {
            _logger.LogTrace("Retrieving test case {id}", caseId);
            string uriPath = $"cases/{HttpUtility.UrlEncode(caseId.ToString())}";


            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetryAsync(
                new[] { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3) },
                onRetry: (exception, retryCount) =>
            {
                _logger.LogTrace("Retrying failed request error {e}", exception);
            });

            var fallbackPolicy = Policy
                                 .Handle <Exception>()
                                 .FallbackAsync(
                fallbackAction: async token => { await Task.Delay(0); },
                onFallbackAsync: async(exception) =>
            {
                _logger.LogWarning("Failed {e}", exception);
                await Task.Delay(0);         // Get rid of warning
            }).WrapAsync(retryPolicy);

            TestCaseRest testCase = null;

            await fallbackPolicy.ExecuteAsync(async() =>
            {
                var result = await _client.GetAsync(uriPath);
                if ((int)result.StatusCode == 404)
                {
                    return;
                }
                result.EnsureSuccessStatusCode();
                testCase = await result.Content.ReadAsAsync <TestCaseRest>();
            });

            return(testCase);
        }
Esempio n. 2
0
        public async Task CreateCase(TestCaseRest testCase)
        {
            _logger.LogTrace("Creating new test case {case}", testCase);
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    var result = await _client.PostAsJsonAsync("cases/", testCase);

                    if (result.IsSuccessStatusCode)
                    {
                        _logger.LogTrace("Test case creation returned success code");
                        return;
                    }
                    _logger.LogWarning("Test case creation returned code {code}", result.StatusCode);
                }
                throw new OperationFailedException("Too many retries");
            }
            catch (Exception e)
            {
                _logger.LogWarning("Creating new test case failed with: {e}", e);
                throw new OperationFailedException();
            }
        }