]> git.lizzy.rs Git - rust.git/blob - library/test/src/lib.rs
Rollup merge of #102578 - lukas-code:ilog-panic, r=m-ou-se
[rust.git] / library / test / src / lib.rs
1 //! Support code for rustc's built in unit-test and micro-benchmarking
2 //! framework.
3 //!
4 //! Almost all user code will only be interested in `Bencher` and
5 //! `black_box`. All other interactions (such as writing tests and
6 //! benchmarks themselves) should be done via the `#[test]` and
7 //! `#[bench]` attributes.
8 //!
9 //! See the [Testing Chapter](../book/ch11-00-testing.html) of the book for more
10 //! details.
11
12 // Currently, not much of this is meant for users. It is intended to
13 // support the simplest interface possible for representing and
14 // running tests while providing a base that other test frameworks may
15 // build off of.
16
17 #![unstable(feature = "test", issue = "50297")]
18 #![doc(test(attr(deny(warnings))))]
19 #![feature(internal_output_capture)]
20 #![feature(staged_api)]
21 #![feature(process_exitcode_internals)]
22 #![feature(test)]
23
24 // Public reexports
25 pub use self::bench::{black_box, Bencher};
26 pub use self::console::run_tests_console;
27 pub use self::options::{ColorConfig, Options, OutputFormat, RunIgnored, ShouldPanic};
28 pub use self::types::TestName::*;
29 pub use self::types::*;
30 pub use self::ColorConfig::*;
31 pub use cli::TestOpts;
32
33 // Module to be used by rustc to compile tests in libtest
34 pub mod test {
35     pub use crate::{
36         assert_test_result,
37         bench::Bencher,
38         cli::{parse_opts, TestOpts},
39         filter_tests,
40         helpers::metrics::{Metric, MetricMap},
41         options::{Concurrent, Options, RunIgnored, RunStrategy, ShouldPanic},
42         run_test, test_main, test_main_static,
43         test_result::{TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk},
44         time::{TestExecTime, TestTimeOptions},
45         types::{
46             DynTestFn, DynTestName, StaticBenchFn, StaticTestFn, StaticTestName, TestDesc,
47             TestDescAndFn, TestId, TestName, TestType,
48         },
49     };
50 }
51
52 use std::{
53     collections::VecDeque,
54     env, io,
55     io::prelude::Write,
56     panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo},
57     process::{self, Command, Termination},
58     sync::mpsc::{channel, Sender},
59     sync::{Arc, Mutex},
60     thread,
61     time::{Duration, Instant},
62 };
63
64 pub mod bench;
65 mod cli;
66 mod console;
67 mod event;
68 mod formatters;
69 mod helpers;
70 mod options;
71 pub mod stats;
72 mod term;
73 mod test_result;
74 mod time;
75 mod types;
76
77 #[cfg(test)]
78 mod tests;
79
80 use core::any::Any;
81 use event::{CompletedTest, TestEvent};
82 use helpers::concurrency::get_concurrency;
83 use helpers::exit_code::get_exit_code;
84 use helpers::shuffle::{get_shuffle_seed, shuffle_tests};
85 use options::{Concurrent, RunStrategy};
86 use test_result::*;
87 use time::TestExecTime;
88
89 // Process exit code to be used to indicate test failures.
90 const ERROR_EXIT_CODE: i32 = 101;
91
92 const SECONDARY_TEST_INVOKER_VAR: &str = "__RUST_TEST_INVOKE";
93
94 // The default console test runner. It accepts the command line
95 // arguments and a vector of test_descs.
96 pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Options>) {
97     let mut opts = match cli::parse_opts(args) {
98         Some(Ok(o)) => o,
99         Some(Err(msg)) => {
100             eprintln!("error: {msg}");
101             process::exit(ERROR_EXIT_CODE);
102         }
103         None => return,
104     };
105     if let Some(options) = options {
106         opts.options = options;
107     }
108     if opts.list {
109         if let Err(e) = console::list_tests_console(&opts, tests) {
110             eprintln!("error: io error when listing tests: {e:?}");
111             process::exit(ERROR_EXIT_CODE);
112         }
113     } else {
114         match console::run_tests_console(&opts, tests) {
115             Ok(true) => {}
116             Ok(false) => process::exit(ERROR_EXIT_CODE),
117             Err(e) => {
118                 eprintln!("error: io error when listing tests: {e:?}");
119                 process::exit(ERROR_EXIT_CODE);
120             }
121         }
122     }
123 }
124
125 /// A variant optimized for invocation with a static test vector.
126 /// This will panic (intentionally) when fed any dynamic tests.
127 ///
128 /// This is the entry point for the main function generated by `rustc --test`
129 /// when panic=unwind.
130 pub fn test_main_static(tests: &[&TestDescAndFn]) {
131     let args = env::args().collect::<Vec<_>>();
132     let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
133     test_main(&args, owned_tests, None)
134 }
135
136 /// A variant optimized for invocation with a static test vector.
137 /// This will panic (intentionally) when fed any dynamic tests.
138 ///
139 /// Runs tests in panic=abort mode, which involves spawning subprocesses for
140 /// tests.
141 ///
142 /// This is the entry point for the main function generated by `rustc --test`
143 /// when panic=abort.
144 pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
145     // If we're being run in SpawnedSecondary mode, run the test here. run_test
146     // will then exit the process.
147     if let Ok(name) = env::var(SECONDARY_TEST_INVOKER_VAR) {
148         env::remove_var(SECONDARY_TEST_INVOKER_VAR);
149         let test = tests
150             .iter()
151             .filter(|test| test.desc.name.as_slice() == name)
152             .map(make_owned_test)
153             .next()
154             .unwrap_or_else(|| panic!("couldn't find a test with the provided name '{name}'"));
155         let TestDescAndFn { desc, testfn } = test;
156         let testfn = match testfn {
157             StaticTestFn(f) => f,
158             _ => panic!("only static tests are supported"),
159         };
160         run_test_in_spawned_subprocess(desc, Box::new(testfn));
161     }
162
163     let args = env::args().collect::<Vec<_>>();
164     let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
165     test_main(&args, owned_tests, Some(Options::new().panic_abort(true)))
166 }
167
168 /// Clones static values for putting into a dynamic vector, which test_main()
169 /// needs to hand out ownership of tests to parallel test runners.
170 ///
171 /// This will panic when fed any dynamic tests, because they cannot be cloned.
172 fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
173     match test.testfn {
174         StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: test.desc.clone() },
175         StaticBenchFn(f) => TestDescAndFn { testfn: StaticBenchFn(f), desc: test.desc.clone() },
176         _ => panic!("non-static tests passed to test::test_main_static"),
177     }
178 }
179
180 /// Invoked when unit tests terminate. Returns `Result::Err` if the test is
181 /// considered a failure. By default, invokes `report() and checks for a `0`
182 /// result.
183 pub fn assert_test_result<T: Termination>(result: T) -> Result<(), String> {
184     let code = result.report().to_i32();
185     if code == 0 {
186         Ok(())
187     } else {
188         Err(format!(
189             "the test returned a termination value with a non-zero status code \
190              ({}) which indicates a failure",
191             code
192         ))
193     }
194 }
195
196 pub fn run_tests<F>(
197     opts: &TestOpts,
198     tests: Vec<TestDescAndFn>,
199     mut notify_about_test_event: F,
200 ) -> io::Result<()>
201 where
202     F: FnMut(TestEvent) -> io::Result<()>,
203 {
204     use std::collections::{self, HashMap};
205     use std::hash::BuildHasherDefault;
206     use std::sync::mpsc::RecvTimeoutError;
207
208     struct RunningTest {
209         join_handle: Option<thread::JoinHandle<()>>,
210     }
211
212     // Use a deterministic hasher
213     type TestMap =
214         HashMap<TestId, RunningTest, BuildHasherDefault<collections::hash_map::DefaultHasher>>;
215
216     struct TimeoutEntry {
217         id: TestId,
218         desc: TestDesc,
219         timeout: Instant,
220     }
221
222     let tests_len = tests.len();
223
224     let mut filtered_tests = filter_tests(opts, tests);
225     if !opts.bench_benchmarks {
226         filtered_tests = convert_benchmarks_to_tests(filtered_tests);
227     }
228
229     let filtered_tests = {
230         let mut filtered_tests = filtered_tests;
231         for test in filtered_tests.iter_mut() {
232             test.desc.name = test.desc.name.with_padding(test.testfn.padding());
233         }
234
235         filtered_tests
236     };
237
238     let filtered_out = tests_len - filtered_tests.len();
239     let event = TestEvent::TeFilteredOut(filtered_out);
240     notify_about_test_event(event)?;
241
242     let filtered_descs = filtered_tests.iter().map(|t| t.desc.clone()).collect();
243
244     let shuffle_seed = get_shuffle_seed(opts);
245
246     let event = TestEvent::TeFiltered(filtered_descs, shuffle_seed);
247     notify_about_test_event(event)?;
248
249     let (filtered_tests, filtered_benchs): (Vec<_>, _) = filtered_tests
250         .into_iter()
251         .enumerate()
252         .map(|(i, e)| (TestId(i), e))
253         .partition(|(_, e)| matches!(e.testfn, StaticTestFn(_) | DynTestFn(_)));
254
255     let concurrency = opts.test_threads.unwrap_or_else(get_concurrency);
256
257     let mut remaining = filtered_tests;
258     if let Some(shuffle_seed) = shuffle_seed {
259         shuffle_tests(shuffle_seed, &mut remaining);
260     } else {
261         remaining.reverse();
262     }
263     let mut pending = 0;
264
265     let (tx, rx) = channel::<CompletedTest>();
266     let run_strategy = if opts.options.panic_abort && !opts.force_run_in_process {
267         RunStrategy::SpawnPrimary
268     } else {
269         RunStrategy::InProcess
270     };
271
272     let mut running_tests: TestMap = HashMap::default();
273     let mut timeout_queue: VecDeque<TimeoutEntry> = VecDeque::new();
274
275     fn get_timed_out_tests(
276         running_tests: &TestMap,
277         timeout_queue: &mut VecDeque<TimeoutEntry>,
278     ) -> Vec<TestDesc> {
279         let now = Instant::now();
280         let mut timed_out = Vec::new();
281         while let Some(timeout_entry) = timeout_queue.front() {
282             if now < timeout_entry.timeout {
283                 break;
284             }
285             let timeout_entry = timeout_queue.pop_front().unwrap();
286             if running_tests.contains_key(&timeout_entry.id) {
287                 timed_out.push(timeout_entry.desc);
288             }
289         }
290         timed_out
291     }
292
293     fn calc_timeout(timeout_queue: &VecDeque<TimeoutEntry>) -> Option<Duration> {
294         timeout_queue.front().map(|&TimeoutEntry { timeout: next_timeout, .. }| {
295             let now = Instant::now();
296             if next_timeout >= now { next_timeout - now } else { Duration::new(0, 0) }
297         })
298     }
299
300     if concurrency == 1 {
301         while !remaining.is_empty() {
302             let (id, test) = remaining.pop().unwrap();
303             let event = TestEvent::TeWait(test.desc.clone());
304             notify_about_test_event(event)?;
305             let join_handle =
306                 run_test(opts, !opts.run_tests, id, test, run_strategy, tx.clone(), Concurrent::No);
307             assert!(join_handle.is_none());
308             let completed_test = rx.recv().unwrap();
309
310             let event = TestEvent::TeResult(completed_test);
311             notify_about_test_event(event)?;
312         }
313     } else {
314         while pending > 0 || !remaining.is_empty() {
315             while pending < concurrency && !remaining.is_empty() {
316                 let (id, test) = remaining.pop().unwrap();
317                 let timeout = time::get_default_test_timeout();
318                 let desc = test.desc.clone();
319
320                 let event = TestEvent::TeWait(desc.clone());
321                 notify_about_test_event(event)?; //here no pad
322                 let join_handle = run_test(
323                     opts,
324                     !opts.run_tests,
325                     id,
326                     test,
327                     run_strategy,
328                     tx.clone(),
329                     Concurrent::Yes,
330                 );
331                 running_tests.insert(id, RunningTest { join_handle });
332                 timeout_queue.push_back(TimeoutEntry { id, desc, timeout });
333                 pending += 1;
334             }
335
336             let mut res;
337             loop {
338                 if let Some(timeout) = calc_timeout(&timeout_queue) {
339                     res = rx.recv_timeout(timeout);
340                     for test in get_timed_out_tests(&running_tests, &mut timeout_queue) {
341                         let event = TestEvent::TeTimeout(test);
342                         notify_about_test_event(event)?;
343                     }
344
345                     match res {
346                         Err(RecvTimeoutError::Timeout) => {
347                             // Result is not yet ready, continue waiting.
348                         }
349                         _ => {
350                             // We've got a result, stop the loop.
351                             break;
352                         }
353                     }
354                 } else {
355                     res = rx.recv().map_err(|_| RecvTimeoutError::Disconnected);
356                     break;
357                 }
358             }
359
360             let mut completed_test = res.unwrap();
361             let running_test = running_tests.remove(&completed_test.id).unwrap();
362             if let Some(join_handle) = running_test.join_handle {
363                 if let Err(_) = join_handle.join() {
364                     if let TrOk = completed_test.result {
365                         completed_test.result =
366                             TrFailedMsg("panicked after reporting success".to_string());
367                     }
368                 }
369             }
370
371             let event = TestEvent::TeResult(completed_test);
372             notify_about_test_event(event)?;
373             pending -= 1;
374         }
375     }
376
377     if opts.bench_benchmarks {
378         // All benchmarks run at the end, in serial.
379         for (id, b) in filtered_benchs {
380             let event = TestEvent::TeWait(b.desc.clone());
381             notify_about_test_event(event)?;
382             run_test(opts, false, id, b, run_strategy, tx.clone(), Concurrent::No);
383             let completed_test = rx.recv().unwrap();
384
385             let event = TestEvent::TeResult(completed_test);
386             notify_about_test_event(event)?;
387         }
388     }
389     Ok(())
390 }
391
392 pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
393     let mut filtered = tests;
394     let matches_filter = |test: &TestDescAndFn, filter: &str| {
395         let test_name = test.desc.name.as_slice();
396
397         match opts.filter_exact {
398             true => test_name == filter,
399             false => test_name.contains(filter),
400         }
401     };
402
403     // Remove tests that don't match the test filter
404     if !opts.filters.is_empty() {
405         filtered.retain(|test| opts.filters.iter().any(|filter| matches_filter(test, filter)));
406     }
407
408     // Skip tests that match any of the skip filters
409     filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
410
411     // Excludes #[should_panic] tests
412     if opts.exclude_should_panic {
413         filtered.retain(|test| test.desc.should_panic == ShouldPanic::No);
414     }
415
416     // maybe unignore tests
417     match opts.run_ignored {
418         RunIgnored::Yes => {
419             filtered.iter_mut().for_each(|test| test.desc.ignore = false);
420         }
421         RunIgnored::Only => {
422             filtered.retain(|test| test.desc.ignore);
423             filtered.iter_mut().for_each(|test| test.desc.ignore = false);
424         }
425         RunIgnored::No => {}
426     }
427
428     // Sort the tests alphabetically
429     filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));
430
431     filtered
432 }
433
434 pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
435     // convert benchmarks to tests, if we're not benchmarking them
436     tests
437         .into_iter()
438         .map(|x| {
439             let testfn = match x.testfn {
440                 DynBenchFn(benchfn) => DynTestFn(Box::new(move || {
441                     bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
442                 })),
443                 StaticBenchFn(benchfn) => DynTestFn(Box::new(move || {
444                     bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
445                 })),
446                 f => f,
447             };
448             TestDescAndFn { desc: x.desc, testfn }
449         })
450         .collect()
451 }
452
453 pub fn run_test(
454     opts: &TestOpts,
455     force_ignore: bool,
456     id: TestId,
457     test: TestDescAndFn,
458     strategy: RunStrategy,
459     monitor_ch: Sender<CompletedTest>,
460     concurrency: Concurrent,
461 ) -> Option<thread::JoinHandle<()>> {
462     let TestDescAndFn { desc, testfn } = test;
463
464     // Emscripten can catch panics but other wasm targets cannot
465     let ignore_because_no_process_support = desc.should_panic != ShouldPanic::No
466         && cfg!(target_family = "wasm")
467         && !cfg!(target_os = "emscripten");
468
469     if force_ignore || desc.ignore || ignore_because_no_process_support {
470         let message = CompletedTest::new(id, desc, TrIgnored, None, Vec::new());
471         monitor_ch.send(message).unwrap();
472         return None;
473     }
474
475     struct TestRunOpts {
476         pub strategy: RunStrategy,
477         pub nocapture: bool,
478         pub concurrency: Concurrent,
479         pub time: Option<time::TestTimeOptions>,
480     }
481
482     fn run_test_inner(
483         id: TestId,
484         desc: TestDesc,
485         monitor_ch: Sender<CompletedTest>,
486         testfn: Box<dyn FnOnce() -> Result<(), String> + Send>,
487         opts: TestRunOpts,
488     ) -> Option<thread::JoinHandle<()>> {
489         let concurrency = opts.concurrency;
490         let name = desc.name.clone();
491
492         let runtest = move || match opts.strategy {
493             RunStrategy::InProcess => run_test_in_process(
494                 id,
495                 desc,
496                 opts.nocapture,
497                 opts.time.is_some(),
498                 testfn,
499                 monitor_ch,
500                 opts.time,
501             ),
502             RunStrategy::SpawnPrimary => spawn_test_subprocess(
503                 id,
504                 desc,
505                 opts.nocapture,
506                 opts.time.is_some(),
507                 monitor_ch,
508                 opts.time,
509             ),
510         };
511
512         // If the platform is single-threaded we're just going to run
513         // the test synchronously, regardless of the concurrency
514         // level.
515         let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_family = "wasm");
516         if concurrency == Concurrent::Yes && supports_threads {
517             let cfg = thread::Builder::new().name(name.as_slice().to_owned());
518             let mut runtest = Arc::new(Mutex::new(Some(runtest)));
519             let runtest2 = runtest.clone();
520             match cfg.spawn(move || runtest2.lock().unwrap().take().unwrap()()) {
521                 Ok(handle) => Some(handle),
522                 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
523                     // `ErrorKind::WouldBlock` means hitting the thread limit on some
524                     // platforms, so run the test synchronously here instead.
525                     Arc::get_mut(&mut runtest).unwrap().get_mut().unwrap().take().unwrap()();
526                     None
527                 }
528                 Err(e) => panic!("failed to spawn thread to run test: {e}"),
529             }
530         } else {
531             runtest();
532             None
533         }
534     }
535
536     let test_run_opts =
537         TestRunOpts { strategy, nocapture: opts.nocapture, concurrency, time: opts.time_options };
538
539     match testfn {
540         DynBenchFn(benchfn) => {
541             // Benchmarks aren't expected to panic, so we run them all in-process.
542             crate::bench::benchmark(id, desc, monitor_ch, opts.nocapture, benchfn);
543             None
544         }
545         StaticBenchFn(benchfn) => {
546             // Benchmarks aren't expected to panic, so we run them all in-process.
547             crate::bench::benchmark(id, desc, monitor_ch, opts.nocapture, benchfn);
548             None
549         }
550         DynTestFn(f) => {
551             match strategy {
552                 RunStrategy::InProcess => (),
553                 _ => panic!("Cannot run dynamic test fn out-of-process"),
554             };
555             run_test_inner(
556                 id,
557                 desc,
558                 monitor_ch,
559                 Box::new(move || __rust_begin_short_backtrace(f)),
560                 test_run_opts,
561             )
562         }
563         StaticTestFn(f) => run_test_inner(
564             id,
565             desc,
566             monitor_ch,
567             Box::new(move || __rust_begin_short_backtrace(f)),
568             test_run_opts,
569         ),
570     }
571 }
572
573 /// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
574 #[inline(never)]
575 fn __rust_begin_short_backtrace<T, F: FnOnce() -> T>(f: F) -> T {
576     let result = f();
577
578     // prevent this frame from being tail-call optimised away
579     black_box(result)
580 }
581
582 fn run_test_in_process(
583     id: TestId,
584     desc: TestDesc,
585     nocapture: bool,
586     report_time: bool,
587     testfn: Box<dyn FnOnce() -> Result<(), String> + Send>,
588     monitor_ch: Sender<CompletedTest>,
589     time_opts: Option<time::TestTimeOptions>,
590 ) {
591     // Buffer for capturing standard I/O
592     let data = Arc::new(Mutex::new(Vec::new()));
593
594     if !nocapture {
595         io::set_output_capture(Some(data.clone()));
596     }
597
598     let start = report_time.then(Instant::now);
599     let result = fold_err(catch_unwind(AssertUnwindSafe(testfn)));
600     let exec_time = start.map(|start| {
601         let duration = start.elapsed();
602         TestExecTime(duration)
603     });
604
605     io::set_output_capture(None);
606
607     let test_result = match result {
608         Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time),
609         Err(e) => calc_result(&desc, Err(e.as_ref()), &time_opts, &exec_time),
610     };
611     let stdout = data.lock().unwrap_or_else(|e| e.into_inner()).to_vec();
612     let message = CompletedTest::new(id, desc, test_result, exec_time, stdout);
613     monitor_ch.send(message).unwrap();
614 }
615
616 fn fold_err<T, E>(
617     result: Result<Result<T, E>, Box<dyn Any + Send>>,
618 ) -> Result<T, Box<dyn Any + Send>>
619 where
620     E: Send + 'static,
621 {
622     match result {
623         Ok(Err(e)) => Err(Box::new(e)),
624         Ok(Ok(v)) => Ok(v),
625         Err(e) => Err(e),
626     }
627 }
628
629 fn spawn_test_subprocess(
630     id: TestId,
631     desc: TestDesc,
632     nocapture: bool,
633     report_time: bool,
634     monitor_ch: Sender<CompletedTest>,
635     time_opts: Option<time::TestTimeOptions>,
636 ) {
637     let (result, test_output, exec_time) = (|| {
638         let args = env::args().collect::<Vec<_>>();
639         let current_exe = &args[0];
640
641         let mut command = Command::new(current_exe);
642         command.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice());
643         if nocapture {
644             command.stdout(process::Stdio::inherit());
645             command.stderr(process::Stdio::inherit());
646         }
647
648         let start = report_time.then(Instant::now);
649         let output = match command.output() {
650             Ok(out) => out,
651             Err(e) => {
652                 let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e);
653                 return (TrFailed, err.into_bytes(), None);
654             }
655         };
656         let exec_time = start.map(|start| {
657             let duration = start.elapsed();
658             TestExecTime(duration)
659         });
660
661         let std::process::Output { stdout, stderr, status } = output;
662         let mut test_output = stdout;
663         formatters::write_stderr_delimiter(&mut test_output, &desc.name);
664         test_output.extend_from_slice(&stderr);
665
666         let result = match (|| -> Result<TestResult, String> {
667             let exit_code = get_exit_code(status)?;
668             Ok(get_result_from_exit_code(&desc, exit_code, &time_opts, &exec_time))
669         })() {
670             Ok(r) => r,
671             Err(e) => {
672                 write!(&mut test_output, "Unexpected error: {}", e).unwrap();
673                 TrFailed
674             }
675         };
676
677         (result, test_output, exec_time)
678     })();
679
680     let message = CompletedTest::new(id, desc, result, exec_time, test_output);
681     monitor_ch.send(message).unwrap();
682 }
683
684 fn run_test_in_spawned_subprocess(
685     desc: TestDesc,
686     testfn: Box<dyn FnOnce() -> Result<(), String> + Send>,
687 ) -> ! {
688     let builtin_panic_hook = panic::take_hook();
689     let record_result = Arc::new(move |panic_info: Option<&'_ PanicInfo<'_>>| {
690         let test_result = match panic_info {
691             Some(info) => calc_result(&desc, Err(info.payload()), &None, &None),
692             None => calc_result(&desc, Ok(()), &None, &None),
693         };
694
695         // We don't support serializing TrFailedMsg, so just
696         // print the message out to stderr.
697         if let TrFailedMsg(msg) = &test_result {
698             eprintln!("{msg}");
699         }
700
701         if let Some(info) = panic_info {
702             builtin_panic_hook(info);
703         }
704
705         if let TrOk = test_result {
706             process::exit(test_result::TR_OK);
707         } else {
708             process::exit(test_result::TR_FAILED);
709         }
710     });
711     let record_result2 = record_result.clone();
712     panic::set_hook(Box::new(move |info| record_result2(Some(&info))));
713     if let Err(message) = testfn() {
714         panic!("{}", message);
715     }
716     record_result(None);
717     unreachable!("panic=abort callback should have exited the process")
718 }