1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! The app's side of the worker: queueing each kind of run and discovery, cancelling,
//! re-rooting, and draining the streamed results into the panel.
use super::*;
impl TestWorker {
pub fn spawn(workspace_root: PathBuf) -> Self {
Self::spawn_with(workspace_root, worker_loop)
}
/// A worker whose thread runs `work`, the real loop outside tests.
pub(super) fn spawn_with(
workspace_root: PathBuf,
work: impl Fn(PathBuf, Receiver<Sent>, Sender<(u64, TestResponse)>, &RunControl, &Owners)
+ Send
+ Sync
+ 'static,
) -> Self {
let control = RunControl::default();
let owners = Arc::new(Owners::default());
let (thread_control, thread_owners) = (control.clone(), Arc::clone(&owners));
let first_root = workspace_root.clone();
let worker = Worker::spawn(
super::RUNNER_THREAD,
Start::AfterFirstFrame,
move |rx, tx| work(first_root.clone(), rx, tx, &thread_control, &thread_owners),
)
.expect("start the test runner");
let worker = Self::around(worker, control, workspace_root);
#[cfg(test)]
let worker = Self { owners, ..worker };
worker
}
fn around(
worker: Worker<Sent, (u64, TestResponse)>,
control: RunControl,
root: PathBuf,
) -> Self {
Self {
worker,
control,
#[cfg(test)]
owners: Arc::default(),
sent: 0,
cancel_pending: false,
discover_queued: false,
expected_epoch: 0,
root,
codeql: None,
extensions: None,
stopped_news: false,
}
}
/// Build a worker around hand-made channels (no thread) so tests can
/// inject tagged responses straight into the drain.
#[cfg(test)]
pub(super) fn for_test() -> (Self, Sender<(u64, TestResponse)>) {
let (request_tx, _request_rx) = std::sync::mpsc::channel::<Sent>();
let (response_tx, response_rx) = std::sync::mpsc::channel::<(u64, TestResponse)>();
let worker = Worker::from_channels(request_tx, response_rx);
(
Self::around(worker, RunControl::default(), PathBuf::new()),
response_tx,
)
}
/// The real worker, whose first `crashes` threads panic on their first
/// request (#2592).
#[cfg(test)]
pub(crate) fn crashing(root: PathBuf, crashes: usize) -> Self {
let started = Arc::new(AtomicU64::new(0));
Self::spawn_with(root, move |root, rx, tx, control, owners| {
if started.fetch_add(1, Ordering::SeqCst) < crashes as u64 {
let _ = rx.recv();
panic!("the test runner crashed (on purpose, in a test)");
}
worker_loop(root, rx, tx, control, owners);
})
}
/// Queue `request`, numbered and tagged with the current root.
pub(super) fn send(&mut self, request: TestRequest) {
self.sent += 1;
self.worker.send(Sent {
number: self.sent,
epoch: self.expected_epoch,
request,
});
}
/// Queue a run or discovery. A worker that stopped after crashing
/// twice starts again here: the user asked for tests (#2592).
fn ask(&mut self, request: TestRequest) {
if self.worker.is_stopped() && self.worker.restart().is_ok() {
self.resend_settings();
}
self.send(request);
}
/// Give a fresh thread the root and settings its crashed one had
/// (#2592), under the same epoch: nothing about the root changed.
fn resend_settings(&mut self) {
self.send(TestRequest::SetRoot(self.root.clone()));
if let Some(program) = self.codeql.clone() {
self.send(TestRequest::SetCodeqlProgram(program));
}
if let Some(dir) = self.extensions.clone() {
self.send(TestRequest::SetExtensionsDir(dir));
}
}
pub fn run_coverage(&mut self) {
self.ask(TestRequest::RunCoverage(None));
}
/// Run only the tests `scope` names with coverage on (#263).
pub fn run_coverage_scoped(&mut self, scope: CoverageScope) {
self.ask(TestRequest::RunCoverage(Some(scope)));
}
pub fn run_all(&mut self) {
self.ask(TestRequest::RunAll);
}
pub fn run_one(&mut self, name: String) {
self.ask(TestRequest::RunOne(name));
}
pub fn run_many(&mut self, names: Vec<String>) {
self.ask(TestRequest::RunMany(names));
}
/// Run the tests matching `pattern`, with the runner for `file`'s
/// language when the run came from one (#1408).
pub fn run_filter(&mut self, pattern: String, file: Option<PathBuf>) {
self.ask(TestRequest::RunFilter(pattern, file));
}
pub fn run_suite(&mut self, suite: String) {
self.ask(TestRequest::RunSuite(suite));
}
pub fn discover(&mut self) {
self.ask(TestRequest::Discover { quiet: false });
}
/// Discover from the Testing view (#1841): queued without looking for a
/// runner first, so opening the view never waits on that search. The
/// worker finds the runner and stays silent when there is none.
pub fn discover_quietly(&mut self) {
self.discover_queued = true;
self.ask(TestRequest::Discover { quiet: true });
}
/// A quiet discovery is queued and has not started yet.
pub fn discover_queued(&self) -> bool {
self.discover_queued
}
/// How many requests have been queued, for tests that count them.
#[cfg(test)]
pub fn requests_sent(&self) -> u64 {
self.sent
}
pub fn set_codeql_program(&mut self, program: PathBuf) {
self.codeql = Some(program.clone());
self.send(TestRequest::SetCodeqlProgram(program));
}
pub fn set_extensions_dir(&mut self, dir: PathBuf) {
self.extensions = Some(dir.clone());
self.send(TestRequest::SetExtensionsDir(dir));
}
/// Stop the run or discovery in flight (#1534): its runner's process
/// group is killed, and its end reaches the panel as cancelled. Also
/// called on quit, so no runner outlives croft.
pub fn cancel(&mut self) {
self.control.cancel(self.sent);
self.cancel_pending = true;
}
/// Rebind the worker to a new workspace root after an Explorer re-root.
/// Everything still streaming for the old root carries the old epoch and
/// is dropped by [`Self::drain`].
pub fn set_root(&mut self, root: PathBuf) {
self.root = root.clone();
self.expected_epoch += 1;
// A discovery queued for the old root reports under the old epoch,
// which the drain drops: nothing would ever clear it.
self.discover_queued = false;
// Every request a pending cancel covers was sent before this
// re-root, so its end carries the old epoch and is dropped below:
// the cancel must not outlive it and swallow the new root's next
// verdict.
self.cancel_pending = false;
self.send(TestRequest::SetRoot(root));
}
#[cfg(test)]
pub fn root(&self) -> &Path {
&self.root
}
/// Drain streamed results into the panel. Returns true iff anything was
/// applied, so the main loop only redraws on a real update. Responses
/// tagged with an epoch older than the last `set_root` belong to the
/// previous project and are dropped.
pub fn drain(&mut self, panel: &mut TestingPanel) -> bool {
let mut changed = false;
loop {
let (epoch, resp) = match self.worker.poll() {
WorkerPoll::Answer(answer) => answer,
WorkerPoll::Idle => break,
// Its thread crashed, and the crash notice said so. A
// fresh one has the same root and settings; the run it
// was on is not sent again, as a run can have side
// effects (#2592).
WorkerPoll::Restarted => {
changed |= self.thread_ended(panel);
self.resend_settings();
continue;
}
WorkerPoll::Stopped => {
self.thread_ended(panel);
self.stopped_news = true;
changed = true;
break;
}
};
if epoch != self.expected_epoch {
continue;
}
match resp {
TestResponse::QuietDiscovery => {
self.discover_queued = false;
continue;
}
TestResponse::Started(activity) => panel.on_busy_started(activity),
TestResponse::Case(case) => panel.apply_case(case),
TestResponse::Progress(line) => panel.set_progress(line),
TestResponse::Finished { .. } | TestResponse::Refused
if std::mem::take(&mut self.cancel_pending) =>
{
panel.on_cancelled()
}
TestResponse::Finished { ok } => panel.on_finished(ok),
TestResponse::Refused => panel.on_refused(),
// A cancelled coverage run's killed child wrote no report:
// that error is not news, and would overwrite the cancel.
TestResponse::Coverage(_) if self.cancel_pending => continue,
TestResponse::Coverage(result) => panel.on_coverage(result),
}
changed = true;
}
changed
}
/// The worker's thread ended (#2592). What was sent to it went with
/// it: the runner it was reading from is killed, a run or discovery
/// in flight ends as stopped, and a queued quiet discovery is
/// forgotten so the Testing view can queue another. True when the
/// panel changed.
fn thread_ended(&mut self, panel: &mut TestingPanel) -> bool {
self.control.abandon(self.sent);
self.discover_queued = false;
self.cancel_pending = false;
if !panel.is_busy() {
return false;
}
panel.on_stopped();
true
}
/// True once when the worker has stopped after crashing again soon
/// after a restart (#2592), so the app can say so.
pub fn take_stopped(&mut self) -> bool {
std::mem::take(&mut self.stopped_news)
}
}