Class TestScheduler
java.lang.Object
io.reactivex.rxjava4.core.Scheduler
io.reactivex.rxjava4.schedulers.TestScheduler
A special, non thread-safe scheduler for testing operators that require
a scheduler without introducing real concurrency and allows manually advancing
a virtual time.
By default, the tasks submitted via the various schedule methods are not
wrapped by the RxJavaPlugins.onSchedule(Runnable) hook. To enable this behavior,
create a TestScheduler via TestScheduler(boolean) or TestScheduler(long, TimeUnit, boolean).
-
Nested Class Summary
Nested classes/interfaces inherited from class Scheduler
Scheduler.WorkerModifier and TypeClassDescriptionstatic classRepresents an isolated, sequential worker of a parent Scheduler for executingRunnabletasks on an underlying task-execution scheme (such as custom Threads, event loop,Executoror Actor system). -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new TestScheduler with initial virtual time of zero.TestScheduler(boolean useOnScheduleHook) Creates a new TestScheduler with the option to use theRxJavaPlugins.onSchedule(Runnable)hook when scheduling tasks.TestScheduler(long delayTime, TimeUnit unit) Creates a new TestScheduler with the specified initial virtual time.TestScheduler(long delayTime, TimeUnit unit, boolean useOnScheduleHook) Creates a new TestScheduler with the specified initial virtual time and with the option to use theRxJavaPlugins.onSchedule(Runnable)hook when scheduling tasks. -
Method Summary
Modifier and TypeMethodDescriptionvoidadvanceTimeBy(long delayTime, TimeUnit unit) Moves the Scheduler's clock forward by a specified amount of time.voidadvanceTimeTo(long delayTime, TimeUnit unit) Moves the Scheduler's clock to a particular moment in time.Retrieves or creates a newScheduler.Workerthat represents sequential execution of actions.longReturns the 'current time' of the Scheduler in the specified time unit.voidTriggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this Scheduler's present time.Methods inherited from class Scheduler
clockDriftTolerance, scheduleDirect, scheduleDirect, schedulePeriodicallyDirect, shutdown, start, toExecutorService, toExecutorService, whenModifier and TypeMethodDescriptionstatic longReturns the clock drift tolerance in nanoseconds.Schedules the given task on this Scheduler without any time delay.scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) Schedules the execution of the given task with the given time delay.schedulePeriodicallyDirect(@NonNull Runnable run, long initialDelay, long period, @NonNull TimeUnit unit) Schedules a periodic execution of the given task with the given initial time delay and repeat period.voidshutdown()Instructs the Scheduler instance to stop threads, stop accepting tasks on any outstandingScheduler.Workerinstances and clean up any associated resources with this Scheduler.voidstart()Allows the Scheduler instance to start threads and accept tasks on them.Turn this Scheduler into an ExecutorService implementation using its various *Direct() methods instead of workers.toExecutorService(boolean useWorker) Turn this Scheduler into an ExecutorService implementation using its various *Direct() methods or worker methods, depending on the parameter.<S extends Scheduler & Disposable>
Swhen(@NonNull Function<Flowable<Flowable<Completable>>, Completable> combine) Allows the use of operators for controlling the timing around when actions scheduled on workers are actually done.
-
Constructor Details
-
TestScheduler
public TestScheduler()Creates a new TestScheduler with initial virtual time of zero. -
TestScheduler
public TestScheduler(boolean useOnScheduleHook) Creates a new TestScheduler with the option to use theRxJavaPlugins.onSchedule(Runnable)hook when scheduling tasks.History: 3.0.10 - experimental
- Parameters:
useOnScheduleHook- iftrue, the tasks submitted to this TestScheduler is wrapped via theRxJavaPlugins.onSchedule(Runnable)hook- Since:
- 3.1.0
-
TestScheduler
Creates a new TestScheduler with the specified initial virtual time.- Parameters:
delayTime- the point in time to move the Scheduler's clock tounit- the units of time thatdelayTimeis expressed in
-
TestScheduler
Creates a new TestScheduler with the specified initial virtual time and with the option to use theRxJavaPlugins.onSchedule(Runnable)hook when scheduling tasks.History: 3.0.10 - experimental
- Parameters:
delayTime- the point in time to move the Scheduler's clock tounit- the units of time thatdelayTimeis expressed inuseOnScheduleHook- iftrue, the tasks submitted to this TestScheduler is wrapped via theRxJavaPlugins.onSchedule(Runnable)hook- Since:
- 3.1.0
-
-
Method Details
-
now
-
advanceTimeBy
Moves the Scheduler's clock forward by a specified amount of time.- Parameters:
delayTime- the amount of time to move the Scheduler's clock forwardunit- the units of time thatdelayTimeis expressed in
-
advanceTimeTo
Moves the Scheduler's clock to a particular moment in time.- Parameters:
delayTime- the point in time to move the Scheduler's clock tounit- the units of time thatdelayTimeis expressed in
-
triggerActions
public void triggerActions()Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this Scheduler's present time. -
createWorker
Description copied from class:SchedulerRetrieves or creates a newScheduler.Workerthat represents sequential execution of actions.When work is completed, the
Workerinstance should be released by callingDisposable.dispose()to avoid potential resource leaks in the underlying task-execution scheme.Work on a
Scheduler.Workeris guaranteed to be sequential and non-overlapping.- Specified by:
createWorkerin classScheduler- Returns:
- a Worker representing a serial queue of actions to be executed
-