Skip to content

Each SmartWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting

License

Notifications You must be signed in to change notification settings

valeraf23/SmartWait

Repository files navigation

SmartWait

Each SmartWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting

NuGet.org Nuget Build status .NET Actions Status

Installation

Install with NuGet Package Manager Console

Install-Package SmartWait

Install with .NET CLI

dotnet add package SmartWait

Example:

WaitFor.Condition(waitCondition, timeoutMessage);
                     
WaitFor.Condition(waitCondition, builder=>builder
                                   .SetMaxWaitTime(maxWaitTime)
                                   .SetCallbackForSuccessful(callback)
                                   .SetNotIgnoredExceptionType(notIgnoredExceptionType)
                                   .Build(), timeoutMessage);
                                   
 static async Task<bool> Expected()
 {
     await Task.Delay(TimeSpan.FromSeconds(1));
     return true;
 }
 
 await WaitFor.Condition(Expected, DefaultTimeOutMessage, timeLimit);
 

In case when you use WaitFor.Condition if the given condition is not met will be rise exception

Screenshot

In case when some exceptions happen and we got not expected value we can read information about a number of exceptions and where it happened

Screenshot

In case when you use WaitFor.For this function wait until the specified condition is met and return the value that we expected. To do this, you must specify the actions in case of failure using the method OnFailure

 var result = WaitFor.For(() => 0)
                .Become(a => a == 5)
                .OnFailure(_ => 1, fail => fail is NotExpectedValue<int>)
                .OnFailure(_ => -2);
                
 //asynchronous option         
  var result = WaitFor.ForAsync(async () =>
     {
         await Task.Delay(10);
         return 0;
     })
     .Become(a => a == 5)
     .OnFailure(_ => 1, fail => fail is NotExpectedValue<int>)
     .OnFailure(_ => -2);                

Using the OnSuccess method, you can specify actions on the value in case of a successful result

var res = WaitFor.For(() => actual).Become(a => a == 3)
              .OnSuccess(x => $"New result {x}")
              .OnFailureThrowException();
// "New result 3"

Result On Failure can be in two cases:

  • get not expected value
    • returns NotExpectedValue<T> type.
  • due to some exceptions
    • returns ExceptionsHappened type.

We have methods that can help to handle these cases:

  • WhenNotExpectedValue and DoWhenNotExpectedValue
  • WhenWasExceptions and DoWhenWasExceptions
 var res = WaitFor.For(() => 3)
               .Become(a => a == 4)
               .WhenNotExpectedValue(x => x.ActuallyValue)
               .OnFailure(_ => 0);
Console.WriteLine(res) //3

 WaitFor.For(() => 3)
               .Become(a => a == 4)
               .DoWhenNotExpectedValue(x => Console.WriteLine(x))
               .OnFailure(_ => 0);
//  Console output :
//  Timeout after 30.6826992 second(s) and NUMBER OF ATTEMPTS 17 
//  Expected: (a) => a == 4, but parameter 'a': 3

var testClass = new SomeClass
{
    SomeNumber = 3,
    Child = new OtherClass
    {
       SomeNumber = 5
    }
};

_ = WaitFor.For(() => testClass)
     .Become(a => a.Child.SomeNumber == 1 && a.SomeNumber == 3)
     .DoWhenNotExpectedValue(x => Console.WriteLine(x));
/*  Console output :      
   Timeout after 30.6749663 second(s) and NUMBER OF ATTEMPTS 17 
   Expected: (a) => a.Child.SomeNumber(5) == 1 && a.SomeNumber(3) == 3
*/

If you use OnFailureThrowException , exception will be throw with next message

await WaitFor.ForAsync(Expected)
              .Become(a => a.Child.SomeNumber == 1 && a.SomeNumber == 3)
              .OnFailureThrowException();

Screenshot

You can use the predefined algorithm like LogarithmStep and ParabolaStep which calculate delay steps

var res = WaitFor.For(() => actual,
                  w => w.SetLogarithmStep(Time.FromSeconds).Build())
                 .Become(a => a == 3)
                 .OnFailureThrowException();

Also, you can use your custom algorithm for delayed steps

var res = WaitFor.For(() => actual, 
                      b => b.SetTimeBetweenStep(retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
                     .Build())
                     .Become(a => a == 5);

For additional information look in Tests Cases

About

Each SmartWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages