Skip to content

Commit 199a60a

Browse files
committed
sim: try to fix the stdout/err issue with the test runner func, unsuccessfully
The contents of this commit have a good account of the things that were tried. The short version is that there isn't much we can do until rust-lang/rust#42474 is fixed (and ← seems to be blocked on custom test frameworks).
1 parent 80cccb5 commit 199a60a

File tree

1 file changed

+47
-0
lines changed
  • baseline-sim/tests/test_infrastructure

1 file changed

+47
-0
lines changed

baseline-sim/tests/test_infrastructure/misc.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,58 @@ use std::thread;
44

55
const STACK_SIZE: usize = 32 * 1024 * 1024;
66

7+
// TODO, low priority: this would be nice to turn into an attribute we can just
8+
// stick on test functions:
9+
// ```rust
10+
// #[test]
11+
// #[with_larger_stack]
12+
// fn foo() { ... }
13+
// ```
714
pub fn with_larger_stack<F: FnOnce() + Send + 'static>(n: Option<String>, f: F) {
815
let child = thread::Builder::new()
916
.stack_size(STACK_SIZE)
17+
.name(n.unwrap_or_else(||
18+
thread::current()
19+
.name()
20+
.map(String::from)
21+
.unwrap_or(String::from("Test Thread"))))
1022
.spawn(f)
1123
.unwrap();
1224

25+
// Note: this still produces errors even when functions are marked with
26+
// `#[should_panic]` but it actually has nothing to do with the panic
27+
// machinery; it's because threads don't propogate their stdout/stderrs to
28+
// spawned threads. Here's a gh issue about it:
29+
// https://github.com/rust-lang/rust/issues/42474
30+
//
31+
// I've tried:
32+
// - Using the forbidden `set_panic` and `set_print` that libtest uses:
33+
// * std::io::set_panic(Some(Box::new(stderr)));
34+
// * std::io::set_print(Some(Box::new(stdout)));
35+
// Which inexplicably doesn't work.
36+
// - Stealing the parent thread's panic handler:
37+
// * std::panic::set_hook({std::panic::take_hook() (in parent)});
38+
// Which understandably doesn't work since the panics are actually being
39+
// propagated just fine.
40+
// - Wrapping the function that runs in the child in a `catch_unwind` and
41+
// then unwrapping the panic manually in the parent thread:
42+
// * ```
43+
// .spawn(|| catch_unwind(|| {
44+
// std::panic::set_hook(Box::new(|_| {}));
45+
// // std::io::set_panic(Some(Box::new(stderr)));
46+
// // std::io::set_print(Some(Box::new(stdout)));
47+
// println!("you shouldn't see this");
48+
// f()
49+
// }))
50+
// .unwrap();
51+
//
52+
// child.join().unwrap().unwrap();
53+
// ```
54+
// Which also doesn't work, because again: the panicking is not the
55+
// problem.
56+
//
57+
// So, I think we just have to live with it for now. Hopefully our tests
58+
// aren't noisy.
59+
1360
child.join().unwrap();
1461
}

0 commit comments

Comments
 (0)