camber 0.4.2

Opinionated async Rust for IO-bound services on top of Tokio
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
use super::BoundListener;
use std::sync::atomic::Ordering;

#[test]
fn bound_listener_transfers_without_rebind() {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
        .unwrap();
    runtime
        .block_on(async {
            let reservation = BoundListener::bind_tcp("127.0.0.1:0")?;
            let reserved_addr = reservation.local_addr();
            let mut router = camber::http::Router::new();
            router.get("/", |_request: &camber::http::Request| async {
                camber::http::Response::text(200, "transferred")
            });
            let server =
                super::ReadyServer::start(reservation, router, std::time::Duration::from_secs(1))?;
            let cleanup = server.cleanup_probe();
            let second_bind = std::net::TcpListener::bind(server.local_addr());
            assert_eq!(
                second_bind.unwrap_err().kind(),
                std::io::ErrorKind::AddrInUse
            );
            let response = super::request(
                reserved_addr,
                "GET",
                "/",
                &[],
                &[],
                std::time::Duration::from_secs(1),
            )?;
            assert_eq!(response.status, 200);
            assert_eq!(response.body.as_ref(), b"transferred");
            server.shutdown_bounded(std::time::Duration::from_secs(1))?;
            assert!(cleanup.joined());
            assert_eq!(cleanup.cleanup_error(), None);
            Ok::<_, super::FixtureError>(())
        })
        .unwrap();
}

#[test]
fn server_readiness_is_signaled_without_sleep() {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
        .unwrap();
    runtime
        .block_on(async {
            let mut router = camber::http::Router::new();
            router.get("/", |_request: &camber::http::Request| async {
                camber::http::Response::text(200, "ready")
            });
            let server = super::spawn_server_ready(router, std::time::Duration::from_secs(1))?;
            assert!(server.local_addr().ip().is_loopback());
            assert_eq!(server.readiness_response().status, 400);
            server.shutdown_bounded(std::time::Duration::from_secs(1))?;
            Ok::<_, super::FixtureError>(())
        })
        .unwrap();
}

#[test]
fn paused_checkpoint_wait_reports_a_missing_observation() {
    let controller = camber::runtime_test_support::runtime_schedule();
    let never = camber::runtime_test_support::RuntimeCheckpoint::ScopeWaitObserved(0);

    // Nothing armed it and no runtime will reach it, so the wait must return
    // at its bound: an observer that parked here would hang the drain-window
    // fixtures instead of failing them.
    assert!(!super::wait_paused_bounded(
        &controller,
        never,
        std::time::Duration::from_millis(25)
    ));
}

#[test]
fn paused_window_probe_reports_a_window_that_never_opened() {
    let controller = camber::runtime_test_support::runtime_schedule();
    let never = camber::runtime_test_support::RuntimeCheckpoint::ScopeWaitObserved(0);
    let probed = std::cell::Cell::new(false);

    // Nothing armed it, so the window never opens. The helper must report the
    // miss at its bound and leave the probe unrun: a fixture that read a
    // default observation as a real one would assert on a reading production
    // never produced.
    let observed = super::probe_paused_window(
        &controller,
        never,
        std::time::Duration::from_millis(25),
        || probed.set(true),
    );

    assert_eq!(observed, None);
    assert!(!probed.get(), "the probe ran on a window that never opened");
}

#[test]
fn paused_window_probe_leaves_an_armed_checkpoint_holding_nothing() {
    let controller = camber::runtime_test_support::runtime_schedule();
    let armed = camber::runtime_test_support::RuntimeCheckpoint::ScopeWaitObserved(0);
    let next = camber::runtime_test_support::RuntimeCheckpoint::ScopeWaitObserved(1);

    controller.pause_once(armed).unwrap();
    let observed = super::probe_paused_window(
        &controller,
        armed,
        std::time::Duration::from_millis(25),
        || (),
    );
    assert_eq!(observed, None);

    // The checkpoint was armed and nothing ever paused at it, which is the case
    // `release` cannot clear — it reports an error precisely when nothing is
    // paused. `arm` refuses while anything is still armed, so this succeeding
    // is what says the guard left nothing behind. Left armed, the next
    // production run to reach it would park with no observer to let it go.
    assert!(
        controller.pause_once(next).is_ok(),
        "the expired probe left its checkpoint armed"
    );
    controller.disarm();
}

#[test]
fn bounded_read_reports_timeout() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let mut client = std::net::TcpStream::connect(listener.local_addr().unwrap()).unwrap();
    let (_server, _) = listener.accept().unwrap();

    let result = super::bounded_read(&mut client, std::time::Duration::from_millis(25), 1024);
    assert!(matches!(
        result,
        Err(super::BoundedReadError::Timeout { .. })
    ));
}

#[test]
fn child_guard_reaps_after_assertion_panic() {
    const MODE: &str = "panic-cleanup";
    if hold_until_parent_kills(MODE, "FIXTURE_READY") {
        return;
    }

    let mut child = super::ChildGuard::spawn_exact_current(
        "fixture_contracts::child_guard_reaps_after_assertion_panic",
        MODE,
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    let child_id = child.id();
    let probe = child.take_reap_probe().unwrap();
    child
        .wait_for_line("FIXTURE_READY", std::time::Duration::from_secs(2))
        .unwrap();

    let panic_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _owned_child = child;
        assert_eq!(1, 2, "intentional fixture assertion failure");
    }));
    assert!(panic_result.is_err());
    assert_eq!(
        probe
            .wait(std::time::Duration::from_secs(2))
            .unwrap()
            .child_id(),
        child_id
    );
}

#[test]
fn child_guard_kills_and_waits_after_readiness_timeout() {
    const MODE: &str = "readiness-timeout";
    if hold_until_parent_kills(MODE, "CHILD_STARTED") {
        return;
    }

    let mut child = super::ChildGuard::spawn_exact_current(
        "fixture_contracts::child_guard_kills_and_waits_after_readiness_timeout",
        MODE,
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    let child_id = child.id();
    let probe = child.take_reap_probe().unwrap();
    child
        .wait_for_line("CHILD_STARTED", std::time::Duration::from_secs(2))
        .unwrap();

    let result = child.wait_for_readiness("SERVER_READY", std::time::Duration::from_millis(25));
    assert!(matches!(
        result,
        Err(super::ProcessError::ReadinessTimeout { .. })
    ));
    let reaped = probe.wait(std::time::Duration::from_secs(2)).unwrap();
    assert_eq!(reaped.child_id(), child_id);
    assert!(!reaped.status().success());
}

#[test]
fn isolated_global_contract_runs_once_in_child() {
    const METRICS_MODE: &str = "isolated-metrics";
    const TEXT_MODE: &str = "isolated-logging-text";
    const JSON_MODE: &str = "isolated-logging-json";
    if super::is_private_child(METRICS_MODE) {
        super::test_runtime()
            .with_metrics()
            .run(|| {
                metrics::counter!("phase3_fixture_metrics_total").increment(1);
                println!("METRICS_INITIALIZED_ONCE");
            })
            .unwrap();
        std::io::Write::flush(&mut std::io::stdout()).unwrap();
        return;
    }
    if super::is_private_child(TEXT_MODE) {
        camber::logging::init_logging(
            camber::logging::LogFormat::Text,
            camber::logging::LogLevel::Info,
        );
        camber::tracing::info!(message = "PHASE3_TEXT_LOG_EVENT");
        println!("TEXT_LOGGING_INITIALIZED_ONCE");
        std::io::Write::flush(&mut std::io::stdout()).unwrap();
        return;
    }
    if super::is_private_child(JSON_MODE) {
        camber::logging::init_logging(
            camber::logging::LogFormat::Json,
            camber::logging::LogLevel::Info,
        );
        camber::tracing::info!(message = "PHASE3_JSON_LOG_EVENT");
        println!("JSON_LOGGING_INITIALIZED_ONCE");
        std::io::Write::flush(&mut std::io::stdout()).unwrap();
        return;
    }

    let metrics = super::run_isolated_exact(
        "fixture_contracts::isolated_global_contract_runs_once_in_child",
        METRICS_MODE,
        "METRICS_INITIALIZED_ONCE",
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    assert!(metrics.success());
    assert_output_once(&metrics, "METRICS_INITIALIZED_ONCE");
    let text = super::run_isolated_exact(
        "fixture_contracts::isolated_global_contract_runs_once_in_child",
        TEXT_MODE,
        "TEXT_LOGGING_INITIALIZED_ONCE",
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    assert!(text.success());
    assert_output_once(&text, "TEXT_LOGGING_INITIALIZED_ONCE");
    assert_output_once(&text, "PHASE3_TEXT_LOG_EVENT");
    let json = super::run_isolated_exact(
        "fixture_contracts::isolated_global_contract_runs_once_in_child",
        JSON_MODE,
        "JSON_LOGGING_INITIALIZED_ONCE",
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    assert!(json.success());
    assert_output_once(&json, "JSON_LOGGING_INITIALIZED_ONCE");
    assert_output_once(&json, "\"message\":\"PHASE3_JSON_LOG_EVENT\"");
}

#[cfg(unix)]
#[test]
fn signal_contract_cannot_reach_parent_process() {
    const SIGINT_MODE: &str = "isolated-sigint";
    const SIGTERM_MODE: &str = "isolated-sigterm";
    let child_mode = match (
        super::is_private_child(SIGINT_MODE),
        super::is_private_child(SIGTERM_MODE),
    ) {
        (true, false) => Some((SIGINT_MODE, signal_hook::consts::SIGINT)),
        (false, true) => Some((SIGTERM_MODE, signal_hook::consts::SIGTERM)),
        _ => None,
    };
    if let Some((mode, signal)) = child_mode {
        let parent_id = super::private_child_parent_id().unwrap();
        assert_ne!(parent_id, std::process::id());
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        runtime.block_on(async {
            let shutdown = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
            let notify = std::sync::Arc::new(tokio::sync::Notify::new());
            let watcher =
                camber::signals::spawn_signal_watcher(std::sync::Arc::clone(&shutdown), notify);
            signal_hook::low_level::raise(signal).unwrap();
            tokio::time::timeout(std::time::Duration::from_secs(1), watcher)
                .await
                .unwrap()
                .unwrap();
            assert!(shutdown.load(Ordering::Acquire));
        });
        println!("CHILD_SIGNAL_CONTRACT_COMPLETE={mode}");
        std::io::Write::flush(&mut std::io::stdout()).unwrap();
        return;
    }

    let parent_id = std::process::id();
    let sigint = super::run_isolated_exact(
        "fixture_contracts::signal_contract_cannot_reach_parent_process",
        SIGINT_MODE,
        "CHILD_SIGNAL_CONTRACT_COMPLETE=isolated-sigint",
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    assert!(sigint.success());
    let sigterm = super::run_isolated_exact(
        "fixture_contracts::signal_contract_cannot_reach_parent_process",
        SIGTERM_MODE,
        "CHILD_SIGNAL_CONTRACT_COMPLETE=isolated-sigterm",
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    assert!(sigterm.success());
    assert_eq!(std::process::id(), parent_id);
}

#[test]
fn complete_http_response_parses_chunked_body_without_eof() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let address = listener.local_addr().unwrap();
    let (release_sender, release_receiver) = std::sync::mpsc::channel();
    let mut server = Some(std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        std::io::Write::write_all(
            &mut stream,
            b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\n\r\n4\r\nWiki\r\n5\r\npedia\r\n0\r\nX-Test: complete\r\n\r\n",
        )
        .unwrap();
        release_receiver
            .recv_timeout(std::time::Duration::from_secs(2))
            .unwrap();
    }));
    let mut client = std::net::TcpStream::connect(address).unwrap();
    // One deadline over the whole response, not a bound per read: the peer holds
    // the connection open after the terminating chunk, so a reader given a fresh
    // budget per syscall would have nothing to expire against.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
    let response = super::read_http_response(&mut client, Some(deadline)).unwrap();
    assert_eq!(response.status, 200);
    assert_eq!(response.body.as_ref(), b"Wikipedia");
    assert_eq!(response.header("connection"), Some("keep-alive"));
    release_sender.send(()).unwrap();
    super::join_thread_bounded(&mut server, std::time::Duration::from_secs(1)).unwrap();
}

#[test]
fn server_guard_joins_after_assertion_panic() {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
        .unwrap();
    runtime.block_on(async {
        let server = super::spawn_server_ready(
            camber::http::Router::new(),
            std::time::Duration::from_secs(1),
        )
        .unwrap();
        let cleanup = server.cleanup_probe();
        let unwind = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _server = server;
            assert_eq!(1, 2, "intentional server fixture assertion failure");
        }));
        assert!(unwind.is_err());
        assert!(cleanup.joined());
        assert_eq!(cleanup.cleanup_error(), None);
    });
}

#[test]
fn child_guard_reaps_after_normal_success() {
    const MODE: &str = "normal-cleanup";
    // The markers the child writes and the parent searches for, spelled once:
    // a hand-counted window length is a second spelling that drifts the moment
    // a marker is renamed. Spelled as text, because the line wait takes text
    // and only the byte searches need the free conversion back.
    const COMPLETE: &str = "NORMAL_COMPLETE";
    const STDERR: &str = "NORMAL_STDERR";
    if super::is_private_child(MODE) {
        std::io::Write::write_all(&mut std::io::stdout(), COMPLETE.as_bytes()).unwrap();
        std::io::Write::write_all(&mut std::io::stdout(), b"\n").unwrap();
        std::io::Write::flush(&mut std::io::stdout()).unwrap();
        std::io::Write::write_all(&mut std::io::stderr(), STDERR.as_bytes()).unwrap();
        std::io::Write::write_all(&mut std::io::stderr(), b"\n").unwrap();
        std::io::Write::flush(&mut std::io::stderr()).unwrap();
        return;
    }
    let mut child = super::ChildGuard::spawn_exact_current(
        "fixture_contracts::child_guard_reaps_after_normal_success",
        MODE,
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    let probe = child.take_reap_probe().unwrap();
    child
        .wait_for_line(COMPLETE, std::time::Duration::from_secs(2))
        .unwrap();
    assert!(
        child
            .wait_bounded(std::time::Duration::from_secs(2))
            .unwrap()
            .success()
    );
    assert!(
        child
            .stdout()
            .windows(COMPLETE.len())
            .any(|bytes| bytes == COMPLETE.as_bytes())
    );
    assert!(
        child
            .stderr()
            .windows(STDERR.len())
            .any(|bytes| bytes == STDERR.as_bytes())
    );
    assert!(
        probe
            .wait(std::time::Duration::from_secs(2))
            .unwrap()
            .status()
            .success()
    );
}

#[test]
fn child_guard_reaps_after_child_timeout() {
    const MODE: &str = "child-timeout";
    if hold_until_parent_kills(MODE, "CHILD_HOLDING") {
        return;
    }
    let mut child = super::ChildGuard::spawn_exact_current(
        "fixture_contracts::child_guard_reaps_after_child_timeout",
        MODE,
        std::time::Duration::from_secs(2),
    )
    .unwrap();
    let probe = child.take_reap_probe().unwrap();
    child
        .wait_for_line("CHILD_HOLDING", std::time::Duration::from_secs(2))
        .unwrap();
    assert!(matches!(
        child.wait_bounded(std::time::Duration::ZERO),
        Err(super::ProcessError::ExitTimeout { .. })
    ));
    assert!(
        !probe
            .wait(std::time::Duration::from_secs(2))
            .unwrap()
            .status()
            .success()
    );
}

#[test]
fn temp_root_cleans_up_during_assertion_unwind() {
    let root = super::TempRoot::new().unwrap();
    let path = root.path().to_owned();
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _root = root;
        assert_eq!(1, 2, "intentional temporary-root assertion failure");
    }));
    assert!(result.is_err());
    assert!(!path.exists());
}

#[test]
fn external_resource_names_are_unique_and_safe() {
    let first = super::unique_external_name("Camber Fixture");
    let second = super::unique_external_name("Camber Fixture");
    assert_ne!(first, second);
    assert!(first.starts_with("camber-fixture-"));
    assert!(first.chars().all(|character| character.is_ascii_lowercase()
        || character.is_ascii_digit()
        || character == '-'));
}

#[test]
fn capture_bus_records_only_the_needle_that_asked() {
    const WATCHED: &str = "camber-capture-contract-watched";
    const UNWATCHED: &str = "camber-capture-contract-unwatched";
    const REASON: &str = "capture-bus-contract";

    let watched = super::capture_events(WATCHED);
    let unwatched = super::capture_events(UNWATCHED);
    camber::tracing::info!(subject = WATCHED, reason = REASON);

    // Both halves matter: a bus that recorded nothing and a bus that recorded
    // everything both leave a needle-scoped assertion looking healthy on its
    // own, and only the pair separates them.
    assert!(
        watched.recorded(&[WATCHED, REASON]),
        "the capture bus did not record the event naming its needle"
    );
    assert_eq!(
        unwatched.len(),
        0,
        "the capture bus recorded an event that never named this needle"
    );
}

#[test]
fn capture_bus_unsubscribes_when_its_handle_drops() {
    const NEEDLE: &str = "camber-capture-contract-dropped";

    assert_eq!(super::captures_for(NEEDLE), 0);
    let capture = super::capture_events(NEEDLE);
    assert_eq!(super::captures_for(NEEDLE), 1);
    drop(capture);

    // A subscription outliving its handle would keep pushing into a buffer no
    // test reads, and grow the fan-out every capture pays for.
    assert_eq!(
        super::captures_for(NEEDLE),
        0,
        "the capture bus kept a subscription after its handle dropped"
    );
}

#[test]
fn read_delimited_frames_a_split_delimiter() {
    const HEAD: &[u8] = b"HTTP/1.1 200 OK\r\nX-Fixture: framed\r";
    const TAIL: &[u8] = b"\n\r\nTRAILING-BODY";
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let address = listener.local_addr().unwrap();

    let mut server = Some(std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        stream.set_nodelay(true).unwrap();
        std::io::Write::write_all(&mut stream, HEAD).unwrap();
        std::io::Write::flush(&mut stream).unwrap();
        // Two writes, with the delimiter split between them. No pause is needed
        // to make the reader resume across that split: it reads one byte per
        // syscall, so the overlap scan runs on every byte of every run whatever
        // TCP does with the segment boundary.
        std::io::Write::write_all(&mut stream, TAIL).unwrap();
        std::io::Write::flush(&mut stream).unwrap();
    }));

    let mut client = std::net::TcpStream::connect(address).unwrap();
    let framed = super::read_delimited(
        &mut client,
        b"\r\n\r\n",
        1024,
        std::time::Duration::from_secs(2),
    )
    .unwrap();

    assert_eq!(
        framed.as_ref(),
        b"HTTP/1.1 200 OK\r\nX-Fixture: framed\r\n\r\n"
    );
    // Framed, not drained: a reader that consumed past its delimiter would
    // leave the next read of this connection short.
    let rest = super::bounded_read(&mut client, std::time::Duration::from_secs(2), 1024).unwrap();
    assert_eq!(rest.as_ref(), b"TRAILING-BODY");
    super::join_thread_bounded(&mut server, std::time::Duration::from_secs(2)).unwrap();
}

#[test]
fn read_delimited_bounds_a_dribbling_peer_by_deadline() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let address = listener.local_addr().unwrap();
    let (stop_sender, stop_receiver) = std::sync::mpsc::channel::<()>();

    // One byte at a time, each inside the reader's bound. A reader that gave
    // every byte a fresh full timeout would never expire against this peer.
    let mut server = Some(std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        stream.set_nodelay(true).unwrap();
        while stop_receiver.try_recv().is_err() {
            match std::io::Write::write_all(&mut stream, b"x") {
                Ok(()) => std::thread::sleep(std::time::Duration::from_millis(10)),
                Err(_) => return,
            }
        }
    }));

    let mut client = std::net::TcpStream::connect(address).unwrap();
    let started = std::time::Instant::now();
    let result = super::read_delimited(
        &mut client,
        b"\r\n\r\n",
        64 * 1024,
        std::time::Duration::from_millis(200),
    );

    // The kind, not just the failure: a reset peer, a size limit, and an early
    // EOF all fail a test named for the deadline while proving nothing about it.
    let error = result.expect_err("a peer that never sent the delimiter produced a frame anyway");
    assert_eq!(
        error.kind(),
        std::io::ErrorKind::TimedOut,
        "the framed read failed for a reason other than its own deadline: {error}"
    );
    assert!(
        started.elapsed() < std::time::Duration::from_secs(2),
        "the framed read outlived its own deadline"
    );
    stop_sender.send(()).unwrap();
    drop(client);
    super::join_thread_bounded(&mut server, std::time::Duration::from_secs(2)).unwrap();
}

/// Hold this process open until the parent kills it, reporting whether this
/// process was that child.
///
/// The parent's subject in every one of these cases is the kill: each proves the
/// guard reaps a child that will not exit on its own. So the child prints
/// `marker` for the parent's line wait, flushes it — a marker left in the buffer
/// reads as a child that never got there — and then parks on a channel whose
/// sender it holds itself, so only the parent can end it. Reaching the bound
/// means the parent never did, which is the fault under test.
fn hold_until_parent_kills(mode: &str, marker: &str) -> bool {
    if !super::is_private_child(mode) {
        return false;
    }
    println!("{marker}");
    std::io::Write::flush(&mut std::io::stdout())
        .expect("the held child could not flush its marker");
    let (_sender, receiver) = std::sync::mpsc::channel::<()>();
    let result = receiver.recv_timeout(std::time::Duration::from_secs(30));
    assert!(result.is_ok(), "fixture parent did not terminate child");
    true
}

fn assert_output_once(run: &super::IsolatedRun, expected: &str) {
    let count = String::from_utf8_lossy(run.stdout())
        .matches(expected)
        .count()
        + String::from_utf8_lossy(run.stderr())
            .matches(expected)
            .count();
    assert_eq!(count, 1, "expected one child marker for {expected:?}");
}