Wait for condition helper for testing
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
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
Usage: