Last active
November 23, 2020 05:14
-
-
Save amielc1/4f3dd05e8f290be2a3187cacc2b8cbb1 to your computer and use it in GitHub Desktop.
WaitUntil - Integration Tests
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
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