-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove test sleeps, fix flaky test (#194)
Signed-off-by: Todd Baert <todd.baert@dynatrace.com> Co-authored-by: Austin Drenski <austin@austindrenski.io>
- Loading branch information
1 parent
a790f78
commit f2b9b03
Showing
3 changed files
with
122 additions
and
38 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,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
internal class Utils | ||
{ | ||
/// <summary> | ||
/// Repeatedly runs the supplied assertion until it doesn't throw, or the timeout is reached. | ||
/// </summary> | ||
/// <param name="assertionFunc">Function which makes an assertion</param> | ||
/// <param name="timeoutMillis">Timeout in millis (defaults to 1000)</param> | ||
/// <param name="pollIntervalMillis">Poll interval (defaults to 100</param> | ||
/// <returns></returns> | ||
public static async Task AssertUntilAsync(Action<CancellationToken> assertionFunc, int timeoutMillis = 1000, int pollIntervalMillis = 100) | ||
{ | ||
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(default(CancellationToken))) | ||
{ | ||
|
||
cts.CancelAfter(timeoutMillis); | ||
|
||
var exceptions = new List<Exception>(); | ||
var message = "AssertUntilAsync timeout reached."; | ||
|
||
while (!cts.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
assertionFunc(cts.Token); | ||
return; | ||
} | ||
catch (TaskCanceledException) when (cts.IsCancellationRequested) | ||
{ | ||
throw new AggregateException(message, exceptions); | ||
} | ||
catch (Exception e) | ||
{ | ||
exceptions.Add(e); | ||
} | ||
|
||
try | ||
{ | ||
await Task.Delay(pollIntervalMillis, cts.Token).ConfigureAwait(false); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
throw new AggregateException(message, exceptions); | ||
} | ||
} | ||
throw new AggregateException(message, exceptions); | ||
} | ||
} | ||
} |
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using OpenFeature.Model; | ||
using Xunit; | ||
|
||
namespace OpenFeature.Tests | ||
{ | ||
public class TestUtilsTest | ||
{ | ||
[Fact] | ||
public async void Should_Fail_If_Assertion_Fails() | ||
{ | ||
await Assert.ThrowsAnyAsync<Exception>(() => Utils.AssertUntilAsync(_ => Assert.True(1.Equals(2)), 100, 10)).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async void Should_Pass_If_Assertion_Fails() | ||
{ | ||
await Utils.AssertUntilAsync(_ => Assert.True(1.Equals(1))).ConfigureAwait(false); | ||
} | ||
} | ||
} |