public class VirtualTimeScheduler extends java.lang.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(java.time.Duration delayTime)
Moves the
VirtualTimeScheduler's clock forward by a specified amount of time. |
void |
advanceTimeTo(java.time.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 newly created
VirtualTimeScheduler to all Schedulers.Factory
factories ONLY if no VirtualTimeScheduler is currently set. |
static VirtualTimeScheduler |
getOrSet(boolean defer)
Assign a newly created
VirtualTimeScheduler to all Schedulers.Factory
factories ONLY if no VirtualTimeScheduler is currently set. |
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(java.util.concurrent.TimeUnit unit) |
static void |
reset()
Re-activate the global
Schedulers and potentially customized
Schedulers.Factory that were
active prior to last activation of VirtualTimeScheduler factories. |
Disposable |
schedule(java.lang.Runnable task) |
Disposable |
schedule(java.lang.Runnable task,
long delay,
java.util.concurrent.TimeUnit unit) |
Disposable |
schedulePeriodically(java.lang.Runnable task,
long initialDelay,
long period,
java.util.concurrent.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()). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdisposeGracefully, init, startpublic 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 ONLY if no VirtualTimeScheduler is currently set. In case of scheduler creation,
there is no deferring of time operations (see create(boolean).
Note that prior to replacing the factories, a snapshot
will be performed. Resetting the factory will restore said snapshot.
While this methods makes best effort to be thread safe, it is usually advised to perform such wide-impact setup serially and BEFORE all test code runs (setup/beforeAll/beforeClass...). The created Scheduler is returned.
public static VirtualTimeScheduler getOrSet(boolean defer)
VirtualTimeScheduler to all Schedulers.Factory
factories ONLY if no VirtualTimeScheduler is currently set. In case of scheduler creation,
there is opt-in deferring of time related operations (see create(boolean).
Note that prior to replacing the factories, a snapshot
will be performed. Resetting the factory will restore said snapshot.
While this methods makes best effort to be thread safe, it is usually advised to perform such wide-impact setup serially and BEFORE all test code runs (setup/beforeAll/beforeClass...). The created Scheduler is returned.
defer - true to defer all clock move operations until there are tasks in queue, if a scheduler is createdcreate(boolean)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.
Note also that prior to replacing the factories, a snapshot
will be performed. Resetting the factory will restore said snapshot.
While this methods makes best effort to be thread safe, it is usually advised to perform such wide-impact setup serially and BEFORE all test code runs (setup/beforeAll/beforeClass...). 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.
Note that prior to replacing the factories, a snapshot
will be performed. Resetting the factory will restore said snapshot.
While this methods makes best effort to be thread safe, it is usually advised to perform such wide-impact setup serially and BEFORE all test code runs (setup/beforeAll/beforeClass...).
scheduler - the VirtualTimeScheduler to use in factories.public static VirtualTimeScheduler get()
VirtualTimeScheduler assigned in SchedulersVirtualTimeScheduler assigned in Schedulersjava.lang.IllegalStateException - 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 and potentially customized
Schedulers.Factory that were
active prior to last activation of VirtualTimeScheduler factories. (ie the
last set or getOrSet).
While this methods makes best effort to be thread safe, it is usually advised to perform such wide-impact setup serially and AFTER all tested code has been run (teardown/afterAll/afterClass...).
public void advanceTime()
VirtualTimeScheduler's present time.public void advanceTimeBy(java.time.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(java.time.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(java.lang.Runnable task)
public Disposable schedule(java.lang.Runnable task, long delay, java.util.concurrent.TimeUnit unit)
public boolean isDisposed()
isDisposed in interface Disposablepublic void dispose()
dispose in interface Disposabledispose in interface Schedulerpublic Disposable schedulePeriodically(java.lang.Runnable task, long initialDelay, long period, java.util.concurrent.TimeUnit unit)
schedulePeriodically in interface Scheduler