public abstract class Poller extends Object
For typical use, create an anonymous subclass inline, and implement the poll() method:
new Poller( times(10), every(100, MILLISECONDS) ) {
public void poll() {
assertThat(someMethodCall(), is("Success"));
}
};
This assertion will be called a number of times (configurable by using the different constructors). The first poll is tried immediately, so if you specify 3 attempts with 1 second pause, the total
execution time will be about 2 seconds.
This class catches any AssertionError thrown in the first n-1 attempts. If any other kind of Exception is encountered, or an AssertionError is thrown at the last attempt then it
will be thrown immediately and your test will fail. All AssertionErrors except the last are swallowed.
For debugging purposes you may call loudly() in your poll method, and interim AssertionErrors will be logged to System.out rather than being simply ignored.
| Constructor and Description |
|---|
Poller()
Creates a new Poller set to repeat the
poll() once per second for ten seconds. |
Poller(int times)
Creates a new Poller set to repeat the
poll() once per second for the specified number of times. |
Poller(int times,
TimeDuration pollPeriod)
Creates a new Poller set to repeat the
poll() once every sleepDuration timeUnits for the specified number of times. |
| Modifier and Type | Method and Description |
|---|---|
static TimeDuration |
every(long duration,
TimeUnit unit)
For helping your fluent interface, this simply creates a
TimeDuration object in a more natural way. |
protected void |
loudly()
Call this method in your
poll() implementation (before any assertions)
to enable logging of intermediate assertions to System.out. |
abstract void |
poll()
Override this method with some kind of assertion that will be re-run according to the polling schedule.
|
static int |
times(int count)
For helping you define your fluent interface.
|
public Poller()
poll() once per second for ten seconds.public Poller(int times)
poll() once per second for the specified number of times.times - The number of times to poll.public Poller(int times,
TimeDuration pollPeriod)
poll() once every sleepDuration timeUnits for the specified number of times.times - The number of times to try.pollPeriod - The duration of the sleep between each poll.protected final void loudly()
poll() implementation (before any assertions)
to enable logging of intermediate assertions to System.out.public abstract void poll()
AssertionError thrown will be swallowed unless this is the last attempt. Any other
kind of Exception will be thrown immediately. loudly() in this method.public static int times(int count)
new Poller( times(4) ) then it is clear what the 4 refers to, rather than just saying new Poller(4) which is ambiguous. 4 what? elephants?count - a numberpublic static TimeDuration every(long duration, TimeUnit unit)
TimeDuration object in a more natural way.
For an example of usage, see the documentation for Poller.duration - the durationunit - the TimeUnitTimeDuration.Copyright © 2013. All Rights Reserved.