Extract a scheduler crate from GPUI to enable unified integration testing of client and server code (#37326)
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>
This commit is contained in:
co-authored by
Antonio Scandurra
parent
a05f86f97b
commit
1ae326432e
@@ -1464,7 +1464,7 @@ async fn test_random_worktree_operations_during_initial_scan(
|
||||
tree.as_local().unwrap().snapshot().check_invariants(true)
|
||||
});
|
||||
|
||||
if rng.gen_bool(0.6) {
|
||||
if rng.random_bool(0.6) {
|
||||
snapshots.push(worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot()));
|
||||
}
|
||||
}
|
||||
@@ -1551,7 +1551,7 @@ async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng)
|
||||
let mut snapshots = Vec::new();
|
||||
let mut mutations_len = operations;
|
||||
while mutations_len > 1 {
|
||||
if rng.gen_bool(0.2) {
|
||||
if rng.random_bool(0.2) {
|
||||
worktree
|
||||
.update(cx, |worktree, cx| {
|
||||
randomly_mutate_worktree(worktree, &mut rng, cx)
|
||||
@@ -1563,8 +1563,8 @@ async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng)
|
||||
}
|
||||
|
||||
let buffered_event_count = fs.as_fake().buffered_event_count();
|
||||
if buffered_event_count > 0 && rng.gen_bool(0.3) {
|
||||
let len = rng.gen_range(0..=buffered_event_count);
|
||||
if buffered_event_count > 0 && rng.random_bool(0.3) {
|
||||
let len = rng.random_range(0..=buffered_event_count);
|
||||
log::info!("flushing {} events", len);
|
||||
fs.as_fake().flush_events(len);
|
||||
} else {
|
||||
@@ -1573,7 +1573,7 @@ async fn test_random_worktree_changes(cx: &mut TestAppContext, mut rng: StdRng)
|
||||
}
|
||||
|
||||
cx.executor().run_until_parked();
|
||||
if rng.gen_bool(0.2) {
|
||||
if rng.random_bool(0.2) {
|
||||
log::info!("storing snapshot {}", snapshots.len());
|
||||
let snapshot = worktree.read_with(cx, |tree, _| tree.as_local().unwrap().snapshot());
|
||||
snapshots.push(snapshot);
|
||||
@@ -1701,7 +1701,7 @@ fn randomly_mutate_worktree(
|
||||
let snapshot = worktree.snapshot();
|
||||
let entry = snapshot.entries(false, 0).choose(rng).unwrap();
|
||||
|
||||
match rng.gen_range(0_u32..100) {
|
||||
match rng.random_range(0_u32..100) {
|
||||
0..=33 if entry.path.as_ref() != Path::new("") => {
|
||||
log::info!("deleting entry {:?} ({})", entry.path, entry.id.0);
|
||||
worktree.delete_entry(entry.id, false, cx).unwrap()
|
||||
@@ -1733,7 +1733,7 @@ fn randomly_mutate_worktree(
|
||||
_ => {
|
||||
if entry.is_dir() {
|
||||
let child_path = entry.path.join(random_filename(rng));
|
||||
let is_dir = rng.gen_bool(0.3);
|
||||
let is_dir = rng.random_bool(0.3);
|
||||
log::info!(
|
||||
"creating {} at {:?}",
|
||||
if is_dir { "dir" } else { "file" },
|
||||
@@ -1776,11 +1776,11 @@ async fn randomly_mutate_fs(
|
||||
}
|
||||
}
|
||||
|
||||
if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
|
||||
if (files.is_empty() && dirs.len() == 1) || rng.random_bool(insertion_probability) {
|
||||
let path = dirs.choose(rng).unwrap();
|
||||
let new_path = path.join(random_filename(rng));
|
||||
|
||||
if rng.r#gen() {
|
||||
if rng.random() {
|
||||
log::info!(
|
||||
"creating dir {:?}",
|
||||
new_path.strip_prefix(root_path).unwrap()
|
||||
@@ -1793,7 +1793,7 @@ async fn randomly_mutate_fs(
|
||||
);
|
||||
fs.create_file(&new_path, Default::default()).await.unwrap();
|
||||
}
|
||||
} else if rng.gen_bool(0.05) {
|
||||
} else if rng.random_bool(0.05) {
|
||||
let ignore_dir_path = dirs.choose(rng).unwrap();
|
||||
let ignore_path = ignore_dir_path.join(*GITIGNORE);
|
||||
|
||||
@@ -1808,11 +1808,11 @@ async fn randomly_mutate_fs(
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let files_to_ignore = {
|
||||
let len = rng.gen_range(0..=subfiles.len());
|
||||
let len = rng.random_range(0..=subfiles.len());
|
||||
subfiles.choose_multiple(rng, len)
|
||||
};
|
||||
let dirs_to_ignore = {
|
||||
let len = rng.gen_range(0..subdirs.len());
|
||||
let len = rng.random_range(0..subdirs.len());
|
||||
subdirs.choose_multiple(rng, len)
|
||||
};
|
||||
|
||||
@@ -1848,7 +1848,7 @@ async fn randomly_mutate_fs(
|
||||
file_path.into_iter().chain(dir_path).choose(rng).unwrap()
|
||||
};
|
||||
|
||||
let is_rename = rng.r#gen();
|
||||
let is_rename = rng.random();
|
||||
if is_rename {
|
||||
let new_path_parent = dirs
|
||||
.iter()
|
||||
@@ -1857,7 +1857,7 @@ async fn randomly_mutate_fs(
|
||||
.unwrap();
|
||||
|
||||
let overwrite_existing_dir =
|
||||
!old_path.starts_with(new_path_parent) && rng.gen_bool(0.3);
|
||||
!old_path.starts_with(new_path_parent) && rng.random_bool(0.3);
|
||||
let new_path = if overwrite_existing_dir {
|
||||
fs.remove_dir(
|
||||
new_path_parent,
|
||||
@@ -1919,7 +1919,7 @@ async fn randomly_mutate_fs(
|
||||
|
||||
fn random_filename(rng: &mut impl Rng) -> String {
|
||||
(0..6)
|
||||
.map(|_| rng.sample(rand::distributions::Alphanumeric))
|
||||
.map(|_| rng.sample(rand::distr::Alphanumeric))
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user