Options
All
  • Public
  • Public/Protected
  • All
Menu

The TestScheduler is a Scheduler type meant for testing purposes, being capable of simulating asynchronous execution and the passage of time.

Example:

const s = new TestScheduler()

s.execute(() => { console.log("Hello, world!") })

// Triggers actual execution
s.tick()

// Simulating delayed execution
const task = s.scheduleOnce(Duration.seconds(10), () => {
  console.log("Hello, delayed!")
})

// We can cancel a delayed task if we want
task.cancel()

// Or we can execute it by moving the internal clock forward in time
s.tick(Duration.seconds(10))

Hierarchy

Index

Constructors

constructor

Properties

batchIndex

batchIndex: number = 0

Index of the current cycle, incremented automatically (modulo the batch size) when doing execution by means of Scheduler.executeBatched and the Scheduler is configured with ExecutionModel.batched.

When observed as being zero, it means an async boundary just happened.

executeBatched

executeBatched: function

Executes tasks in batches, according to the rules set by the given ExecutionModel.

The rules, depending on the chosen ExecutionModel:

  • if synchronous, then all tasks are executed with Scheduler.trampoline
  • if asynchronous, then all tasks are executed with Scheduler.executeAsync
  • if batched(n), then n tasks will be executed with Scheduler.trampoline and then the next execution will force an asynchronous boundary by means of Scheduler.executeAsync

Thus, in case of batched execution, an internal counter gets incremented to keep track of how many tasks where executed immediately (trampolined), a counter that's reset when reaching the threshold or when an executeAsync happens.

Type declaration

    • (runnable: function): void
    • Parameters

      • runnable: function
          • (): void
          • Returns void

      Returns void

executionModel

executionModel: ExecutionModel

The ExecutionModel is a specification of how run-loops and producers should behave in regards to executing tasks either synchronously or asynchronously.

Static global

global: DynamicRef<Scheduler> = DynamicRef.of(() => globalSchedulerRef)

Exposes a reusable GlobalScheduler reference by means of a DynamicRef, which allows for lexically scoped bindings to happen.

const myScheduler = new GlobalScheduler(false)

Scheduler.global.bind(myScheduler, () => {
  Scheduler.global.get() // myScheduler
})

Scheduler.global.get() // default instance

Methods

currentTimeMillis

  • currentTimeMillis(): number

executeAsync

  • executeAsync(runnable: function): void

hasTasksLeft

  • hasTasksLeft(): boolean
  • Returns true if there are any tasks left to execute, false otherwise.

    Returns boolean

reportFailure

  • reportFailure(e: Throwable): void

scheduleAtFixedRate

  • Schedules a periodic task that becomes enabled first after the given initial delay, and subsequently with the given period. Executions will commence after initialDelay then initialDelay + period, then initialDelay + 2 * period and so on.

    If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the scheduler. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

    For example the following schedules a message to be printed to standard output approximately every 10 seconds with an initial delay of 5 seconds:

    const task =
      s.scheduleAtFixedRate(Duration.seconds(5), Duration.seconds(10), () => {
        console.log("repeated message")
      })
    
      // later if you change your mind ...
      task.cancel()
    

    Parameters

    • initialDelay: number | Duration

      is the time to wait until the first execution happens

    • period: number | Duration

      is the time to wait between 2 successive executions of the task

    • runnable: function

      is the thunk to be executed

        • (): void
        • Returns void

    Returns ICancelable

    a cancelable that can be used to cancel the execution of this repeated task at any time.

scheduleOnce

scheduleWithFixedDelay

  • Schedules for execution a periodic task that is first executed after the given initial delay and subsequently with the given delay between the termination of one execution and the commencement of the next.

    For example the following schedules a message to be printed to standard output every 10 seconds with an initial delay of 5 seconds:

    const task =
      s.scheduleWithFixedDelay(Duration.seconds(5), Duration.seconds(10), () => {
        console.log("repeated message")
      })
    
    // later if you change your mind ...
    task.cancel()
    

    Parameters

    • initialDelay: number | Duration

      is the time to wait until the first execution happens

    • delay: number | Duration

      is the time to wait between 2 successive executions of the task

    • runnable: function

      is the thunk to be executed

        • (): void
        • Returns void

    Returns ICancelable

    a cancelable that can be used to cancel the execution of this repeated task at any time.

tick

  • tick(duration?: number | Duration): number
  • Executes the current batch of tasks that are pending, relative to currentTimeMillis.

    const s = new TestScheduler()
    
    // Immediate execution
    s.executeAsync(() => console.log("A"))
    s.executeAsync(() => console.log("B"))
    // Delay with 1 second from now
    s.scheduleOnce(Duration.seconds(1), () => console.log("C"))
    s.scheduleOnce(Duration.seconds(1), () => console.log("D"))
    // Delay with 2 seconds from now
    s.scheduleOnce(Duration.seconds(2), () => console.log("E"))
    s.scheduleOnce(Duration.seconds(2), () => console.log("F"))
    
    // Actual execution...
    
    // Prints A, B
    s.tick()
    // Prints C, D
    s.tick(Duration.seconds(1))
    // Prints E, F
    s.tick(Duration.seconds(1))
    

    Parameters

    • Optional duration: number | Duration

      is an optional timespan to user for incrementing currentTimeMillis, thus allowing the execution of tasks scheduled to execute with a delay.

    Returns number

    the number of executed tasks

tickOne

  • tickOne(): boolean
  • Executes the task that's at the top of the stack, in case we have a task to execute that doesn't require a jump in time.

    const ec = new TestScheduler()
    
    ec.execute(() => console.log("A"))
    ec.execute(() => console.log("B"))
    
    // Prints B
    ec.tickOne()
    // Prints A
    ec.tickOne()
    

    Returns boolean

trampoline

  • trampoline(runnable: function): void

triggeredFailures

  • triggeredFailures(): Array<any>
  • Returns a list of triggered errors, if any happened during the tick execution.

    Returns Array<any>

withExecutionModel

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc