Extracts and cleans up GPUI's scheduler code into a new `scheduler` crate, making it pluggable by external runtimes. This will enable deterministic integration testing with cloud components by providing a unified test scheduler across Zed and backend code. In Zed, it will replace the existing GPUI scheduler for consistent async task management across platforms. ## Changes - **Core Implementation**: `TestScheduler` with seed-based randomization, session tracking (`SessionId`), and foreground/background task separation for reproducible testing. - **Executors**: `ForegroundExecutor` (!Send, thread-local) and `BackgroundExecutor` (Send, with blocking/timeout support) as GPUI-compatible wrappers. - **Clock and Timer**: Controllable `TestClock` and future-based `Timer` for time-sensitive tests. - **Testing APIs**: `once()`, `with_seed()`, and `many()` methods for configurable test runs. - **Dependencies**: Added `async-task`, `chrono`, `futures`, etc., with updates to `Cargo.toml` and lock file. ## Benefits - **Integration Testing**: Facilitates reliable async tests involving cloud sessions, reducing flakiness via deterministic execution. - **Pluggability**: Trait-based design (`Scheduler`) allows easy integration into non-GPUI runtimes while maintaining GPUI compatibility. - **Cleanup**: Refactors GPUI scheduler logic for clarity, correctness (no `unwrap()`, proper error handling), and extensibility. Follows Rust guidelines; run `./script/clippy` for verification. - [x] Define and test a core scheduler that we think can power our cloud code and GPUI - [ ] Replace GPUI's scheduler Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com>
35 lines
707 B
Rust
35 lines
707 B
Rust
use chrono::{DateTime, Duration, Utc};
|
|
use parking_lot::Mutex;
|
|
|
|
pub trait Clock {
|
|
fn now(&self) -> DateTime<Utc>;
|
|
}
|
|
|
|
pub struct TestClock {
|
|
now: Mutex<DateTime<Utc>>,
|
|
}
|
|
|
|
impl TestClock {
|
|
pub fn new() -> Self {
|
|
const START_TIME: &str = "2025-07-01T23:59:58-00:00";
|
|
let now = DateTime::parse_from_rfc3339(START_TIME).unwrap().to_utc();
|
|
Self {
|
|
now: Mutex::new(now),
|
|
}
|
|
}
|
|
|
|
pub fn set_now(&self, now: DateTime<Utc>) {
|
|
*self.now.lock() = now;
|
|
}
|
|
|
|
pub fn advance(&self, duration: Duration) {
|
|
*self.now.lock() += duration;
|
|
}
|
|
}
|
|
|
|
impl Clock for TestClock {
|
|
fn now(&self) -> DateTime<Utc> {
|
|
*self.now.lock()
|
|
}
|
|
}
|