Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Asserts supporting auto-retry/polling #2223

Closed
Exoow opened this issue Feb 1, 2024 · 3 comments
Closed

[Feature] Asserts supporting auto-retry/polling #2223

Exoow opened this issue Feb 1, 2024 · 3 comments

Comments

@Exoow
Copy link
Contributor

Exoow commented Feb 1, 2024

Summary

Provide Assert methods that automatically retry the given function to assert until timeout.

Background and Motivation

Currently, my test code has asserts like these:

await Assert.That.ShouldBeEqual(value, GetValueAsync);
await Assert.That.ShouldBeTrue(async () => options.RowCount <= await GetRowCountAsync());

These are custom extensions that simply wrap around a Retry object that polls until success or a timeout.

public static async Task ShouldBeTrue(this Assert assert, Func<Task<bool>> condition, string? message = null, RetrierOptions? options = null)
{
    await retrier.Retry(async () => Assert.IsTrue(await condition(), message), options);
}

public static async Task ShouldBeEqual(this Assert assert, string expected, Func<Task<string?>> function, string? message = null, RetrierOptions? options = null)
{
    await retrier.Retry(async () => Assert.AreEqual(expected, await function(), message), options);
}

Proposed Feature

Ideally I can replace the code above by the following. Note, non-async counterparts are also useful.

await Assert.ToBeEqualAsync(T expected, Func<Task<T>> retryFunction, string message);
await Assert.ToBeTrueAsync(Func<Task<T>> retryFunction, string message);
// and this where the function should not throw an error
Assert.ToPass(Action retryFunction, string message);
await Assert.ToPassAsync(Func<Task> retryFunction, string message);
@Exoow Exoow changed the title Asserts supporting auto-retry/polling [Feature] Asserts supporting auto-retry/polling Feb 1, 2024
@Evangelink
Copy link
Member

Hi @Exoow,

Thanks for the suggestion!

I have to admit I am personally not fan of this feature request as I believe the assertion should only check values and that there are already many retry solutions available out there (e.g. https://github.com/App-vNext/Polly).

But I will keep this open for some time to collect thumbs up/down.

@Exoow
Copy link
Contributor Author

Exoow commented Feb 1, 2024

Hello @Evangelink - I understand, considering retries are only applicable in test levels beyond unit tests.

The Polly library seems a bit overkill in terms of syntax/features for simple asserts.
In case this doesn't make it, I'll stick to my own extension methods. Thanks for your feedback! :)

@Evangelink
Copy link
Member

@Exoow I will move forward by closing this issue as won't fix as there was no traction.

Thank you again for suggesting this feature.

@Evangelink Evangelink closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants