Class Polling
- java.lang.Object
-
- org.apache.sling.testing.clients.util.poller.Polling
-
public class Polling extends Object implements Callable<Boolean>
Helper for repeating a call until it returns true, with timeout capabilities. Subclasses should override thecall()
method. Can be used with lambda expressions, using the constructorPolling(Callable c)
.- Since:
- 1.1.0
-
-
Field Summary
Fields Modifier and Type Field Description protected Callable<Boolean>
c
Optional object to be used by the default implementation of call()protected List<Exception>
exceptions
List of all the exceptions thrown by call(), to be used for loggingprotected Exception
lastException
Holder for the last exception thrown by call(), to be used for loggingprotected long
waited
Counter for total waiting time
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Boolean
call()
Method to be called bypoll(long timeout, long delay)
, potentially multiple times, until it returns true or timeout is reached.
Subclasses can override it to change the check accordingly.List<Exception>
getExceptions()
Return the list of all exceptions while pollingException
getLastException()
Return the last exception while polling or {null}long
getWaited()
protected String
message()
Returns the string to be used in theTimeoutException
, if needed.void
poll(long timeout, long delay)
Tries to executecall()
until it returns true or untiltimeout
is reached.
-
-
-
Field Detail
-
c
protected final Callable<Boolean> c
Optional object to be used by the default implementation of call()
-
lastException
protected Exception lastException
Holder for the last exception thrown by call(), to be used for logging
-
exceptions
protected List<Exception> exceptions
List of all the exceptions thrown by call(), to be used for logging
-
waited
protected long waited
Counter for total waiting time
-
-
Constructor Detail
-
Polling
public Polling()
Default constructor to be used in subclasses that override thecall()
method. Should not be used directly onPolling
instances, but only on extended classes. If used directly to get aPolling
instance, executingpoll(long timeout, long delay)
will be equivalent toThread.sleep(timeout)
-
-
Method Detail
-
call
public Boolean call() throws Exception
Method to be called by
poll(long timeout, long delay)
, potentially multiple times, until it returns true or timeout is reached.
Subclasses can override it to change the check accordingly. The method should return true only when the call was successful.
It can return false or throw anyException
to make the poller try again later.The default implementation delegates the call to the
Callable c
instance.
-
poll
public void poll(long timeout, long delay) throws TimeoutException, InterruptedException
Tries to execute
call()
until it returns true or untiltimeout
is reached. Between retries, it waits usingThread.sleep(delay)
. It means the retry is not at a fixed pace, but depends on the execution time of the call itself.The method guarantees that the call() will be executed at least once. If the timeout is 0 or less, then call() will be executed exactly once.
The timeout is adjusted using
TimeoutsProvider
so the final value can be changed using the system property: "sling.testing.timeout.multiplier"- Parameters:
timeout
- max total execution time, in millisecondsdelay
- time to wait between calls, in milliseconds- Throws:
TimeoutException
- iftimeout
was reachedInterruptedException
- if the thread was interrupted while sleeping; caller should throw it further
-
getWaited
public long getWaited()
-
message
protected String message()
Returns the string to be used in theTimeoutException
, if needed. The string is passed toString.format(message(), timeout, delay)
, so it can be a format including%1$d
and%2$d
. The fieldlastException
is also available for logging- Returns:
- the format string
-
getLastException
public Exception getLastException()
Return the last exception while polling or {null}- Returns:
- the last exception (can be null)
-
-