Skip to content

Instantly share code, notes, and snippets.

@natuanorg
Forked from sdsantos/Wait.java
Created April 13, 2017 04:22
Show Gist options
  • Save natuanorg/f57968f55a2afe22287f8fd790951ccd to your computer and use it in GitHub Desktop.
Save natuanorg/f57968f55a2afe22287f8fd790951ccd to your computer and use it in GitHub Desktop.
Wait for condition helper for testing
public class Wait {
private static final int CHECK_INTERVAL = 100;
private static final int TIMEOUT = 10000;
public interface Condition {
boolean check();
}
private Condition mCondition;
public Wait(Condition condition) {
mCondition = condition;
}
public void waitForIt() {
boolean state = mCondition.check();
long startTime = System.currentTimeMillis();
while (!state) {
try {
Thread.sleep(CHECK_INTERVAL);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (System.currentTimeMillis() - startTime > TIMEOUT) {
throw new AssertionError("Wait timeout.");
}
state = mCondition.check();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment