forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

worker_panic.rs87 lines, 3.1 KiBHistoryRawDownload
1//! Regression for the threaded decode (lane-av1panichang): a worker that
2//! panics must end the decode with an error, never hang it.
3//!
4//! Every worker site the threaded decoder waits on is armed in turn through
5//! `ec_av1::fault`: the frame job itself (inside and past its own catch). Each
6//! decode runs on a helper thread and must answer within 60 s; the answer must
7//! be an error that names the panic. A second decode in the same process must
8//! then succeed.
9
10use ec_av1::fault;
11use ec_av1::stream::decode_stream_with_threads;
12use std::sync::Mutex;
13use std::sync::mpsc;
14use std::time::Duration;
15
16/// The fault plan is process-global, so the tests in this binary take turns.
17static SERIAL: Mutex<()> = Mutex::new(());
18
19const BUDGET: Duration = Duration::from_secs(60);
20
21fn fixture(name: &str) -> Vec<u8> {
22 let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
23 .join("fixtures")
24 .join(name);
25 std::fs::read(&path).unwrap_or_else(|e| panic!("reading {}: {e}", path.display()))
26}
27
28/// Decodes `data` on a helper thread and returns the shown-picture count, or
29/// the error text. Panics (failing the test) if no answer arrives in time.
30fn decode_within_budget(data: Vec<u8>, threads: usize) -> Result<usize, String> {
31 let (tx, rx) = mpsc::channel();
32 std::thread::spawn(move || {
33 let mut shown = 0usize;
34 let r = decode_stream_with_threads(&data, threads, |_picture, _idx, _shown| {
35 shown += 1;
36 Ok(())
37 });
38 let _ = tx.send(r.map(|()| shown).map_err(|e| e.to_string()));
39 });
40 rx.recv_timeout(BUDGET)
41 .unwrap_or_else(|_| panic!("the decode did not answer within {BUDGET:?}: it hung"))
42}
43
44#[test]
45fn a_worker_panic_ends_the_decode_with_an_error_at_every_site() {
46 let _guard = SERIAL
47 .lock()
48 .unwrap_or_else(std::sync::PoisonError::into_inner);
49 let stream = fixture("reelb_arf_witness.obu");
50 // (site, which hit of that site to panic on)
51 for (site, nth) in [
52 ("frame", 5),
53 ("frame-after", 5),
54 ("recon", 3),
55 ] {
56 fault::arm(site, nth);
57 let got = decode_within_budget(stream.clone(), 4);
58 fault::disarm();
59 let err = got.expect_err(&format!(
60 "site `{site}` (hit {nth}): the decode should fail"
61 ));
62 assert!(
63 err.contains("panicked"),
64 "site `{site}`: the error should name the worker panic, got: {err}"
65 );
66 }
67}
68
69#[test]
70fn a_decode_after_a_worker_panic_still_works() {
71 let _guard = SERIAL
72 .lock()
73 .unwrap_or_else(std::sync::PoisonError::into_inner);
74 let stream = fixture("reelb_arf_witness.obu");
75 let serial = decode_within_budget(stream.clone(), 1).expect("serial decode");
76
77 fault::arm("frame", 3);
78 let failed = decode_within_budget(stream.clone(), 4);
79 fault::disarm();
80 assert!(failed.is_err(), "the armed decode should fail");
81
82 let again = decode_within_budget(stream, 4).expect("a decode after the panic must succeed");
83 assert_eq!(
84 again, serial,
85 "the post-panic decode shows as many pictures"
86 );
87}