Skip to main content

TestScheduler

Struct TestScheduler 

Source
pub struct TestScheduler { /* private fields */ }

Implementations§

Source§

impl TestScheduler

Source

pub fn once<R>(f: impl AsyncFnOnce(Arc<TestScheduler>) -> R) -> R

Run a test once with default configuration (seed 0)

Source

pub fn many<R>( default_iterations: usize, f: impl AsyncFnMut(Arc<TestScheduler>) -> R, ) -> Vec<R>

Run a test multiple times with sequential seeds (0, 1, 2, …)

Source

pub fn new(config: TestSchedulerConfig) -> Self

Source

pub fn end_test(&self)

Source

pub fn clock(&self) -> Arc<TestClock> ⓘ

Source

pub fn rng(&self) -> SharedRng

Source

pub fn set_timeout_ticks(&self, timeout_ticks: RangeInclusive<usize>)

Source

pub fn allow_parking(&self)

Source

pub fn forbid_parking(&self)

Source

pub fn parking_allowed(&self) -> bool

Source

pub fn is_main_thread(&self) -> bool

Source

pub fn allocate_session_id(&self) -> SessionId

Source

pub fn foreground(self: &Arc<Self>) -> LocalExecutor

Create a local executor for this scheduler.

Source

pub fn background(self: &Arc<Self>) -> BackgroundExecutor

Create a background executor for this scheduler

Source

pub fn yield_random(&self) -> Yield ⓘ

Source

pub fn run(&self)

Source

pub fn run_with_clock_advancement(&self)

Source

pub fn tick(&self) -> bool

Execute one tick of the scheduler, processing expired timers and running at most one task. Returns true if any work was done.

This is the public interface for GPUI’s TestDispatcher to drive task execution.

Source

pub fn tick_background_only(&self) -> bool

Execute one tick, but only run background tasks (no foreground/session tasks). Returns true if any work was done.

Source

pub fn has_pending_tasks(&self) -> bool

Check if there are any pending tasks or timers that could run.

Source

pub fn pending_task_counts(&self) -> (usize, usize)

Returns counts of (foreground_tasks, background_tasks) currently queued. Foreground tasks are those with a session_id, background tasks have none.

Source

pub fn drain_tasks(&self)

Drops all runnable tasks from the scheduler.

This is used by the leak detector to ensure that all tasks have been dropped as tasks may keep entities alive otherwise. Why do we even have tasks left when tests finish you may ask. The reason for that is simple, the scheduler itself is the executor and it retains the scheduled runnables. A lot of tasks, including every foreground task contain an executor handle that keeps the test scheduler alive, causing a reference cycle, thus the need for this function right now.

Source

pub fn advance_clock_to_next_timer(&self) -> bool

Source

pub fn advance_clock(&self, duration: Duration)

Trait Implementations§

Source§

impl Scheduler for TestScheduler

Source§

fn block( &self, session_id: Option<SessionId>, future: Pin<&mut dyn Future<Output = ()>>, timeout: Option<Duration>, ) -> bool

Block until the given future completes, with an optional timeout. If the future is unable to make progress at any moment before the timeout and no other tasks or timers remain, we panic unless parking is allowed. If parking is allowed, we block up to the timeout or indefinitely if none is provided. This is to allow testing a mix of deterministic and non-deterministic async behavior, such as when interacting with I/O in an otherwise deterministic test.

Source§

fn spawn_dedicated( self: Arc<Self>, f: Box<dyn FnOnce(LocalExecutor) -> Pin<Box<dyn Future<Output = Box<dyn Any + Send + Sync>> + 'static>> + Send + 'static>, ) -> Task<Box<dyn Any + Send + Sync>> ⓘ

In the test world, dedicated work is just a fresh local session driven by the test scheduler’s run loop alongside everything else. No real thread is spawned, so determinism under TestScheduler::many is preserved.

Source§

fn schedule_local( &self, session_id: SessionId, runnable: Runnable<RunnableMeta>, )

Schedule a runnable on the local (session-pinned) queue for session_id. Runnables scheduled here run in order on whichever thread drains the session — the main thread for ordinary sessions, or a dedicated OS thread for sessions created via spawn_dedicated_thread.
Source§

fn schedule_background_with_priority( &self, runnable: Runnable<RunnableMeta>, priority: Priority, )

Schedule a background task with the given priority.
Source§

fn spawn_realtime(&self, f: Box<dyn FnOnce() + Send>)

Spawn a closure on a dedicated realtime thread for audio processing.
Source§

fn timer(&self, duration: Duration) -> Timer ⓘ

Source§

fn clock(&self) -> Arc<dyn Clock> ⓘ

Source§

fn as_test(&self) -> Option<&TestScheduler>

Source§

fn schedule_background(&self, runnable: Runnable<RunnableMeta>)

Schedule a background task with default (medium) priority.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V