Class VirtualTimeScheduler

java.lang.Object
reactor.test.scheduler.VirtualTimeScheduler
All Implemented Interfaces:
Disposable, Scheduler

public class VirtualTimeScheduler extends Object implements Scheduler
A 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
  • Constructor Details

    • VirtualTimeScheduler

      protected VirtualTimeScheduler(boolean defer)
  • Method Details

    • create

      public static VirtualTimeScheduler create()
      Create a new VirtualTimeScheduler without enabling it. Call getOrSet(VirtualTimeScheduler) to enable it on Schedulers.Factory factories.
      Returns:
      a new VirtualTimeScheduler intended for timed-only Schedulers factories.
    • create

      public static VirtualTimeScheduler create(boolean defer)
      Create a new VirtualTimeScheduler without enabling it. Call getOrSet(VirtualTimeScheduler) to enable it on Schedulers.Factory factories.
      Parameters:
      defer - true to defer all clock move operations until there are tasks in queue
      Returns:
      a new VirtualTimeScheduler intended for timed-only Schedulers factories.
    • getOrSet

      public static VirtualTimeScheduler getOrSet()
      Assign a newly created 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.

      Returns:
      the VirtualTimeScheduler that was created and set through the factory
    • getOrSet

      public static VirtualTimeScheduler getOrSet(boolean defer)
      Assign a newly created 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.

      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

      public 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()). 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.

      Parameters:
      scheduler - the VirtualTimeScheduler to use in factories.
      Returns:
      the enabled VirtualTimeScheduler (can be different from the provided one)
    • set

      public 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()). 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...).

      Parameters:
      scheduler - the VirtualTimeScheduler to use in factories.
      Returns:
      the enabled VirtualTimeScheduler (same as provided), for chaining
    • get

      public static VirtualTimeScheduler get()
      The current VirtualTimeScheduler assigned in Schedulers
      Returns:
      current VirtualTimeScheduler assigned in Schedulers
      Throws:
      IllegalStateException - if no VirtualTimeScheduler has been found
    • isFactoryEnabled

      public 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).
    • reset

      public static void reset()
      Re-activate the global 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...).

    • advanceTime

      public 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.
    • advanceTimeBy

      public void advanceTimeBy(Duration delayTime)
      Moves the VirtualTimeScheduler's clock forward by a specified amount of time.
      Parameters:
      delayTime - the amount of time to move the VirtualTimeScheduler's clock forward
    • advanceTimeTo

      public void advanceTimeTo(Instant instant)
      Moves the VirtualTimeScheduler's clock to a particular moment in time.
      Parameters:
      instant - the point in time to move the VirtualTimeScheduler'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:
      createWorker in interface Scheduler
    • now

      public long now(TimeUnit unit)
      Specified by:
      now in interface Scheduler
    • schedule

      public Disposable schedule(Runnable task)
      Specified by:
      schedule in interface Scheduler
    • schedule

      public Disposable schedule(Runnable task, long delay, TimeUnit unit)
      Specified by:
      schedule in interface Scheduler
    • isDisposed

      public boolean isDisposed()
      Specified by:
      isDisposed in interface Disposable
    • dispose

      public void dispose()
      Specified by:
      dispose in interface Disposable
      Specified by:
      dispose in interface Scheduler
    • schedulePeriodically

      public Disposable schedulePeriodically(Runnable task, long initialDelay, long period, TimeUnit unit)
      Specified by:
      schedulePeriodically in interface Scheduler