SOLR-12087: Fixed bug in TimeOut.waitFor that can cause an infinite loop and added javadocs

This commit is contained in:
Shalin Shekhar Mangar 2018-03-26 16:58:56 +05:30
parent 07d255a708
commit b0f677c383
1 changed files with 8 additions and 1 deletions

View File

@ -51,9 +51,16 @@ public class TimeOut {
return unit.convert(timeSource.getTimeNs() - startTime, NANOSECONDS);
}
/**
* Wait until the given {@link Supplier} returns true or the time out expires which ever happens first
* @param messageOnTimeOut the exception message to be used in case a TimeoutException is thrown
* @param supplier a {@link Supplier} that returns a {@link Boolean} value
* @throws InterruptedException if any thread has interrupted the current thread
* @throws TimeoutException if the timeout expires
*/
public void waitFor(String messageOnTimeOut, Supplier<Boolean> supplier)
throws InterruptedException, TimeoutException {
while (!supplier.get() && hasTimedOut()) {
while (!supplier.get() && !hasTimedOut()) {
Thread.sleep(500);
}
if (hasTimedOut()) throw new TimeoutException(messageOnTimeOut);