kache 1.2.1

Compiler cache for Rust, C/C++ and CUDA, with S3, GCS, shared-filesystem and OCI remotes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! `kache test-runner`: an opt-in Cargo target runner that paces test
//! binaries through the machine-wide scheduler.
//!
//! A project opts in with
//! `[target.'cfg(all())'] runner = ["kache", "test-runner"]` in its
//! `.cargo/config.toml`, and Cargo runs every test binary as
//! `kache test-runner <binary> <args>`.
//!
//! A test binary waits for slots above the compile reserve (see
//! [`scheduler::reserve_for`]) and holds them until it exits. Its children
//! inherit the lease marker in [`scheduler::TEST_LEASE_ENV`], so compiles and
//! test runners nested inside it take no slots of their own. Everything else
//! runs unchanged: binaries without Cargo's metadata hash (`cargo run`,
//! doctests), listing, benches, and any run with the scheduler off.
//!
//! `kache init` can instead export `CARGO_TARGET_<HOST>_RUNNER` for every
//! project. Cargo ranks that above a project's own runner, so [`delegate`]
//! finds the runner the project set and the test binary still runs under it.
//!
//! The runner never writes to stdout: test harnesses and nextest read it.

use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;

use crate::scheduler::{self, TestWant};

mod delegate;
#[cfg(unix)]
pub(crate) use delegate::runner_var;

/// Wait budget under nextest, which counts the wait toward its own
/// `slow-timeout` and `terminate-after`.
const NEXTEST_WAIT: Duration = Duration::from_secs(120);

/// Exit code when the test binary cannot be run: the code a failed test run
/// gives Cargo.
const RUN_FAILED: i32 = 101;

/// The environment the runner reads, captured once at startup.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TestEnv {
    /// `NEXTEST_EXECUTION_MODE`, set whenever nextest runs the binary.
    pub(crate) nextest_mode: Option<String>,
    /// `RUST_TEST_THREADS`.
    pub(crate) rust_test_threads: Option<String>,
    /// [`scheduler::TEST_LEASE_ENV`] inherited from a test above this one.
    pub(crate) lease: Option<PathBuf>,
    /// `CARGO_PKG_NAME` of the package the binary belongs to.
    pub(crate) pkg_name: Option<String>,
}

impl TestEnv {
    fn capture() -> Self {
        Self {
            nextest_mode: std::env::var("NEXTEST_EXECUTION_MODE").ok(),
            rust_test_threads: std::env::var("RUST_TEST_THREADS").ok(),
            lease: std::env::var_os(scheduler::TEST_LEASE_ENV).map(PathBuf::from),
            pkg_name: std::env::var("CARGO_PKG_NAME").ok(),
        }
    }
}

/// What the runner does with a test binary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Plan {
    /// Run it unchanged.
    Exec,
    /// Take a test lease first. `key` names the RSS history: the binary's,
    /// or the test's under nextest's process-per-test mode.
    Schedule { key: String, want: TestWant },
}

/// Run `args` (the test binary, then its arguments) and return the exit code
/// for the runner. Returns only when the binary could not be run or has
/// exited; a binary killed by a signal kills the runner with it.
pub fn run(args: &[OsString]) -> i32 {
    if args.is_empty() {
        eprintln!("kache test-runner: expected a test binary to run");
        return RUN_FAILED;
    }
    let env = TestEnv::capture();
    let config = crate::config::Config::load().ok();
    let cache_dir = config.as_ref().map(|config| config.cache_dir.as_path());
    let test_args = args[1..].iter().map(OsString::as_os_str);
    let target_use = match cache_dir
        .map(|dir| crate::target_use::shared_for(dir, test_args))
        .transpose()
    {
        Ok(lease) => lease,
        Err(error) => {
            eprintln!("kache test-runner: cannot protect target directory: {error}");
            return RUN_FAILED;
        }
    };
    let scheduler_dir = config
        .as_ref()
        .filter(|config| config.scheduler)
        .map(|config| config.cache_dir.as_path());
    let prefix = match delegate::prefix() {
        Ok(prefix) => prefix,
        Err(error) => {
            eprintln!("kache test-runner: {error}");
            return RUN_FAILED;
        }
    };
    let marker = (!prefix.is_empty()).then(|| delegate::delegate_marker(&prefix));
    let command = [prefix, args.to_vec()].concat();
    match (scheduler_dir, plan(args, &env, scheduler_dir)) {
        (Some(cache_dir), Plan::Schedule { key, want }) => {
            let _target_use = target_use;
            run_scheduled(&command, marker, cache_dir, &env, &key, want)
        }
        _ => match target_use {
            Some(lease) => run_protected(&command, marker, lease),
            None => exec(&command, marker),
        },
    }
}

/// Keep the target lease in this process until an unscheduled test exits.
fn run_protected(
    args: &[OsString],
    marker: Option<String>,
    _lease: crate::target_use::Lease,
) -> i32 {
    let mut command = Command::new(&args[0]);
    command.args(&args[1..]);
    set_marker(&mut command, marker);
    let mut child = match command.spawn() {
        Ok(child) => child,
        Err(error) => return run_failed(&args[0], &error),
    };
    signals::forward_to(child.id());
    match child.wait() {
        Ok(status) => finish(exit_disposition(status.code(), signals::of(&status))),
        Err(error) => run_failed(&args[0], &error),
    }
}

/// Decide whether to schedule `args`. `cache_dir` is `None` when the
/// scheduler is off or the config could not be loaded.
pub(crate) fn plan(args: &[OsString], env: &TestEnv, cache_dir: Option<&Path>) -> Plan {
    let Some(cache_dir) = cache_dir else {
        return Plan::Exec;
    };
    let Some(stem) = args.first().and_then(|program| artifact_stem(program)) else {
        return Plan::Exec;
    };
    let rest = &args[1..];
    if rest.iter().any(|arg| arg == "--list" || arg == "--bench") {
        return Plan::Exec;
    }
    let root = scheduler::scheduler_root(cache_dir);
    if scheduler::lease_covers(env.lease.as_deref(), &root) {
        return Plan::Exec;
    }
    let per_test = env.nextest_mode.as_deref() == Some("process-per-test");
    let pkg = env.pkg_name.as_deref().unwrap_or_default();
    // A process that runs one test keeps that test's history. A sample
    // shared by the binary would give every test the last one's memory.
    let key = match if per_test { exact_test(rest) } else { None } {
        Some(test) => format!("test:{pkg}:{stem}:{test}"),
        None => format!("test:{pkg}:{stem}"),
    };
    let floor = scheduler::test_floor(cache_dir, &key);
    let want = if per_test {
        TestWant::Fixed(floor)
    } else if let Some(threads) = declared_threads(rest, env.rust_test_threads.as_deref()) {
        TestWant::Fixed(threads.max(floor))
    } else {
        TestWant::Elastic(floor)
    };
    Plan::Schedule { key, want }
}

/// The name of a Cargo test binary without its `-<16 hex>` metadata suffix
/// or `.exe`. `None` for any other name: `cargo run` binaries and doctests
/// (`rust_out`) carry no hash.
pub(crate) fn artifact_stem(program: &OsStr) -> Option<&str> {
    let name = Path::new(program).file_name()?.to_str()?;
    let name = name.strip_suffix(".exe").unwrap_or(name);
    let (stem, hash) = name.rsplit_once('-')?;
    let hashed = hash.len() == 16 && hash.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'));
    hashed.then_some(stem)
}

/// The one test nextest runs in this process: the argument after `--exact`.
pub(crate) fn exact_test(args: &[OsString]) -> Option<&str> {
    let at = args.iter().position(|arg| arg == "--exact")?;
    args.get(at + 1)?
        .to_str()
        .filter(|test| !test.starts_with('-'))
}

/// Threads the binary was told to use: the first valid `--test-threads N`
/// or `--test-threads=N` among `args`, else a valid `RUST_TEST_THREADS`.
/// Zero, garbage and a missing value are not valid.
pub(crate) fn declared_threads(args: &[OsString], env_threads: Option<&str>) -> Option<u32> {
    let mut args = args.iter();
    while let Some(arg) = args.next() {
        let value = if arg == "--test-threads" {
            args.next().and_then(|value| value.to_str())
        } else {
            arg.to_str()
                .and_then(|arg| arg.strip_prefix("--test-threads="))
        };
        if let Some(threads) = value.and_then(positive) {
            return Some(threads);
        }
    }
    env_threads.and_then(positive)
}

fn positive(value: &str) -> Option<u32> {
    value.parse().ok().filter(|threads| *threads > 0)
}

/// How long to wait for a lease before running without one.
pub(crate) fn wait_budget(env: &TestEnv) -> Duration {
    if env.nextest_mode.is_some() {
        NEXTEST_WAIT
    } else {
        scheduler::WAIT_TIMEOUT
    }
}

/// Variables a leased test binary gets: the lease marker, and for an
/// elastic want the number of slots held as libtest's thread count.
/// Without that, libtest starts a thread per CPU and the lease bounds
/// nothing.
fn lease_env(want: TestWant, marker: &Path, held: u32) -> Vec<(&'static str, OsString)> {
    let mut vars = vec![(scheduler::TEST_LEASE_ENV, marker.as_os_str().to_owned())];
    if let TestWant::Elastic(_) = want {
        vars.push(("RUST_TEST_THREADS", held.to_string().into()));
    }
    vars
}

/// How the runner ends once the test binary has exited.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Disposition {
    /// Exit with this code.
    Exit(i32),
    /// Die of `signal` as the binary did, or exit with `fallback` when
    /// raising it returns.
    Signal { signal: i32, fallback: i32 },
}

/// Map the binary's exit code or terminating signal to the runner's end.
pub(crate) fn exit_disposition(code: Option<i32>, signal: Option<i32>) -> Disposition {
    match (code, signal) {
        (Some(code), _) => Disposition::Exit(code),
        (None, Some(signal)) => Disposition::Signal {
            signal,
            fallback: 128 + signal,
        },
        (None, None) => Disposition::Exit(RUN_FAILED),
    }
}

/// Set [`delegate::DELEGATE_ENV`] for a delegated runner, and clear one
/// this runner inherited, so a test that starts Cargo again gets its own
/// project runner.
fn set_marker(command: &mut Command, marker: Option<String>) {
    match marker {
        Some(marker) => command.env(delegate::DELEGATE_ENV, marker),
        None => command.env_remove(delegate::DELEGATE_ENV),
    };
}

/// Run the binary as it is. On Unix the runner becomes the binary.
/// `marker` is set as [`delegate::DELEGATE_ENV`] when a project runner
/// comes first.
fn exec(args: &[OsString], marker: Option<String>) -> i32 {
    let mut command = Command::new(&args[0]);
    command.args(&args[1..]);
    set_marker(&mut command, marker);
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        let error = command.exec();
        run_failed(&args[0], &error)
    }
    #[cfg(not(unix))]
    {
        match command.status() {
            Ok(status) => status.code().unwrap_or(RUN_FAILED),
            Err(error) => run_failed(&args[0], &error),
        }
    }
}

/// Run the binary under a test lease, then record its peak RSS.
fn run_scheduled(
    args: &[OsString],
    marker: Option<String>,
    cache_dir: &Path,
    env: &TestEnv,
    key: &str,
    want: TestWant,
) -> i32 {
    let lease = scheduler::acquire_test_lease(
        cache_dir,
        scheduler::default_pool_size(),
        want,
        wait_budget(env),
    );
    let mut command = Command::new(&args[0]);
    command.args(&args[1..]);
    set_marker(&mut command, marker);
    if let Some(lease) = &lease {
        command.envs(lease_env(want, lease.marker_path(), lease.held()));
    }
    let mut child = match command.spawn() {
        Ok(child) => child,
        Err(error) => return run_failed(&args[0], &error),
    };
    signals::forward_to(child.id());
    let status = child.wait();
    drop(lease);
    scheduler::record_test_rss(cache_dir, key);
    match status {
        Ok(status) => finish(exit_disposition(status.code(), signals::of(&status))),
        Err(error) => run_failed(&args[0], &error),
    }
}

fn run_failed(program: &OsStr, error: &std::io::Error) -> i32 {
    eprintln!(
        "kache test-runner: cannot run {}: {error}",
        Path::new(program).display()
    );
    RUN_FAILED
}

/// The runner's exit code. A signal death re-raises the signal first.
pub(crate) fn finish(disposition: Disposition) -> i32 {
    match disposition {
        Disposition::Exit(code) => code,
        Disposition::Signal { signal, fallback } => {
            signals::raise(signal);
            fallback
        }
    }
}

#[cfg(unix)]
pub(crate) mod signals {
    use std::sync::atomic::{AtomicI32, Ordering};

    static CHILD: AtomicI32 = AtomicI32::new(0);

    extern "C" fn forward(signal: libc::c_int) {
        // SAFETY: `kill` is async-signal-safe, and `forward_to` stores the
        // pid before it installs this handler.
        unsafe { libc::kill(CHILD.load(Ordering::SeqCst), signal) };
    }

    /// Leave terminal interrupts to the child, which shares the process
    /// group and receives them itself, and pass termination on to it. The
    /// runner then exits the way the child does.
    pub(crate) fn forward_to(child: u32) {
        CHILD.store(child as libc::pid_t, Ordering::SeqCst);
        let handler = forward as extern "C" fn(libc::c_int) as libc::sighandler_t;
        for (signal, action) in [
            (libc::SIGINT, libc::SIG_IGN),
            (libc::SIGQUIT, libc::SIG_IGN),
            (libc::SIGTERM, handler),
            (libc::SIGHUP, handler),
        ] {
            // SAFETY: a zeroed `sigaction` is a valid empty action; the
            // handler only calls `kill`.
            unsafe {
                let mut sa: libc::sigaction = std::mem::zeroed();
                sa.sa_sigaction = action;
                sa.sa_flags = libc::SA_RESTART;
                libc::sigemptyset(&mut sa.sa_mask);
                libc::sigaction(signal, &sa, std::ptr::null_mut());
            }
        }
    }

    pub(crate) fn of(status: &std::process::ExitStatus) -> Option<i32> {
        use std::os::unix::process::ExitStatusExt;
        status.signal()
    }

    /// Die of `signal` without dumping core: the child already did.
    pub(crate) fn raise(signal: i32) {
        // SAFETY: plain libc calls on values owned here.
        unsafe {
            let no_core = libc::rlimit {
                rlim_cur: 0,
                rlim_max: 0,
            };
            libc::setrlimit(libc::RLIMIT_CORE, &no_core);
            libc::signal(signal, libc::SIG_DFL);
            let mut set: libc::sigset_t = std::mem::zeroed();
            libc::sigemptyset(&mut set);
            libc::sigaddset(&mut set, signal);
            libc::pthread_sigmask(libc::SIG_UNBLOCK, &set, std::ptr::null_mut());
            libc::raise(signal);
        }
    }
}

// Named apart from the Unix module so the Linux mutation lane, which never
// compiles it, can exclude it by name.
#[cfg(windows)]
pub(crate) use windows_signals as signals;

#[cfg(windows)]
pub(crate) mod windows_signals {
    /// Leave Ctrl-C to the child, which shares the console and receives it
    /// itself. The runner then exits the way the child does.
    pub(crate) fn forward_to(_child: u32) {
        // SAFETY: a null handler with TRUE makes this process ignore Ctrl-C.
        unsafe {
            windows_sys::Win32::System::Console::SetConsoleCtrlHandler(None, 1);
        }
    }

    pub(crate) fn of(_status: &std::process::ExitStatus) -> Option<i32> {
        None
    }

    pub(super) fn raise(_signal: i32) {}
}

#[cfg(test)]
mod tests {
    use super::*;

    fn os(args: &[&str]) -> Vec<OsString> {
        args.iter().map(OsString::from).collect()
    }

    fn env() -> TestEnv {
        TestEnv {
            nextest_mode: None,
            rust_test_threads: None,
            lease: None,
            pkg_name: Some("demo".into()),
        }
    }

    const PROBE: &str = "/t/debug/deps/probe-0123456789abcdef";

    #[test]
    fn artifact_stem_requires_cargo_metadata_hash() {
        let stem = |name: &str| artifact_stem(OsStr::new(name)).map(str::to_owned);
        assert_eq!(stem(PROBE).as_deref(), Some("probe"));
        assert_eq!(stem("probe-0123456789abcdef").as_deref(), Some("probe"));
        assert_eq!(
            stem("C:/t/deps/my-crate-0123456789abcdef.exe").as_deref(),
            Some("my-crate")
        );
        assert_eq!(stem("/t/debug/demo"), None, "cargo run binary");
        assert_eq!(stem("/tmp/rustdoctestXYZ/rust_out"), None, "doctest");
        assert_eq!(stem("probe-0123456789abcde"), None, "15 hex digits");
        assert_eq!(stem("probe-0123456789abcdef0"), None, "17 hex digits");
        assert_eq!(stem("probe-0123456789ABCDEF"), None, "uppercase");
        assert_eq!(stem("probe-0123456789abcdeg"), None, "not hex");
    }

    #[test]
    fn declared_threads_reads_argv_before_env() {
        assert_eq!(
            declared_threads(&os(&["--test-threads", "3"]), None),
            Some(3)
        );
        assert_eq!(declared_threads(&os(&["--test-threads=5"]), None), Some(5));
        assert_eq!(
            declared_threads(&os(&["--test-threads=5"]), Some("2")),
            Some(5),
            "argv beats env"
        );
        assert_eq!(declared_threads(&os(&["--exact", "t"]), Some("2")), Some(2));
        assert_eq!(declared_threads(&os(&[]), None), None);
    }

    #[test]
    fn declared_threads_rejects_zero_garbage_and_missing_values() {
        assert_eq!(declared_threads(&os(&["--test-threads=0"]), None), None);
        assert_eq!(declared_threads(&os(&["--test-threads", "x"]), None), None);
        assert_eq!(declared_threads(&os(&["--test-threads"]), None), None);
        assert_eq!(declared_threads(&os(&[]), Some("0")), None);
        assert_eq!(declared_threads(&os(&[]), Some("many")), None);
        assert_eq!(declared_threads(&os(&[]), Some("1")), Some(1));
        assert_eq!(
            declared_threads(&os(&["--test-threads=0"]), Some("4")),
            Some(4),
            "an invalid argv value falls back to env"
        );
    }

    #[test]
    fn plan_execs_when_the_scheduler_is_off() {
        assert_eq!(plan(&os(&[PROBE]), &env(), None), Plan::Exec);
    }

    #[test]
    fn plan_execs_unhashed_binaries() {
        let cache = tempfile::tempdir().unwrap();
        assert_eq!(
            plan(&os(&["/t/debug/demo"]), &env(), Some(cache.path())),
            Plan::Exec
        );
        assert_eq!(plan(&[], &env(), Some(cache.path())), Plan::Exec);
    }

    #[test]
    fn plan_execs_listing_and_benches() {
        let cache = tempfile::tempdir().unwrap();
        for flag in ["--list", "--bench"] {
            assert_eq!(
                plan(&os(&[PROBE, flag]), &env(), Some(cache.path())),
                Plan::Exec,
                "{flag}"
            );
        }
    }

    #[test]
    fn plan_execs_under_a_covering_lease() {
        let cache = tempfile::tempdir().unwrap();
        let tests = scheduler::scheduler_root(cache.path()).join("tests");
        std::fs::create_dir_all(&tests).unwrap();
        let covered = TestEnv {
            lease: Some(tests.join("1")),
            ..env()
        };
        assert_eq!(
            plan(&os(&[PROBE]), &covered, Some(cache.path())),
            Plan::Exec
        );
        let other = tempfile::tempdir().unwrap();
        assert!(matches!(
            plan(&os(&[PROBE]), &covered, Some(other.path())),
            Plan::Schedule { .. }
        ));
    }

    #[test]
    fn plan_wants_elastic_floor_by_default() {
        let cache = tempfile::tempdir().unwrap();
        assert_eq!(
            plan(&os(&[PROBE, "--exact", "t"]), &env(), Some(cache.path())),
            Plan::Schedule {
                key: "test:demo:probe".into(),
                want: TestWant::Elastic(1),
            }
        );
        let no_pkg = TestEnv {
            pkg_name: None,
            ..env()
        };
        assert_eq!(
            plan(&os(&[PROBE]), &no_pkg, Some(cache.path())),
            Plan::Schedule {
                key: "test::probe".into(),
                want: TestWant::Elastic(1),
            }
        );
    }

    #[test]
    fn plan_uses_recorded_rss_as_the_floor() {
        let cache = tempfile::tempdir().unwrap();
        scheduler::write_test_weight(cache.path(), "test:demo:probe", 3 * 512 * 1024 * 1024);
        let want = |args: &[&str], env: &TestEnv| match plan(&os(args), env, Some(cache.path())) {
            Plan::Schedule { want, .. } => want,
            Plan::Exec => panic!("hashed binary must be scheduled"),
        };
        assert_eq!(want(&[PROBE], &env()), TestWant::Elastic(3));
        assert_eq!(
            want(&[PROBE, "--test-threads=2"], &env()),
            TestWant::Fixed(3),
            "the floor beats fewer declared threads"
        );
        assert_eq!(
            want(&[PROBE, "--test-threads=5"], &env()),
            TestWant::Fixed(5),
            "more declared threads beat the floor"
        );
        let per_test = TestEnv {
            nextest_mode: Some("process-per-test".into()),
            ..env()
        };
        assert_eq!(want(&[PROBE], &per_test), TestWant::Fixed(3));
    }

    #[test]
    fn exact_test_is_the_argument_after_exact() {
        assert_eq!(
            exact_test(&os(&["--exact", "mod::t", "--nocapture"])),
            Some("mod::t")
        );
        assert_eq!(exact_test(&os(&["--nocapture", "--exact", "t"])), Some("t"));
        assert_eq!(exact_test(&os(&["--exact", "--nocapture"])), None);
        assert_eq!(exact_test(&os(&["--exact"])), None);
        assert_eq!(exact_test(&os(&["t", "--nocapture"])), None);
    }

    #[test]
    fn per_test_runs_keep_their_own_rss_history() {
        const SLOT: u64 = 512 * 1024 * 1024;
        let cache = tempfile::tempdir().unwrap();
        let per_test = TestEnv {
            nextest_mode: Some("process-per-test".into()),
            ..env()
        };
        let plan_of = |test: &str| {
            plan(
                &os(&[PROBE, "--exact", test, "--nocapture"]),
                &per_test,
                Some(cache.path()),
            )
        };
        let Plan::Schedule { key: heavy, .. } = plan_of("heavy") else {
            panic!("hashed binary must be scheduled");
        };
        assert_eq!(heavy, "test:demo:probe:heavy");
        // The heavy test finished last and recorded its peak.
        scheduler::write_test_weight(cache.path(), &heavy, 3 * SLOT);
        assert_eq!(
            plan_of("heavy"),
            Plan::Schedule {
                key: heavy.clone(),
                want: TestWant::Fixed(3),
            }
        );
        assert_eq!(
            plan_of("light"),
            Plan::Schedule {
                key: "test:demo:probe:light".into(),
                want: TestWant::Fixed(1),
            },
            "a light test does not ask for the heavy one's memory"
        );
    }

    #[test]
    fn plan_fixes_declared_and_per_test_wants() {
        let cache = tempfile::tempdir().unwrap();
        let want = |env: &TestEnv| match plan(&os(&[PROBE]), env, Some(cache.path())) {
            Plan::Schedule { want, .. } => want,
            Plan::Exec => panic!("hashed binary must be scheduled"),
        };
        let per_test = TestEnv {
            nextest_mode: Some("process-per-test".into()),
            rust_test_threads: Some("4".into()),
            ..env()
        };
        assert_eq!(want(&per_test), TestWant::Fixed(1));
        let threads = TestEnv {
            rust_test_threads: Some("4".into()),
            ..env()
        };
        assert_eq!(want(&threads), TestWant::Fixed(4));
        let other_mode = TestEnv {
            nextest_mode: Some("process-per-binary".into()),
            ..env()
        };
        assert_eq!(want(&other_mode), TestWant::Elastic(1));
    }

    #[test]
    fn wait_budget_is_shorter_under_nextest() {
        assert_eq!(wait_budget(&env()), scheduler::WAIT_TIMEOUT);
        let nextest = TestEnv {
            nextest_mode: Some("process-per-test".into()),
            ..env()
        };
        assert_eq!(wait_budget(&nextest), Duration::from_secs(120));
    }

    #[test]
    fn lease_env_sets_threads_only_for_elastic_wants() {
        let marker = Path::new("/cache/scheduler/tests/2");
        assert_eq!(
            lease_env(TestWant::Elastic(1), marker, 3),
            vec![
                (scheduler::TEST_LEASE_ENV, OsString::from(marker)),
                ("RUST_TEST_THREADS", OsString::from("3")),
            ]
        );
        assert_eq!(
            lease_env(TestWant::Fixed(3), marker, 3),
            vec![(scheduler::TEST_LEASE_ENV, OsString::from(marker))]
        );
    }

    #[test]
    fn exit_disposition_keeps_codes_and_signals() {
        assert_eq!(exit_disposition(Some(7), None), Disposition::Exit(7));
        assert_eq!(exit_disposition(Some(0), Some(9)), Disposition::Exit(0));
        assert_eq!(
            exit_disposition(None, Some(6)),
            Disposition::Signal {
                signal: 6,
                fallback: 134,
            }
        );
        assert_eq!(exit_disposition(None, None), Disposition::Exit(101));
    }

    #[test]
    fn finish_returns_the_exit_code() {
        assert_eq!(finish(Disposition::Exit(7)), 7);
        assert_eq!(finish(Disposition::Exit(0)), 0);
    }
}