Class VirtualTimeScheduler
- All Implemented Interfaces:
Disposable,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.- Author:
- Stephane Maldini
-
Nested Class Summary
Nested classes/interfaces inherited from interface reactor.core.Disposable
Disposable.Composite, Disposable.SwapNested classes/interfaces inherited from interface reactor.core.scheduler.Scheduler
Scheduler.Worker -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidTriggers any tasks that have not yet been executed and that are scheduled to be executed at or before thisVirtualTimeScheduler's present time.voidadvanceTimeBy(Duration delayTime) Moves theVirtualTimeScheduler's clock forward by a specified amount of time.voidadvanceTimeTo(Instant instant) Moves theVirtualTimeScheduler's clock to a particular moment in time.static VirtualTimeSchedulercreate()Create a newVirtualTimeSchedulerwithout enabling it.static VirtualTimeSchedulercreate(boolean defer) Create a newVirtualTimeSchedulerwithout enabling it.reactor.test.scheduler.VirtualTimeScheduler.VirtualTimeWorkervoiddispose()static VirtualTimeSchedulerget()The currentVirtualTimeSchedulerassigned inSchedulersstatic VirtualTimeSchedulergetOrSet()Assign a newly createdVirtualTimeSchedulerto allSchedulers.Factoryfactories ONLY if noVirtualTimeScheduleris currently set.static VirtualTimeSchedulergetOrSet(boolean defer) Assign a newly createdVirtualTimeSchedulerto allSchedulers.Factoryfactories ONLY if noVirtualTimeScheduleris currently set.static VirtualTimeSchedulergetOrSet(VirtualTimeScheduler scheduler) Assign an externally createdVirtualTimeSchedulerto the relevantSchedulers.Factoryfactories, depending on how it was created (seecreate()andcreate()).longGet the number of scheduled tasks.booleanstatic booleanReturn true if there is aVirtualTimeSchedulercurrently used by theSchedulersfactory (ie it has beenenabled), false otherwise (ie it has beenreset).longstatic voidreset()Re-activate the globalSchedulersand potentially customizedSchedulers.Factorythat were active prior to last activation ofVirtualTimeSchedulerfactories.schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) static VirtualTimeSchedulerset(VirtualTimeScheduler scheduler) Assign an externally createdVirtualTimeSchedulerto the relevantSchedulers.Factoryfactories, depending on how it was created (seecreate()andcreate()).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface reactor.core.scheduler.Scheduler
disposeGracefully, init, start
-
Constructor Details
-
VirtualTimeScheduler
protected VirtualTimeScheduler(boolean defer)
-
-
Method Details
-
create
Create a newVirtualTimeSchedulerwithout enabling it. CallgetOrSet(VirtualTimeScheduler)to enable it onSchedulers.Factoryfactories.- Returns:
- a new
VirtualTimeSchedulerintended for timed-onlySchedulersfactories.
-
create
Create a newVirtualTimeSchedulerwithout enabling it. CallgetOrSet(VirtualTimeScheduler)to enable it onSchedulers.Factoryfactories.- Parameters:
defer- true to defer all clock move operations until there are tasks in queue- Returns:
- a new
VirtualTimeSchedulerintended for timed-onlySchedulersfactories.
-
getOrSet
Assign a newly createdVirtualTimeSchedulerto allSchedulers.Factoryfactories ONLY if noVirtualTimeScheduleris currently set. In case of scheduler creation, there is no deferring of time operations (seecreate(boolean). Note that prior to replacing the factories, asnapshotwill 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.
- Returns:
- the VirtualTimeScheduler that was created and set through the factory
-
getOrSet
Assign a newly createdVirtualTimeSchedulerto allSchedulers.Factoryfactories ONLY if noVirtualTimeScheduleris currently set. In case of scheduler creation, there is opt-in deferring of time related operations (seecreate(boolean). Note that prior to replacing the factories, asnapshotwill 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.
- Parameters:
defer- true to defer all clock move operations until there are tasks in queue, if a scheduler is created- Returns:
- the VirtualTimeScheduler that was created and set through the factory
- See Also:
-
getOrSet
Assign an externally createdVirtualTimeSchedulerto the relevantSchedulers.Factoryfactories, depending on how it was created (seecreate()andcreate()). 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, asnapshotwill 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.
- Parameters:
scheduler- theVirtualTimeSchedulerto use in factories.- Returns:
- the enabled VirtualTimeScheduler (can be different from the provided one)
-
set
Assign an externally createdVirtualTimeSchedulerto the relevantSchedulers.Factoryfactories, depending on how it was created (seecreate()andcreate()). Contrary togetOrSet(VirtualTimeScheduler), the provided scheduler is always used, even if a matching scheduler is currently enabled. Note that prior to replacing the factories, asnapshotwill 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...).
- Parameters:
scheduler- theVirtualTimeSchedulerto use in factories.- Returns:
- the enabled VirtualTimeScheduler (same as provided), for chaining
-
get
The currentVirtualTimeSchedulerassigned inSchedulers- Returns:
- current
VirtualTimeSchedulerassigned inSchedulers - Throws:
IllegalStateException- if noVirtualTimeSchedulerhas been found
-
isFactoryEnabled
public static boolean isFactoryEnabled()Return true if there is aVirtualTimeSchedulercurrently used by theSchedulersfactory (ie it has beenenabled), false otherwise (ie it has beenreset). -
reset
public static void reset()Re-activate the globalSchedulersand potentially customizedSchedulers.Factorythat were active prior to last activation ofVirtualTimeSchedulerfactories. (ie the lastsetorgetOrSet).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...).
-
advanceTime
public void advanceTime()Triggers any tasks that have not yet been executed and that are scheduled to be executed at or before thisVirtualTimeScheduler's present time. -
advanceTimeBy
Moves theVirtualTimeScheduler's clock forward by a specified amount of time.- Parameters:
delayTime- the amount of time to move theVirtualTimeScheduler's clock forward
-
advanceTimeTo
Moves theVirtualTimeScheduler's clock to a particular moment in time.- Parameters:
instant- the point in time to move theVirtualTimeScheduler's clock to
-
getScheduledTaskCount
public long getScheduledTaskCount()Get the number of scheduled tasks.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.
- Returns:
- number of tasks that have scheduled on this scheduler.
-
createWorker
public reactor.test.scheduler.VirtualTimeScheduler.VirtualTimeWorker createWorker()- Specified by:
createWorkerin interfaceScheduler
-
now
-
schedule
-
schedule
-
isDisposed
public boolean isDisposed()- Specified by:
isDisposedin interfaceDisposable
-
dispose
public void dispose()- Specified by:
disposein interfaceDisposable- Specified by:
disposein interfaceScheduler
-
schedulePeriodically
public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit) - Specified by:
schedulePeriodicallyin interfaceScheduler
-