-
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.
- Loading branch information
Showing
15 changed files
with
790 additions
and
482 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace CloudFabric.EventSourcing.Tests; | ||
|
||
public static class TestHelpers | ||
{ | ||
public static async Task<T?> RepeatUntilNotNull<T>(Func<Task<T?>> lambdaToRepeat, TimeSpan timeout, int millisecondsRetryDelay = 500) | ||
{ | ||
var startTime = DateTime.UtcNow; | ||
var result = default(T); | ||
|
||
while (result == null || DateTime.UtcNow - startTime > timeout) | ||
{ | ||
result = await lambdaToRepeat(); | ||
if (result != null) | ||
{ | ||
return result; | ||
} | ||
|
||
await Task.Delay(millisecondsRetryDelay); | ||
} | ||
|
||
throw new Exception("Function failed to return non-null value within timeout"); | ||
} | ||
|
||
/// <summary> | ||
/// Calls `functionToRepeat` and passes result value to `conditionCheckFunction` until it returns true. | ||
/// Returns result of `functionToRepeat` when `conditionCheckFunction` returns true or null when timeout passes. | ||
/// </summary> | ||
/// <param name="functionToRepeat"></param> | ||
/// <param name="conditionCheckFunction"></param> | ||
/// <param name="timeout"></param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
/// <exception cref="Exception"></exception> | ||
public static async Task<T?> RepeatUntil<T>(Func<Task<T?>> functionToRepeat, Func<T?, bool> conditionCheckFunction, TimeSpan timeout, int millisecondsRetryDelay = 500) | ||
{ | ||
var startTime = DateTime.UtcNow; | ||
var result = default(T); | ||
bool? conditionResult = null; | ||
|
||
while (DateTime.UtcNow - startTime < timeout) | ||
{ | ||
result = await functionToRepeat(); | ||
|
||
conditionResult = conditionCheckFunction(result); | ||
if (conditionResult == true) | ||
{ | ||
return result; | ||
} | ||
|
||
await Task.Delay(millisecondsRetryDelay); | ||
} | ||
|
||
throw new Exception("Function failed to return non-null value within timeout"); | ||
} | ||
} |
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
Oops, something went wrong.