forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ProcessTests.HandleCountChanges more reliable (dotnet#37330)
* Make ProcessTests.HandleCountChanges more reliable * Address PR feedback * Update CoreFx.Private.TestUtilities.csproj
- Loading branch information
1 parent
49262a8
commit d398edf
Showing
6 changed files
with
159 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/CoreFx.Private.TestUtilities/src/System/RetryHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System | ||
{ | ||
public static partial class RetryHelper | ||
{ | ||
private static readonly Func<int, int> s_defaultBackoffFunc = i => Math.Min(i * 100, 60_000); | ||
|
||
/// <summary>Executes the <paramref name="test"/> action up to a maximum of <paramref name="maxAttempts"/> times.</summary> | ||
/// <param name="maxAttempts">The maximum number of times to invoke <paramref name="test"/>.</param> | ||
/// <param name="test">The test to invoke.</param> | ||
/// <param name="backoffFunc">After a failure, invoked to determine how many milliseconds to wait before the next attempt. It's passed the number of iterations attempted.</param> | ||
public static void Execute(Action test, int maxAttempts = 5, Func<int, int> backoffFunc = null) | ||
{ | ||
// Validate arguments | ||
if (maxAttempts < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(maxAttempts)); | ||
} | ||
if (test == null) | ||
{ | ||
throw new ArgumentNullException(nameof(test)); | ||
} | ||
|
||
// Execute the test until it either passes or we run it maxAttempts times | ||
var exceptions = new List<Exception>(); | ||
for (int i = 1; i <= maxAttempts; i++) | ||
{ | ||
try | ||
{ | ||
test(); | ||
return; | ||
} | ||
catch (Exception e) | ||
{ | ||
exceptions.Add(e); | ||
if (i == maxAttempts) | ||
{ | ||
throw new AggregateException(exceptions); | ||
} | ||
} | ||
|
||
Thread.Sleep((backoffFunc ?? s_defaultBackoffFunc)(i)); | ||
} | ||
} | ||
|
||
/// <summary>Executes the <paramref name="test"/> action up to a maximum of <paramref name="maxAttempts"/> times.</summary> | ||
/// <param name="maxAttempts">The maximum number of times to invoke <paramref name="test"/>.</param> | ||
/// <param name="test">The test to invoke.</param> | ||
/// <param name="backoffFunc">After a failure, invoked to determine how many milliseconds to wait before the next attempt. It's passed the number of iterations attempted.</param> | ||
public static async Task ExecuteAsync(Func<Task> test, int maxAttempts = 5, Func<int, int> backoffFunc = null) | ||
{ | ||
// Validate arguments | ||
if (maxAttempts < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(maxAttempts)); | ||
} | ||
if (test == null) | ||
{ | ||
throw new ArgumentNullException(nameof(test)); | ||
} | ||
|
||
// Execute the test until it either passes or we run it maxAttempts times | ||
var exceptions = new List<Exception>(); | ||
for (int i = 1; i <= maxAttempts; i++) | ||
{ | ||
try | ||
{ | ||
await test().ConfigureAwait(false); | ||
return; | ||
} | ||
catch (Exception e) | ||
{ | ||
exceptions.Add(e); | ||
if (i == maxAttempts) | ||
{ | ||
throw new AggregateException(exceptions); | ||
} | ||
} | ||
|
||
await Task.Delay((backoffFunc ?? s_defaultBackoffFunc)(i)).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters