Introduce a gpui2::test macro

Co-Authored-By: Conrad Irwin <conrad.irwin@gmail.com>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
Co-Authored-By: Kyle <kyle@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-10-25 20:54:05 +02:00
parent 7ec9cc08c7
commit 43d230cb2d
8 changed files with 366 additions and 53 deletions

View file

@ -17,6 +17,8 @@ mod styled;
mod subscription;
mod svg_renderer;
mod taffy;
#[cfg(any(test, feature = "test-support"))]
mod test;
mod text_system;
mod util;
mod view;
@ -48,6 +50,8 @@ pub use styled::*;
pub use subscription::*;
pub use svg_renderer::*;
pub use taffy::{AvailableSpace, LayoutId};
#[cfg(any(test, feature = "test-support"))]
pub use test::*;
pub use text_system::*;
pub use util::arc_cow::ArcCow;
pub use view::*;

View file

@ -75,6 +75,10 @@ impl TestDispatcher {
count: self.state.lock().random.gen_range(0..10),
}
}
pub fn run_until_parked(&self) {
while self.poll() {}
}
}
impl Clone for TestDispatcher {

51
crates/gpui2/src/test.rs Normal file
View file

@ -0,0 +1,51 @@
use crate::TestDispatcher;
use rand::prelude::*;
use std::{
env,
panic::{self, RefUnwindSafe},
};
pub fn run_test(
mut num_iterations: u64,
max_retries: usize,
test_fn: &mut (dyn RefUnwindSafe + Fn(TestDispatcher)),
on_fail_fn: Option<fn()>,
_fn_name: String, // todo!("re-enable fn_name")
) {
let starting_seed = env::var("SEED")
.map(|seed| seed.parse().expect("invalid SEED variable"))
.unwrap_or(0);
let is_randomized = num_iterations > 1;
if let Ok(iterations) = env::var("ITERATIONS") {
num_iterations = iterations.parse().expect("invalid ITERATIONS variable");
}
for seed in starting_seed..starting_seed + num_iterations {
let mut retry = 0;
loop {
if is_randomized {
eprintln!("seed = {seed}");
}
let result = panic::catch_unwind(|| {
let dispatcher = TestDispatcher::new(StdRng::seed_from_u64(seed));
test_fn(dispatcher);
});
match result {
Ok(_) => break,
Err(error) => {
if retry < max_retries {
println!("retrying: attempt {}", retry);
retry += 1;
} else {
if is_randomized {
eprintln!("failing seed: {}", seed);
}
on_fail_fn.map(|f| f());
panic::resume_unwind(error);
}
}
}
}
}
}