| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace Azure.Core.TestFramework |
| | | 9 | | { |
| | | 10 | | public static class TestRetryHelper |
| | | 11 | | { |
| | | 12 | | public static async Task<T> RetryAsync<T>(Func<Task<T>> operation, int maxIterations = 20, TimeSpan delay = defa |
| | | 13 | | { |
| | 0 | 14 | | if (delay == default) |
| | | 15 | | { |
| | 0 | 16 | | delay = TimeSpan.FromSeconds(5); |
| | | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | List<Exception> exceptions = null; |
| | | 20 | | |
| | 0 | 21 | | for (int i = 0; i < maxIterations; i++) |
| | | 22 | | { |
| | | 23 | | try |
| | | 24 | | { |
| | 0 | 25 | | return await operation().ConfigureAwait(false); |
| | | 26 | | } |
| | | 27 | | catch (Exception e) |
| | | 28 | | { |
| | 0 | 29 | | exceptions ??= new List<Exception>(); |
| | 0 | 30 | | exceptions.Add(e); |
| | | 31 | | |
| | 0 | 32 | | await Task.Delay(delay); |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | throw new AggregateException(exceptions); |
| | 0 | 37 | | } |
| | | 38 | | } |
| | | 39 | | } |