Package io.reactivex.rxjava3.schedulers
Class TestScheduler
- java.lang.Object
-
- io.reactivex.rxjava3.core.Scheduler
-
- io.reactivex.rxjava3.schedulers.TestScheduler
-
public final class TestScheduler extends Scheduler
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 theRxJavaPlugins.onSchedule(Runnable)
hook. To enable this behavior, create aTestScheduler
viaTestScheduler(boolean)
orTestScheduler(long, TimeUnit, boolean)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
TestScheduler.TestWorker
(package private) static class
TestScheduler.TimedRunnable
-
Nested classes/interfaces inherited from class io.reactivex.rxjava3.core.Scheduler
Scheduler.Worker
-
-
Field Summary
Fields Modifier and Type Field Description (package private) long
counter
The per-scheduler global order counter.(package private) java.util.Queue<TestScheduler.TimedRunnable>
queue
The ordered queue for the runnable tasks.(package private) long
time
(package private) boolean
useOnScheduleHook
Use theRxJavaPlugins.onSchedule(Runnable)
hook when scheduling tasks.
-
Constructor Summary
Constructors Constructor Description TestScheduler()
Creates 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, java.util.concurrent.TimeUnit unit)
Creates a new TestScheduler with the specified initial virtual time.TestScheduler(long delayTime, java.util.concurrent.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
advanceTimeBy(long delayTime, java.util.concurrent.TimeUnit unit)
Moves the Scheduler's clock forward by a specified amount of time.void
advanceTimeTo(long delayTime, java.util.concurrent.TimeUnit unit)
Moves the Scheduler's clock to a particular moment in time.@NonNull Scheduler.Worker
createWorker()
Retrieves or creates a newScheduler.Worker
that represents sequential execution of actions.long
now(@NonNull java.util.concurrent.TimeUnit unit)
Returns the 'current time' of the Scheduler in the specified time unit.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.private void
triggerActions(long targetTimeInNanoseconds)
-
Methods inherited from class io.reactivex.rxjava3.core.Scheduler
clockDriftTolerance, scheduleDirect, scheduleDirect, schedulePeriodicallyDirect, shutdown, start, when
-
-
-
-
Field Detail
-
queue
final java.util.Queue<TestScheduler.TimedRunnable> queue
The ordered queue for the runnable tasks.
-
useOnScheduleHook
final boolean useOnScheduleHook
Use theRxJavaPlugins.onSchedule(Runnable)
hook when scheduling tasks.
-
counter
long counter
The per-scheduler global order counter.
-
time
volatile long time
-
-
Constructor Detail
-
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
public TestScheduler(long delayTime, java.util.concurrent.TimeUnit unit)
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 thatdelayTime
is expressed in
-
TestScheduler
public TestScheduler(long delayTime, java.util.concurrent.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.History: 3.0.10 - experimental
- Parameters:
delayTime
- the point in time to move the Scheduler's clock tounit
- the units of time thatdelayTime
is expressed inuseOnScheduleHook
- iftrue
, the tasks submitted to this TestScheduler is wrapped via theRxJavaPlugins.onSchedule(Runnable)
hook- Since:
- 3.1.0
-
-
Method Detail
-
now
public long now(@NonNull @NonNull java.util.concurrent.TimeUnit unit)
Description copied from class:Scheduler
Returns the 'current time' of the Scheduler in the specified time unit.
-
advanceTimeBy
public void advanceTimeBy(long delayTime, java.util.concurrent.TimeUnit unit)
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 thatdelayTime
is expressed in
-
advanceTimeTo
public void advanceTimeTo(long delayTime, java.util.concurrent.TimeUnit unit)
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 thatdelayTime
is 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.
-
triggerActions
private void triggerActions(long targetTimeInNanoseconds)
-
createWorker
@NonNull public @NonNull Scheduler.Worker createWorker()
Description copied from class:Scheduler
Retrieves or creates a newScheduler.Worker
that represents sequential execution of actions.When work is completed, the
Worker
instance should be released by callingDisposable.dispose()
to avoid potential resource leaks in the underlying task-execution scheme.Work on a
Scheduler.Worker
is guaranteed to be sequential and non-overlapping.- Specified by:
createWorker
in classScheduler
- Returns:
- a Worker representing a serial queue of actions to be executed
-
-