public class VirtualTimeScheduler extends Object implements Scheduler
Scheduler that uses a virtual clock, allowing to manipulate time
(eg. in tests). Can replace the default reactor schedulers by using
the getOrSet() / set(VirtualTimeScheduler) methods.Scheduler.WorkerDisposable.Composite, Disposable.Swap| Modifier | Constructor and Description |
|---|---|
protected |
VirtualTimeScheduler(boolean defer) |
| Modifier and Type | Method and Description |
|---|---|
void |
advanceTime()
Triggers any tasks that have not yet been executed and that are scheduled to be
executed at or before this
VirtualTimeScheduler's present time. |
void |
advanceTimeBy(Duration delayTime)
Moves the
VirtualTimeScheduler's clock forward by a specified amount of time. |
void |
advanceTimeTo(Instant instant)
Moves the
VirtualTimeScheduler's clock to a particular moment in time. |
static VirtualTimeScheduler |
create()
Create a new
VirtualTimeScheduler without enabling it. |
static VirtualTimeScheduler |
create(boolean defer)
Create a new
VirtualTimeScheduler without enabling it. |
reactor.test.scheduler.VirtualTimeScheduler.VirtualTimeWorker |
createWorker() |
void |
dispose() |
static VirtualTimeScheduler |
get()
The current
VirtualTimeScheduler assigned in Schedulers |
static VirtualTimeScheduler |
getOrSet()
Assign a single newly created
VirtualTimeScheduler to all Schedulers.Factory
factories. |
static VirtualTimeScheduler |
getOrSet(VirtualTimeScheduler scheduler)
Assign an externally created
VirtualTimeScheduler to the relevant
Schedulers.Factory factories, depending on how it was created (see
create() and create()). |
long |
getScheduledTaskCount()
Get the number of scheduled tasks.
|
boolean |
isDisposed() |
static boolean |
isFactoryEnabled()
Return true if there is a
VirtualTimeScheduler currently used by the
Schedulers factory (ie it has been enabled),
false otherwise (ie it has been reset). |
long |
now(TimeUnit unit) |
static void |
reset()
Re-assign the default Reactor Core
Schedulers factories. |
Disposable |
schedule(Runnable task) |
Disposable |
schedule(Runnable task,
long delay,
TimeUnit unit) |
Disposable |
schedulePeriodically(Runnable task,
long initialDelay,
long period,
TimeUnit unit) |
static VirtualTimeScheduler |
set(VirtualTimeScheduler scheduler)
Assign an externally created
VirtualTimeScheduler to the relevant
Schedulers.Factory factories, depending on how it was created (see
create() and create()). |
public static VirtualTimeScheduler create()
VirtualTimeScheduler without enabling it. Call
getOrSet(VirtualTimeScheduler) to enable it on
Schedulers.Factory factories.VirtualTimeScheduler intended for timed-only
Schedulers factories.public static VirtualTimeScheduler create(boolean defer)
VirtualTimeScheduler without enabling it. Call
getOrSet(VirtualTimeScheduler) to enable it on
Schedulers.Factory factories.defer - true to defer all clock move operations until there are tasks in queueVirtualTimeScheduler intended for timed-only
Schedulers factories.public static VirtualTimeScheduler getOrSet()
VirtualTimeScheduler to all Schedulers.Factory
factories. While the method is thread safe, its usually advised to execute such
wide-impact BEFORE all tested code runs (setup etc). The created scheduler is returned.public static VirtualTimeScheduler getOrSet(VirtualTimeScheduler scheduler)
VirtualTimeScheduler to the relevant
Schedulers.Factory factories, depending on how it was created (see
create() and create()). Note that the returned scheduler
should always be captured and used going forward, as the provided scheduler can be
superseded by a matching scheduler that has already been enabled.
While the method is thread safe, it's usually advised to execute such wide-impact BEFORE all tested code runs (setup etc). The actual enabled Scheduler is returned.
scheduler - the VirtualTimeScheduler to use in factories.public static VirtualTimeScheduler set(VirtualTimeScheduler scheduler)
VirtualTimeScheduler to the relevant
Schedulers.Factory factories, depending on how it was created (see
create() and create()). Contrary to
getOrSet(VirtualTimeScheduler), the provided scheduler is always used, even
if a matching scheduler is currently enabled.
While the method is thread safe, it's usually advised to execute such wide-impact BEFORE all tested code runs (setup etc).
scheduler - the VirtualTimeScheduler to use in factories.public static VirtualTimeScheduler get()
VirtualTimeScheduler assigned in SchedulersVirtualTimeScheduler assigned in SchedulersIllegalStateException - if no VirtualTimeScheduler has been foundpublic static boolean isFactoryEnabled()
VirtualTimeScheduler currently used by the
Schedulers factory (ie it has been enabled),
false otherwise (ie it has been reset).public static void reset()
Schedulers factories.
While the method is thread safe, its usually advised to execute such wide-impact
AFTER all tested code has been run (teardown etc).public void advanceTime()
VirtualTimeScheduler's present time.public void advanceTimeBy(Duration delayTime)
VirtualTimeScheduler's clock forward by a specified amount of time.delayTime - the amount of time to move the VirtualTimeScheduler's clock forwardpublic void advanceTimeTo(Instant instant)
VirtualTimeScheduler's clock to a particular moment in time.instant - the point in time to move the VirtualTimeScheduler's
clock topublic long getScheduledTaskCount()
This count includes tasks that have already performed as well as ones scheduled in future. For periodical task, initial task is first scheduled and counted as one. Whenever subsequent repeat happens this count gets incremented for the one that is scheduled for the next run.
public reactor.test.scheduler.VirtualTimeScheduler.VirtualTimeWorker createWorker()
createWorker in interface Schedulerpublic Disposable schedule(Runnable task)
public Disposable schedule(Runnable task, long delay, TimeUnit unit)
public boolean isDisposed()
isDisposed in interface Disposablepublic void dispose()
dispose in interface Disposabledispose in interface Schedulerpublic Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit)
schedulePeriodically in interface Scheduler