rigtest 0.4.0

Runtime library for the cargo-rigtest test framework
Documentation
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::anyhow;
use rand::seq::SliceRandom as _;
use rand::RngExt as _;
use rand::SeedableRng as _;
use tokio::sync::Semaphore;
use tokio::task::JoinSet;

use crate::junit::{JunitConfig, JunitReporter};
use crate::protocol::SubprocessOutcome;
use crate::registry::{RIG_GLOBAL_SETUP, RIG_GLOBAL_TEARDOWN, RIG_TEST_CASES};
use crate::reporter::{
    MultiReporter, Outcome as ReportOutcome, Reporter, TestEventReporter, TestRef,
};
use crate::scheduler::RuntimeArgs;
use crate::subprocess::{OsSubprocessRunner, SpawnRequest, SubprocessRunner};

fn classify_failed(stderr: &str) -> ReportOutcome {
    if stderr.contains("panicked at") {
        ReportOutcome::Panic
    } else {
        ReportOutcome::Assertion
    }
}

fn test_ref(tc: &crate::registry::TestCase) -> TestRef<'_> {
    TestRef {
        name: tc.name,
        module: tc.module,
        file: tc.file,
    }
}

/// Build the reporter stack from CLI args. Always includes the live console
/// [`Reporter`]; `--reporter junit` adds a [`JunitReporter`] alongside it.
fn build_reporter(args: &RuntimeArgs, seed: u64) -> anyhow::Result<MultiReporter> {
    let mut reporters: Vec<Box<dyn TestEventReporter>> = vec![Box::new(Reporter::new())];

    if let Some(name) = args.reporter.as_deref() {
        match name {
            "junit" => {
                let config = resolve_junit_config(seed)?;
                reporters.push(Box::new(JunitReporter::new(config)));
            }
            other => {
                return Err(anyhow!(
                    "cargo-rigtest: unknown --reporter '{other}' (expected 'junit')"
                ));
            }
        }
    }

    Ok(MultiReporter::new(reporters))
}

/// Strip a trailing cargo hash suffix (e.g. `acceptance-9dbf02a2431e03ff`)
/// from a binary stem. Cargo's metadata hash is always 16 ASCII hex chars —
/// gating on that length prevents mis-stripping legitimate names that happen
/// to end in hex (e.g. `my-test-cafe`).
fn strip_hash_suffix(stem: &str) -> &str {
    if let Some(idx) = stem.rfind('-') {
        let tail = &stem[idx + 1..];
        if tail.len() == 16 && tail.chars().all(|c| c.is_ascii_hexdigit()) {
            return &stem[..idx];
        }
    }
    stem
}

#[cfg(test)]
mod tests_strip_hash {
    use super::strip_hash_suffix;

    #[test]
    fn strips_16_char_hex_suffix() {
        assert_eq!(
            strip_hash_suffix("acceptance-9dbf02a2431e03ff"),
            "acceptance"
        );
    }

    #[test]
    fn preserves_short_hex_tail() {
        assert_eq!(strip_hash_suffix("my-test-cafe"), "my-test-cafe");
    }

    #[test]
    fn preserves_non_hex_tail() {
        assert_eq!(strip_hash_suffix("my-test-foobar"), "my-test-foobar");
    }

    #[test]
    fn preserves_stem_without_dash() {
        assert_eq!(strip_hash_suffix("acceptance"), "acceptance");
    }
}

fn resolve_junit_config(seed: u64) -> anyhow::Result<JunitConfig> {
    let exe =
        std::env::current_exe().map_err(|e| anyhow!("failed to find current executable: {e}"))?;
    let raw_stem = exe
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("rigtest");
    let binary_stem = strip_hash_suffix(raw_stem).to_string();

    let output_path = match std::env::var("RIGTEST_JUNIT_OUTPUT_PATH").ok() {
        Some(p) => std::path::PathBuf::from(p),
        None => default_junit_output_path(&exe),
    };

    // When the parent invokes us it passes the target name verbatim so the
    // suite element matches the human-readable name even if the part file
    // is keyed by a unique executable stem. Fall back to deriving from the
    // current executable for direct-invocation use cases.
    let suite_name = std::env::var("RIGTEST_JUNIT_SUITE_NAME")
        .ok()
        .filter(|s| !s.is_empty())
        .unwrap_or(binary_stem);

    Ok(JunitConfig {
        output_path,
        suite_name,
        seed,
    })
}

/// Default to `<target>/rigtest/junit.xml` resolved by walking up from the
/// current exe to the `target` directory cargo built it into.
fn default_junit_output_path(exe: &std::path::Path) -> std::path::PathBuf {
    let target_dir = exe
        .ancestors()
        .find(|p| p.file_name().is_some_and(|n| n == "target"))
        .map(std::path::Path::to_path_buf);

    target_dir
        .unwrap_or_else(|| std::path::PathBuf::from("target"))
        .join("rigtest")
        .join("junit.xml")
}

fn default_jobs() -> usize {
    std::thread::available_parallelism().map_or(4, std::num::NonZero::get)
}

fn apply_filter<'a>(
    cases: &[&'a crate::registry::TestCase],
    filter: Option<&str>,
) -> Vec<&'a crate::registry::TestCase> {
    cases
        .iter()
        .filter(|tc| filter.is_none_or(|f| tc.name.contains(f)))
        .copied()
        .collect()
}

#[derive(Clone, Copy)]
enum Outcome {
    Passed,
    Skipped,
    Failed,
}

/// Run a test with retries, returning the final outcome and updating the
/// reporter.
async fn run_test<R: SubprocessRunner, P: TestEventReporter>(
    runner: &R,
    reporter: &P,
    tc: &crate::registry::TestCase,
    state_var: &str,
    state_json: &str,
) -> (Outcome, Duration) {
    let tref = test_ref(tc);
    reporter.test_started(tref);
    let test_start = Instant::now();
    let max_attempts = tc.retries + 1;
    let mut attempt_start = Instant::now();

    for attempt in 1..=max_attempts {
        let outcome = runner
            .run(SpawnRequest {
                test_name: tc.name,
                state_var,
                state_json,
                timeout: tc.timeout,
            })
            .await;

        let is_last = attempt == max_attempts;
        let duration = test_start.elapsed();
        let attempt_duration = attempt_start.elapsed();

        match outcome {
            Ok(SubprocessOutcome::Passed) => {
                reporter.test_passed(tref, duration);
                return (Outcome::Passed, duration);
            }
            Ok(SubprocessOutcome::Skipped(reason)) => {
                reporter.test_skipped(tref, duration, &reason);
                return (Outcome::Skipped, duration);
            }
            Ok(SubprocessOutcome::Failed {
                reason,
                stdout,
                stderr,
            }) => {
                let report_outcome = classify_failed(&stderr);
                if is_last {
                    reporter.test_failed(tref, duration, report_outcome, &reason, &stdout, &stderr);
                    return (Outcome::Failed, duration);
                }
                reporter.test_retrying(
                    tref,
                    attempt,
                    max_attempts,
                    report_outcome,
                    &reason,
                    &stdout,
                    &stderr,
                    attempt_duration,
                );
            }
            Ok(SubprocessOutcome::TimedOut(dur)) => {
                let reason = format!("timed out after {:.1}s", dur.as_secs_f64());
                if is_last {
                    reporter.test_failed(tref, duration, ReportOutcome::Timeout, &reason, "", "");
                    return (Outcome::Failed, duration);
                }
                reporter.test_retrying(
                    tref,
                    attempt,
                    max_attempts,
                    ReportOutcome::Timeout,
                    &reason,
                    "",
                    "",
                    attempt_duration,
                );
            }
            Err(e) => {
                if is_last {
                    reporter.test_failed(
                        tref,
                        duration,
                        ReportOutcome::Crash,
                        &e.to_string(),
                        "",
                        "",
                    );
                    return (Outcome::Failed, duration);
                }
                reporter.test_retrying(
                    tref,
                    attempt,
                    max_attempts,
                    ReportOutcome::Crash,
                    &e.to_string(),
                    "",
                    "",
                    attempt_duration,
                );
            }
        }

        attempt_start = Instant::now();
    }

    unreachable!()
}

async fn dispatch_cases<R: SubprocessRunner, P: TestEventReporter>(
    runner: Arc<R>,
    reporter: Arc<P>,
    state_var: String,
    state_json: String,
    semaphore: Arc<Semaphore>,
    parallel_cases: Vec<&'static crate::registry::TestCase>,
    serial_cases: Vec<&'static crate::registry::TestCase>,
) -> (usize, usize) {
    let mut passed = 0usize;
    let mut skipped = 0usize;
    let mut join_set: JoinSet<Outcome> = JoinSet::new();

    for tc in parallel_cases {
        let runner = Arc::clone(&runner);
        let reporter = Arc::clone(&reporter);
        let semaphore = Arc::clone(&semaphore);
        let state_var = state_var.clone();
        let state_json = state_json.clone();

        join_set.spawn(async move {
            let _permit = semaphore
                .acquire()
                .await
                .expect("semaphore should not be closed");
            let (outcome, _) = run_test(&*runner, &*reporter, tc, &state_var, &state_json).await;
            outcome
        });
    }

    while let Some(result) = join_set.join_next().await {
        match result {
            Ok(Outcome::Passed) => passed += 1,
            Ok(Outcome::Skipped) => skipped += 1,
            Ok(Outcome::Failed) => {}
            Err(e) => eprintln!("cargo-rigtest: task join error: {e}"),
        }
    }

    for tc in serial_cases {
        let (outcome, _) = run_test(&*runner, &*reporter, tc, &state_var, &state_json).await;
        match outcome {
            Outcome::Passed => passed += 1,
            Outcome::Skipped => skipped += 1,
            Outcome::Failed => {}
        }
    }

    (passed, skipped)
}

/// Run the full test suite (coordinator path).
///
/// # Errors
///
/// Returns an error if any test fails or if the current executable path
/// cannot be determined.
///
/// # Panics
///
/// Panics if more than one `#[global_setup]` or `#[global_teardown]` function
/// is registered.
pub(crate) async fn run(args: RuntimeArgs) -> anyhow::Result<()> {
    assert!(
        RIG_GLOBAL_SETUP.len() <= 1,
        "cargo-rigtest: at most one #[global_setup] function may be defined, found {}",
        RIG_GLOBAL_SETUP.len()
    );
    assert!(
        RIG_GLOBAL_TEARDOWN.len() <= 1,
        "cargo-rigtest: at most one #[global_teardown] function may be defined, found {}",
        RIG_GLOBAL_TEARDOWN.len()
    );
    #[cfg(feature = "http-client")]
    assert!(
        crate::registry::RIG_HTTP_CLIENT_CONFIGURATOR.len() <= 1,
        "cargo-rigtest: at most one #[rigtest::main(http_client = …)] may be defined, found {}",
        crate::registry::RIG_HTTP_CLIENT_CONFIGURATOR.len()
    );
    #[cfg(all(feature = "ssh-client", unix))]
    assert!(
        crate::registry::RIG_SSH_CLIENT_CONFIGURATOR.len() <= 1,
        "cargo-rigtest: at most one #[rigtest::main(ssh_client = …)] may be defined, found {}",
        crate::registry::RIG_SSH_CLIENT_CONFIGURATOR.len()
    );

    let mut rng = rand::rng();
    let seed = args.seed.unwrap_or_else(|| rng.random::<u64>());

    let reporter = Arc::new(build_reporter(&args, seed)?);

    let global_setup = RIG_GLOBAL_SETUP.first();

    let global_data: Box<dyn std::any::Any + Send + Sync> = if let Some(entry) = global_setup {
        reporter.print_phase("global setup");
        (entry.setup_fn)().await
    } else {
        Box::new(())
    };

    let state_var = format!("RIG_STATE_{:016x}", rng.random::<u64>());
    let state_json: String = if let Some(entry) = global_setup {
        (entry.serialize_fn)(&*global_data)
    } else {
        String::new()
    };

    let cases_refs: Vec<&'static crate::registry::TestCase> = RIG_TEST_CASES.iter().collect();
    let mut cases = apply_filter(&cases_refs, args.filter.as_deref());

    println!("cargo-rigtest: running with seed {seed}");

    let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(seed);
    cases.shuffle(&mut rng);

    let total = cases.len();
    let jobs = if args.no_capture {
        1
    } else {
        args.jobs.unwrap_or_else(default_jobs)
    };
    let semaphore = Arc::new(Semaphore::new(jobs));

    let exe =
        std::env::current_exe().map_err(|e| anyhow!("failed to find current executable: {e}"))?;

    let runner = Arc::new(OsSubprocessRunner::new(exe, args.no_capture));

    let suite_start = Instant::now();

    let (serial_cases, parallel_cases): (Vec<_>, Vec<_>) =
        cases.into_iter().partition(|tc| tc.serial);

    let (passed, skipped) = dispatch_cases(
        runner,
        Arc::clone(&reporter),
        state_var,
        state_json,
        semaphore,
        parallel_cases,
        serial_cases,
    )
    .await;

    let elapsed = suite_start.elapsed();
    let finish_result = reporter.finish(passed, skipped, total, elapsed);

    if let Some(entry) = RIG_GLOBAL_TEARDOWN.first() {
        reporter.print_phase("global teardown");
        (entry.teardown_fn)(global_data).await;
    }

    let failed = total - passed - skipped;
    if failed > 0 {
        Err(anyhow!("Test suite failed: {passed}/{total} passed"))
    } else {
        // Surface a reporter (e.g. JUnit XML) write error as the run's
        // exit so a CI consumer that promised an artifact gets a hard fail
        // rather than a misleading green.
        finish_result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::TestContext;
    use crate::registry::{BoxFuture, TestCase};
    use crate::reporter::{Event, NullReporter, RecordingReporter};
    use std::sync::Mutex;

    fn make_case(name: &'static str) -> TestCase {
        TestCase {
            name,
            module: "test_module",
            file: "test.rs",
            serial: false,
            timeout: None,
            retries: 0,
            test_fn: |_ctx: Arc<TestContext>| -> BoxFuture<
                'static,
                Result<(), Box<dyn std::error::Error + Send + Sync>>,
            > { Box::pin(async { Ok(()) }) },
        }
    }

    #[test]
    fn filter_none_returns_all() {
        let cases = [make_case("foo"), make_case("bar"), make_case("baz")];
        let refs: Vec<&TestCase> = cases.iter().collect();
        assert_eq!(apply_filter(&refs, None).len(), 3);
    }

    #[test]
    fn filter_matches_substring() {
        let cases = [
            make_case("test_login"),
            make_case("test_logout"),
            make_case("health_check"),
        ];
        let refs: Vec<&TestCase> = cases.iter().collect();
        let filtered = apply_filter(&refs, Some("test_"));
        assert_eq!(filtered.len(), 2);
        assert!(filtered.iter().all(|tc| tc.name.contains("test_")));
    }

    #[test]
    fn filter_no_match_returns_empty() {
        let cases = [make_case("foo"), make_case("bar")];
        let refs: Vec<&TestCase> = cases.iter().collect();
        assert_eq!(apply_filter(&refs, Some("xyz")).len(), 0);
    }

    // ── Subprocess seam demo tests ───────────────────────────────────────
    //
    // These prove the SubprocessRunner trait is a real seam: the
    // orchestration logic (retry loop) is now driveable end-to-end with a
    // fake runner — no OS processes spawned.

    /// Test double that returns a pre-programmed queue of outcomes and records
    /// every call. Multiple queued outcomes per test exercise the retry path.
    struct FakeRunner {
        queue: Mutex<Vec<SubprocessOutcome>>,
        calls: Mutex<u32>,
    }

    impl FakeRunner {
        fn new(outcomes: Vec<SubprocessOutcome>) -> Self {
            Self {
                queue: Mutex::new(outcomes),
                calls: Mutex::new(0),
            }
        }

        fn call_count(&self) -> u32 {
            *self.calls.lock().unwrap()
        }
    }

    impl SubprocessRunner for FakeRunner {
        async fn run(&self, _req: SpawnRequest<'_>) -> anyhow::Result<SubprocessOutcome> {
            *self.calls.lock().unwrap() += 1;
            Ok(self.queue.lock().unwrap().remove(0))
        }
    }

    fn case_with_retries(name: &'static str, retries: u32) -> TestCase {
        let mut tc = make_case(name);
        tc.retries = retries;
        tc
    }

    #[tokio::test]
    async fn retry_loop_succeeds_after_one_failure() {
        let runner = FakeRunner::new(vec![
            SubprocessOutcome::Failed {
                reason: "transient".into(),
                stdout: String::new(),
                stderr: String::new(),
            },
            SubprocessOutcome::Passed,
        ]);
        let tc = case_with_retries("flaky", 1);
        let reporter = NullReporter;

        let (outcome, _) = run_test(&runner, &reporter, &tc, "X", "{}").await;

        assert!(matches!(outcome, Outcome::Passed));
        assert_eq!(runner.call_count(), 2);
    }

    #[tokio::test]
    async fn skip_does_not_retry() {
        let runner = FakeRunner::new(vec![SubprocessOutcome::Skipped("nope".into())]);
        let tc = case_with_retries("skipper", 3);
        let reporter = NullReporter;

        let (outcome, _) = run_test(&runner, &reporter, &tc, "X", "{}").await;

        assert!(matches!(outcome, Outcome::Skipped));
        assert_eq!(runner.call_count(), 1);
    }

    #[tokio::test]
    async fn retry_exhausts_and_reports_failure() {
        let mk_fail = || SubprocessOutcome::Failed {
            reason: "boom".into(),
            stdout: String::new(),
            stderr: String::new(),
        };
        let runner = FakeRunner::new(vec![mk_fail(), mk_fail(), mk_fail()]);
        let tc = case_with_retries("always_fails", 2);
        let reporter = NullReporter;

        let (outcome, _) = run_test(&runner, &reporter, &tc, "X", "{}").await;

        assert!(matches!(outcome, Outcome::Failed));
        assert_eq!(runner.call_count(), 3); // initial + 2 retries
    }

    // ── Reporter seam: assert on the event sequence ──────────────────────

    #[test]
    fn classify_failed_detects_panic_marker() {
        let panic = "thread 'main' panicked at src/lib.rs:1\n";
        assert!(matches!(classify_failed(panic), ReportOutcome::Panic));
        assert!(matches!(
            classify_failed("error: boom"),
            ReportOutcome::Assertion
        ));
        assert!(matches!(classify_failed(""), ReportOutcome::Assertion));
    }

    #[tokio::test]
    async fn retry_emits_retrying_event_before_passed() {
        let runner = FakeRunner::new(vec![
            SubprocessOutcome::Failed {
                reason: "first failure".into(),
                stdout: String::new(),
                stderr: String::new(),
            },
            SubprocessOutcome::Passed,
        ]);
        let tc = case_with_retries("flaky", 1);
        let reporter = RecordingReporter::new();

        let (outcome, _) = run_test(&runner, &reporter, &tc, "X", "{}").await;

        assert!(matches!(outcome, Outcome::Passed));
        let events = reporter.events();
        assert!(matches!(events[0], Event::Started(ref n) if n == "flaky"));
        assert!(
            matches!(events[1], Event::Retrying(ref n, 1, 2, _, _) if n == "flaky"),
            "expected Retrying(flaky, 1/2) at index 1, got {:?}",
            events[1]
        );
        assert!(matches!(events[2], Event::Passed(ref n) if n == "flaky"));
        assert_eq!(events.len(), 3);
    }

    // ── Dispatch tests: serial/parallel ordering, semaphore cap, counts ──

    use std::collections::HashMap;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn leaked_case(name: &'static str, serial: bool) -> &'static TestCase {
        let mut tc = make_case(name);
        tc.serial = serial;
        Box::leak(Box::new(tc))
    }

    /// Returns a pre-programmed outcome per test name; otherwise Passed.
    struct ByNameRunner {
        outcomes: HashMap<&'static str, SubprocessOutcome>,
    }

    impl SubprocessRunner for ByNameRunner {
        async fn run(&self, req: SpawnRequest<'_>) -> anyhow::Result<SubprocessOutcome> {
            Ok(self
                .outcomes
                .get(req.test_name)
                .cloned()
                .unwrap_or(SubprocessOutcome::Passed))
        }
    }

    #[tokio::test]
    async fn dispatch_counts_pass_skip_fail_correctly() {
        let mut outcomes = HashMap::new();
        outcomes.insert("a", SubprocessOutcome::Passed);
        outcomes.insert("b", SubprocessOutcome::Skipped("nope".into()));
        outcomes.insert(
            "c",
            SubprocessOutcome::Failed {
                reason: "boom".into(),
                stdout: String::new(),
                stderr: String::new(),
            },
        );
        outcomes.insert("d", SubprocessOutcome::Passed);
        outcomes.insert("e", SubprocessOutcome::Passed);
        let runner = Arc::new(ByNameRunner { outcomes });
        let reporter = Arc::new(NullReporter);
        let semaphore = Arc::new(Semaphore::new(4));

        let cases: Vec<&'static TestCase> = ["a", "b", "c", "d", "e"]
            .into_iter()
            .map(|n| leaked_case(n, false))
            .collect();

        let (passed, skipped) = dispatch_cases(
            runner,
            reporter,
            "X".into(),
            "{}".into(),
            semaphore,
            cases,
            Vec::new(),
        )
        .await;

        assert_eq!(passed, 3);
        assert_eq!(skipped, 1);
        // failed = total - passed - skipped = 5 - 3 - 1 = 1
    }

    #[tokio::test]
    async fn dispatch_runs_serial_cases_after_all_parallel() {
        let runner = Arc::new(ByNameRunner {
            outcomes: HashMap::new(),
        });
        let reporter = Arc::new(RecordingReporter::new());
        let semaphore = Arc::new(Semaphore::new(2));

        let parallel = vec![
            leaked_case("p1", false),
            leaked_case("p2", false),
            leaked_case("p3", false),
        ];
        let serial = vec![leaked_case("s1", true), leaked_case("s2", true)];

        let _ = dispatch_cases(
            Arc::clone(&runner),
            Arc::clone(&reporter),
            "X".into(),
            "{}".into(),
            semaphore,
            parallel,
            serial,
        )
        .await;

        let events = reporter.events();
        let started: Vec<&str> = events
            .iter()
            .filter_map(|e| match e {
                Event::Started(n) => Some(n.as_str()),
                _ => None,
            })
            .collect();
        // Every "p*" must come before every "s*".
        let last_parallel_idx = started
            .iter()
            .rposition(|n| n.starts_with('p'))
            .expect("at least one parallel started");
        let first_serial_idx = started
            .iter()
            .position(|n| n.starts_with('s'))
            .expect("at least one serial started");
        assert!(
            last_parallel_idx < first_serial_idx,
            "expected all parallel cases to start before any serial case, got started order: {started:?}"
        );
    }

    /// Runner that records the maximum number of concurrent in-flight calls
    /// observed at any point.
    struct ConcurrencyRunner {
        active: AtomicUsize,
        max_observed: AtomicUsize,
    }

    impl ConcurrencyRunner {
        fn new() -> Self {
            Self {
                active: AtomicUsize::new(0),
                max_observed: AtomicUsize::new(0),
            }
        }
    }

    impl SubprocessRunner for ConcurrencyRunner {
        async fn run(&self, _req: SpawnRequest<'_>) -> anyhow::Result<SubprocessOutcome> {
            let now = self.active.fetch_add(1, Ordering::SeqCst) + 1;
            self.max_observed.fetch_max(now, Ordering::SeqCst);
            // Yield so other tasks can interleave and bump `active` if they
            // are allowed to.
            tokio::time::sleep(Duration::from_millis(10)).await;
            self.active.fetch_sub(1, Ordering::SeqCst);
            Ok(SubprocessOutcome::Passed)
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn dispatch_respects_semaphore_cap() {
        let runner = Arc::new(ConcurrencyRunner::new());
        let reporter = Arc::new(NullReporter);
        let semaphore = Arc::new(Semaphore::new(2));

        let cases: Vec<&'static TestCase> = (0..10)
            .map(|i| {
                let name: &'static str = Box::leak(format!("t{i}").into_boxed_str());
                leaked_case(name, false)
            })
            .collect();

        let _ = dispatch_cases(
            Arc::clone(&runner),
            reporter,
            "X".into(),
            "{}".into(),
            semaphore,
            cases,
            Vec::new(),
        )
        .await;

        let max = runner.max_observed.load(Ordering::SeqCst);
        assert!(
            max <= 2,
            "semaphore cap of 2 violated: max concurrent was {max}"
        );
        assert!(
            max >= 1,
            "expected some concurrency to be observed, got {max}"
        );
    }
}