public void RetryDelayShouldBeCancellableT() { RetryPolicy rp = new RetryPolicy <string>(1000, ExponentialBackoffWaitingPolicy.Default, AllErrorsTransientDetectionStrategy <string> .Instance); Stopwatch sp = Stopwatch.StartNew(); bool operationCanceled = false; int attempts = 0; try { using (CancellationTokenSource cts = new CancellationTokenSource(50)) { rp.ExecuteWithRetriesAsync((ct) => Task.Run(async() => { attempts++; await Task.Delay(Timeout.Infinite, ct); }), TimeSpan.Zero, cts.Token).GetAwaiter().GetResult(); } } catch (OperationCanceledException) { Assert.IsTrue(sp.Elapsed < TimeSpan.FromSeconds(5), sp.Elapsed.ToString()); operationCanceled = true; } Assert.AreEqual(1, attempts); Assert.AreEqual(true, operationCanceled); }
public async Task RetryNegativeTimeoutOperationAsyncT() { RetryPolicy rp = new RetryPolicy <string>(5, new FixedWaitingPolicy(TimeSpan.FromMilliseconds(10)), AllErrorsTransientDetectionStrategy <string> .Instance); await rp.ExecuteWithRetriesAsync(async (ct) => { await Task.Delay(1); }, TimeSpan.FromMilliseconds(-1)); }
public async Task RetryTimeoutFailedAsync() { RetryPolicy rp = new RetryPolicy(5, new FixedWaitingPolicy(TimeSpan.FromMilliseconds(10)), AllErrorsTransientDetectionStrategy.Instance); int attempts = 0; await rp.ExecuteWithRetriesAsync((ct) => { attempts++; throw new OperationCanceledException(); }); }
public void RetryOperationShouldTimeout() { RetryPolicy rp = new RetryPolicy(5, new FixedWaitingPolicy(TimeSpan.FromMilliseconds(10)), AllErrorsTransientDetectionStrategy.Instance); Stopwatch sp = Stopwatch.StartNew(); bool operationCanceled = false; int attempts = 0; try { rp.ExecuteWithRetriesAsync((ct) => Task.Run(async() => { attempts++; await Task.Delay(Timeout.Infinite, ct); }), TimeSpan.FromMilliseconds(50), CancellationToken.None).GetAwaiter().GetResult(); } catch (TimeoutException) { Assert.IsTrue(sp.Elapsed < TimeSpan.FromSeconds(5), sp.Elapsed.ToString()); operationCanceled = true; } Assert.AreEqual(5, attempts); Assert.AreEqual(true, operationCanceled); }
/// <summary> /// Calls a service endpoint with retries. /// </summary> /// <typeparam name="TResult">Type of the result.</typeparam> /// <param name="rsp">ResolvedServicePartition instance.</param> /// <param name="token">CancellationToken instance.</param> /// <param name="func">Fucntion to execute, actually makeing the HTTP request.</param> /// <returns>Result of the function execution.</returns> private async Task <TResult> CallAsync <TResult>(ResolvedServicePartition rsp, CancellationToken token, Func <ResolvedServiceEndpoint, CancellationToken, Task <TResult> > func) { TResult result = default(TResult); // Wrap in a retry policy. await _retryPolicy.ExecuteWithRetriesAsync(async (ct) => { try { ResolvedServiceEndpoint rse = rsp.GetEndpoint(); result = await func(rse, ct); } catch (FabricTransientException) { rsp = await GetRspAsync(((Int64RangePartitionInformation)rsp.Info).LowKey, token).ConfigureAwait(false); } catch (HttpRequestException ex) when((ex.InnerException as WebException)?.Status == WebExceptionStatus.ConnectFailure) { rsp = await GetRspAsync(((Int64RangePartitionInformation)rsp.Info).LowKey, token).ConfigureAwait(false); } }, cancellationToken : token); return(result); }
public async Task RetryNullOperationAsyncT() { RetryPolicy rp = new RetryPolicy <string>(5, new FixedWaitingPolicy(TimeSpan.FromMilliseconds(10)), AllErrorsTransientDetectionStrategy <string> .Instance); await rp.ExecuteWithRetriesAsync(null); }