Created
December 1, 2017 15:46
-
-
Save batmat/dca3b5ecf19efcccfce815bad1568189 to your computer and use it in GitHub Desktop.
Awaitility quick demo code
This file contains hidden or 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
* Java source, ASCII text | |
import org.apache.commons.io.FileUtils; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
import javax.annotation.Nonnull; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
import static org.awaitility.Awaitility.await; | |
public class AwaitilityDummyExampleTest { | |
@Test | |
public void demo() throws Exception { | |
await().atMost(1, TimeUnit.SECONDS) | |
.pollInterval(50, TimeUnit.MILLISECONDS) | |
.until(() -> asyncResource.length() > 0); | |
} | |
@Rule | |
public TemporaryFolder folder = new TemporaryFolder(); | |
@Nonnull | |
File asyncResource; | |
private int MAX_WAIT_BEFORE_WRITE_MILLISECONDS = 10 * 1000; | |
/** | |
* Will wait a random amount of time, max MAX_WAIT + 1 second, before writing into {@link #asyncResource} | |
*/ | |
@Before | |
public void fakeAsyncDemo() throws IOException { | |
asyncResource = folder.newFile(); | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
Thread.sleep(1000); | |
// Sleep up to 10 seconds before writing into the file | |
final int millis = new Random(System.currentTimeMillis()).nextInt( | |
MAX_WAIT_BEFORE_WRITE_MILLISECONDS); | |
System.err.println("Sleeping for "+millis+" ms"); | |
Thread.sleep(millis); | |
FileUtils.writeStringToFile(asyncResource, "blah"); | |
} catch (Exception e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
}.start(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment