1use std::borrow::Cow;
9use std::collections::HashMap;
10use std::hash::{BuildHasherDefault, DefaultHasher};
11use std::num::NonZero;
12use std::sync::{Arc, mpsc};
13use std::{env, hint, mem, panic, thread};
14
15use camino::Utf8PathBuf;
16
17use crate::common::{Config, TestPaths};
18use crate::output_capture::{self, ConsoleOut};
19use crate::panic_hook;
20
21mod deadline;
22mod json;
23
24pub(crate) fn run_tests(config: &Config, tests: Vec<CollectedTest>) -> bool {
25 let tests_len = tests.len();
26 let filtered = filter_tests(config, tests);
27 let mut fresh_tests = (0..).map(TestId).zip(&filtered);
29
30 let concurrency = get_concurrency();
31 assert!(concurrency > 0);
32 let concurrent_capacity = concurrency.min(filtered.len());
33
34 let mut listener = json::Listener::new();
35 let mut running_tests = HashMap::with_capacity_and_hasher(
36 concurrent_capacity,
37 BuildHasherDefault::<DefaultHasher>::new(),
38 );
39 let mut deadline_queue = deadline::DeadlineQueue::with_capacity(concurrent_capacity);
40
41 let num_filtered_out = tests_len - filtered.len();
42 listener.suite_started(filtered.len(), num_filtered_out);
43
44 let (completion_tx, completion_rx) = mpsc::channel::<TestCompletion>();
46
47 loop {
50 while running_tests.len() < concurrency
52 && let Some((id, test)) = fresh_tests.next()
53 {
54 listener.test_started(test);
55 deadline_queue.push(id, test);
56 let join_handle = spawn_test_thread(id, test, completion_tx.clone());
57 running_tests.insert(id, RunningTest { test, join_handle });
58 }
59
60 if running_tests.is_empty() {
63 break;
64 }
65
66 let completion = deadline_queue
67 .read_channel_while_checking_deadlines(
68 &completion_rx,
69 |id| running_tests.contains_key(&id),
70 |_id, test| listener.test_timed_out(test),
71 )
72 .expect("receive channel should never be closed early");
73
74 let RunningTest { test, join_handle } = running_tests.remove(&completion.id).unwrap();
75 if let Some(join_handle) = join_handle {
76 join_handle.join().unwrap_or_else(|_| {
77 panic!("thread for `{}` panicked after reporting completion", test.desc.name)
78 });
79 }
80
81 listener.test_finished(test, &completion);
82
83 if completion.outcome.is_failed() && config.fail_fast {
84 mem::forget(completion_rx);
87 break;
88 }
89 }
90
91 let suite_passed = listener.suite_finished();
92 suite_passed
93}
94
95fn spawn_test_thread(
99 id: TestId,
100 test: &CollectedTest,
101 completion_sender: mpsc::Sender<TestCompletion>,
102) -> Option<thread::JoinHandle<()>> {
103 if test.desc.ignore && !test.config.run_ignored {
104 completion_sender
105 .send(TestCompletion { id, outcome: TestOutcome::Ignored, stdout: None })
106 .unwrap();
107 return None;
108 }
109
110 let args = TestThreadArgs {
111 id,
112 config: Arc::clone(&test.config),
113 testpaths: test.testpaths.clone(),
114 revision: test.revision.clone(),
115 should_fail: test.desc.should_fail,
116 completion_sender,
117 };
118 let thread_builder = thread::Builder::new().name(test.desc.name.clone());
119 let join_handle = thread_builder.spawn(move || test_thread_main(args)).unwrap();
120 Some(join_handle)
121}
122
123struct TestThreadArgs {
125 id: TestId,
126
127 config: Arc<Config>,
128 testpaths: TestPaths,
129 revision: Option<String>,
130 should_fail: ShouldFail,
131
132 completion_sender: mpsc::Sender<TestCompletion>,
133}
134
135fn test_thread_main(args: TestThreadArgs) {
137 let capture = CaptureKind::for_config(&args.config);
138
139 if capture.should_set_panic_hook() {
141 panic_hook::set_capture_buf(Default::default());
142 }
143
144 let stdout = capture.stdout();
145 let stderr = capture.stderr();
146
147 let panic_payload = panic::catch_unwind(|| {
154 __rust_begin_short_backtrace(|| {
155 crate::runtest::run(
156 &args.config,
157 stdout,
158 stderr,
159 &args.testpaths,
160 args.revision.as_deref(),
161 );
162 });
163 })
164 .err();
165
166 if let Some(panic_buf) = panic_hook::take_capture_buf() {
167 let panic_buf = panic_buf.lock().unwrap_or_else(|e| e.into_inner());
168 write!(stderr, "{panic_buf}");
170 }
171
172 let outcome = match (args.should_fail, panic_payload) {
174 (ShouldFail::No, None) | (ShouldFail::Yes, Some(_)) => TestOutcome::Succeeded,
175 (ShouldFail::No, Some(_)) => TestOutcome::Failed { message: None },
176 (ShouldFail::Yes, None) => {
177 TestOutcome::Failed { message: Some("`//@ should-fail` test did not fail as expected") }
178 }
179 };
180
181 let stdout = capture.into_inner();
182 args.completion_sender.send(TestCompletion { id: args.id, outcome, stdout }).unwrap();
183}
184
185enum CaptureKind {
186 None,
191
192 Capture { buf: output_capture::CaptureBuf },
195}
196
197impl CaptureKind {
198 fn for_config(config: &Config) -> Self {
199 if config.nocapture {
200 Self::None
201 } else {
202 Self::Capture { buf: output_capture::CaptureBuf::new() }
203 }
204 }
205
206 fn should_set_panic_hook(&self) -> bool {
207 match self {
208 Self::None => false,
209 Self::Capture { .. } => true,
210 }
211 }
212
213 fn stdout(&self) -> &dyn ConsoleOut {
214 self.capture_buf_or(&output_capture::Stdout)
215 }
216
217 fn stderr(&self) -> &dyn ConsoleOut {
218 self.capture_buf_or(&output_capture::Stderr)
219 }
220
221 fn capture_buf_or<'a>(&'a self, fallback: &'a dyn ConsoleOut) -> &'a dyn ConsoleOut {
222 match self {
223 Self::None => fallback,
224 Self::Capture { buf } => buf,
225 }
226 }
227
228 fn into_inner(self) -> Option<Vec<u8>> {
229 match self {
230 Self::None => None,
231 Self::Capture { buf } => Some(buf.into_inner().into()),
232 }
233 }
234}
235
236#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
237struct TestId(usize);
238
239#[inline(never)]
241fn __rust_begin_short_backtrace<T, F: FnOnce() -> T>(f: F) -> T {
242 let result = f();
243
244 hint::black_box(result)
246}
247
248struct RunningTest<'a> {
249 test: &'a CollectedTest,
250 join_handle: Option<thread::JoinHandle<()>>,
251}
252
253struct TestCompletion {
256 id: TestId,
257 outcome: TestOutcome,
258 stdout: Option<Vec<u8>>,
259}
260
261#[derive(Clone, Debug, PartialEq, Eq)]
262enum TestOutcome {
263 Succeeded,
264 Failed { message: Option<&'static str> },
265 Ignored,
266}
267
268impl TestOutcome {
269 fn is_failed(&self) -> bool {
270 matches!(self, Self::Failed { .. })
271 }
272}
273
274fn filter_tests(opts: &Config, tests: Vec<CollectedTest>) -> Vec<CollectedTest> {
282 let mut filtered = tests;
283
284 let matches_filter = |test: &CollectedTest, filter_str: &str| {
285 if opts.filter_exact {
286 test.desc.filterable_path.as_str() == filter_str
289 } else {
290 test.desc.name.contains(filter_str)
293 }
294 };
295
296 if !opts.filters.is_empty() {
298 filtered.retain(|test| opts.filters.iter().any(|filter| matches_filter(test, filter)));
299 }
300
301 if !opts.skip.is_empty() {
303 filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
304 }
305
306 filtered
307}
308
309fn get_concurrency() -> usize {
317 if let Ok(value) = env::var("RUST_TEST_THREADS") {
318 match value.parse::<NonZero<usize>>().ok() {
319 Some(n) => n.get(),
320 _ => panic!("RUST_TEST_THREADS is `{value}`, should be a positive integer."),
321 }
322 } else {
323 thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
324 }
325}
326
327pub(crate) struct CollectedTest {
329 pub(crate) desc: CollectedTestDesc,
330 pub(crate) config: Arc<Config>,
331 pub(crate) testpaths: TestPaths,
332 pub(crate) revision: Option<String>,
333}
334
335pub(crate) struct CollectedTestDesc {
337 pub(crate) name: String,
338 pub(crate) filterable_path: Utf8PathBuf,
339 pub(crate) ignore: bool,
340 pub(crate) ignore_message: Option<Cow<'static, str>>,
341 pub(crate) should_fail: ShouldFail,
342}
343
344#[derive(Copy, Clone, Default, Debug)]
346pub enum ColorConfig {
347 #[default]
348 AutoColor,
349 AlwaysColor,
350 NeverColor,
351}
352
353#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
356pub(crate) enum ShouldFail {
357 No,
358 Yes,
359}