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.Worker
Disposable.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 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(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(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() ). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
disposeGracefully, init, start
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 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 Schedulers
VirtualTimeScheduler
assigned in Schedulers
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(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 Scheduler
public Disposable schedule(Runnable task)
public Disposable schedule(Runnable task, long delay, TimeUnit unit)
public boolean isDisposed()
isDisposed
in interface Disposable
public void dispose()
dispose
in interface Disposable
dispose
in interface Scheduler
public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit)
schedulePeriodically
in interface Scheduler