Read env vars in TestScheduler::many (#38897)

This allows ITERATIONS and SEED environment variables to override the
hard coded values during testing.

cc @ConradIrwin @as-cii 

Release Notes:

- N/A

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Nathan Sobo 2025-09-25 13:03:56 -06:00 committed by GitHub
parent de1de25712
commit ae036f8ead
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 15 deletions

View file

@ -41,14 +41,26 @@ impl TestScheduler {
} }
/// Run a test multiple times with sequential seeds (0, 1, 2, ...) /// Run a test multiple times with sequential seeds (0, 1, 2, ...)
pub fn many<R>(iterations: usize, mut f: impl AsyncFnMut(Arc<TestScheduler>) -> R) -> Vec<R> { pub fn many<R>(
(0..iterations as u64) default_iterations: usize,
mut f: impl AsyncFnMut(Arc<TestScheduler>) -> R,
) -> Vec<R> {
let num_iterations = std::env::var("ITERATIONS")
.map(|iterations| iterations.parse().unwrap())
.unwrap_or(default_iterations);
let seed = std::env::var("SEED")
.map(|seed| seed.parse().unwrap())
.unwrap_or(0);
(seed..num_iterations as u64)
.map(|seed| { .map(|seed| {
let mut unwind_safe_f = AssertUnwindSafe(&mut f); let mut unwind_safe_f = AssertUnwindSafe(&mut f);
eprintln!("Running seed: {seed}");
match panic::catch_unwind(move || Self::with_seed(seed, &mut *unwind_safe_f)) { match panic::catch_unwind(move || Self::with_seed(seed, &mut *unwind_safe_f)) {
Ok(result) => result, Ok(result) => result,
Err(error) => { Err(error) => {
eprintln!("Failing Seed: {seed}"); eprintln!("\x1b[31mFailing Seed: {seed}\x1b[0m");
panic::resume_unwind(error); panic::resume_unwind(error);
} }
} }
@ -56,8 +68,7 @@ impl TestScheduler {
.collect() .collect()
} }
/// Run a test once with a specific seed fn with_seed<R>(seed: u64, f: impl AsyncFnOnce(Arc<TestScheduler>) -> R) -> R {
pub fn with_seed<R>(seed: u64, f: impl AsyncFnOnce(Arc<TestScheduler>) -> R) -> R {
let scheduler = Arc::new(TestScheduler::new(TestSchedulerConfig::with_seed(seed))); let scheduler = Arc::new(TestScheduler::new(TestSchedulerConfig::with_seed(seed)));
let future = f(scheduler.clone()); let future = f(scheduler.clone());
let result = scheduler.foreground().block_on(future); let result = scheduler.foreground().block_on(future);

View file

@ -288,16 +288,6 @@ fn test_helper_methods() {
background.spawn(async { 10 }).await background.spawn(async { 10 }).await
}); });
assert_eq!(results, vec![10, 10, 10]); assert_eq!(results, vec![10, 10, 10]);
// Test the with_seed method
let result = TestScheduler::with_seed(123, async |scheduler: Arc<TestScheduler>| {
let background = scheduler.background();
// Spawn a background task and wait for its result
let task = background.spawn(async { 99 });
task.await
});
assert_eq!(result, 99);
} }
#[test] #[test]