tickle

Types

Represents either a simulated or native scheduler, for scheduling send_after calls.

pub opaque type Scheduler

Represents either a simulated, or native Erlang timer, similar to process.Timer.

pub opaque type Timer

An error that can occur when waiting for a notification.

pub type WaitError {
  WaitTimedOut
  AlreadyWaiting
}

Constructors

  • WaitTimedOut

    notify was not called on time.

  • AlreadyWaiting

    Someone is already waiting for the same value on the scheduler.

Functions

pub fn advance(scheduler: Scheduler, amount: Int) -> Nil

Advances a simulated scheduler, or panics if the scheduler is not simulated. Calling this function concurrently leads to undefined behavior.

pub fn cancel_timer(timer: Timer) -> Cancelled

Cancel a given timer, causing it not to trigger if it has not done so already.

Should behave the same as process.cancel_timer

pub fn deferred_notify(
  scheduler: Scheduler,
  value: a,
  after: fn() -> b,
) -> b

Executes the given function, and then calls notify with the given arguments. Example:

use <- tickle.deferred_notify(scheduler, value)
use_value(value)
pub fn native_scheduler() -> Scheduler

Constructs a native scheduler, which will use standard Erlang scheduling.

pub fn notify(scheduler: Scheduler, value: a) -> Nil

Notifies any active waits on the given value on a simulated scheduler. Does nothing on a native scheduler.

pub fn send_after(
  scheduler: Scheduler,
  subject: Subject(a),
  delay: Int,
  message: a,
) -> Timer

Send a message over a channel after a specified number of milliseconds.

If the scheduler is a simulated scheduler, the message will not be sent until advance is called, even if the delay is zero. Otherwise should behave the same as process.send_after.

pub fn simulate(operation: fn(Scheduler) -> a) -> a

Runs an operation using a simulated scheduler, and tears down the setup after the operation.

pub fn wait_for_notify(
  scheduler: Scheduler,
  value: a,
  timeout: Int,
  trigger: fn() -> b,
) -> Result(b, WaitError)

Wait for another process to call notify on this scheduler with the given value, after running the given trigger. Only one wait can be active at once on the same value. Trying to wait for the same value from multiple processes is suspect to race conditions. Returns the value of the trigger, or an error. Will panic if called on a native scheduler.

Search Document