Skip to content

Instantly share code, notes, and snippets.

@amielc1
Last active November 23, 2020 05:14
Show Gist options
  • Save amielc1/4f3dd05e8f290be2a3187cacc2b8cbb1 to your computer and use it in GitHub Desktop.
Save amielc1/4f3dd05e8f290be2a3187cacc2b8cbb1 to your computer and use it in GitHub Desktop.
WaitUntil - Integration Tests
const int DefultNumberOfcheck = 100;
const int OneCycleMinimumSleepMsec = 50;
public void WaitUntil(TimeSpan timeout, Func<bool> checkCondition)
{
var sleepTime = getSleepTime(timeout);
Stopwatch sw = Stopwatch.StartNew();
while (true)
{
Assert.Less(sw.Elapsed, sleepTime);
if (checkCondition())
{
return;
}
Thread.Sleep(sleepTime);
}
}
private TimeSpan getSleepTime(TimeSpan timeout)
{
var sleepTimeoutmilisec = (int)(timeout.TotalMilliseconds / DefultNumberOfcheck);
return TimeSpan.FromMilliseconds(Math.Max(sleepTimeoutmilisec, OneCycleMinimumSleepMsec));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment