Skip to content

Commit f41b363

Browse files
committed
Update libtest for single-threaded emscripten support
1 parent b8b50f0 commit f41b363

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

src/libtest/lib.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,26 +1182,63 @@ pub fn run_test(opts: &TestOpts,
11821182
}
11831183
}
11841184

1185-
thread::spawn(move || {
1186-
let data = Arc::new(Mutex::new(Vec::new()));
1187-
let data2 = data.clone();
1188-
let cfg = thread::Builder::new().name(match desc.name {
1189-
DynTestName(ref name) => name.clone(),
1190-
StaticTestName(name) => name.to_owned(),
1185+
// If the platform is single-threaded we're just going to run
1186+
// the test synchronously, regardless of the concurrency
1187+
// level.
1188+
let supports_threads = !cfg!(target_os = "emscripten");
1189+
1190+
// Buffer for capturing standard I/O
1191+
let data = Arc::new(Mutex::new(Vec::new()));
1192+
let data2 = data.clone();
1193+
1194+
if supports_threads {
1195+
thread::spawn(move || {
1196+
let cfg = thread::Builder::new().name(match desc.name {
1197+
DynTestName(ref name) => name.clone(),
1198+
StaticTestName(name) => name.to_owned(),
1199+
});
1200+
1201+
let result_guard = cfg.spawn(move || {
1202+
if !nocapture {
1203+
io::set_print(box Sink(data2.clone()));
1204+
io::set_panic(box Sink(data2));
1205+
}
1206+
testfn()
1207+
})
1208+
.unwrap();
1209+
let test_result = calc_result(&desc, result_guard.join());
1210+
let stdout = data.lock().unwrap().to_vec();
1211+
monitor_ch.send((desc.clone(), test_result, stdout)).unwrap();
11911212
});
1213+
} else {
1214+
let oldio = if !nocapture {
1215+
Some((
1216+
io::set_print(box Sink(data2.clone())),
1217+
io::set_panic(box Sink(data2))
1218+
))
1219+
} else {
1220+
None
1221+
};
1222+
1223+
use std::panic::{catch_unwind, AssertUnwindSafe};
11921224

1193-
let result_guard = cfg.spawn(move || {
1194-
if !nocapture {
1195-
io::set_print(box Sink(data2.clone()));
1196-
io::set_panic(box Sink(data2));
1197-
}
1198-
testfn()
1199-
})
1200-
.unwrap();
1201-
let test_result = calc_result(&desc, result_guard.join());
1225+
let result = catch_unwind(AssertUnwindSafe(|| {
1226+
testfn()
1227+
}));
1228+
1229+
if let Some((printio, panicio)) = oldio {
1230+
if let Some(printio) = printio {
1231+
io::set_print(printio);
1232+
}
1233+
if let Some(panicio) = panicio {
1234+
io::set_panic(panicio);
1235+
}
1236+
};
1237+
1238+
let test_result = calc_result(&desc, result);
12021239
let stdout = data.lock().unwrap().to_vec();
12031240
monitor_ch.send((desc.clone(), test_result, stdout)).unwrap();
1204-
});
1241+
}
12051242
}
12061243

12071244
match testfn {

0 commit comments

Comments
 (0)