Spawn a worker thread running tests.
(
thread_num: usize,
requests: Arc<Mutex<Receiver<Request>>>,
replies: Sender<Reply>,
)
| 127 | |
| 128 | /// Spawn a worker thread running tests. |
| 129 | fn worker_thread( |
| 130 | thread_num: usize, |
| 131 | requests: Arc<Mutex<Receiver<Request>>>, |
| 132 | replies: Sender<Reply>, |
| 133 | ) -> thread::JoinHandle<timing::PassTimes> { |
| 134 | thread::Builder::new() |
| 135 | .name(format!("worker #{thread_num}")) |
| 136 | .spawn(move || { |
| 137 | file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); |
| 138 | loop { |
| 139 | // Lock the mutex only long enough to extract a request. |
| 140 | let Request(jobid, path) = match requests.lock().unwrap().recv() { |
| 141 | Err(..) => break, // TX end shut down. exit thread. |
| 142 | Ok(req) => req, |
| 143 | }; |
| 144 | |
| 145 | // Tell them we're starting this job. |
| 146 | // The receiver should always be present for this as long as we have jobs. |
| 147 | replies.send(Reply::Starting { jobid }).unwrap(); |
| 148 | |
| 149 | let result = catch_unwind(|| runone::run(path.as_path(), None, None)) |
| 150 | .unwrap_or_else(|e| { |
| 151 | // The test panicked, leaving us a `Box<Any>`. |
| 152 | // Panics are usually strings. |
| 153 | if let Some(msg) = e.downcast_ref::<String>() { |
| 154 | anyhow::bail!("panicked in worker #{thread_num}: {msg}") |
| 155 | } else if let Some(msg) = e.downcast_ref::<&'static str>() { |
| 156 | anyhow::bail!("panicked in worker #{thread_num}: {msg}") |
| 157 | } else { |
| 158 | anyhow::bail!("panicked in worker #{thread_num}") |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | if let Err(ref msg) = result { |
| 163 | error!("FAIL: {msg}"); |
| 164 | } |
| 165 | |
| 166 | replies.send(Reply::Done { jobid, result }).unwrap(); |
| 167 | } |
| 168 | |
| 169 | // Timing is accumulated independently per thread. |
| 170 | // Timings from this worker thread will be aggregated by `ConcurrentRunner::join()`. |
| 171 | timing::take_current() |
| 172 | }) |
| 173 | .unwrap() |
| 174 | } |
no test coverage detected