Visual Studio Code, rebuilt in Rust
Rust
test(exthost): cover ask_workbench sending queued workbench calls
rokartur6d5e4a6a19c4
diff --git a/native/crates/exthost/src/lib.rs b/native/crates/exthost/src/lib.rsindex 6b0ee7144e9..4b762abd2cf 100644--- a/native/crates/exthost/src/lib.rs+++ b/native/crates/exthost/src/lib.rs@@ -176,6 +176,17 @@ impl ExtensionHost {pub fn terminate(&self) {self.send(vec![MESSAGE_TERMINATE]);}++ /// A host that never sends anything, over a socket pair whose other end is `peer`.+ #[cfg(test)]+ pub(crate) fn silent() -> (Self, UnixStream) {+ let (stream, peer) = UnixStream::pair().unwrap();+ let writer = Arc::new(Mutex::new(WriterState { stream: stream.try_clone().unwrap(), outgoing_id: 0, incoming_id: 0 }));+ let (sender, incoming) = channel();+ spawn_reader(stream, writer.clone(), sender);+ let child = Command::new("sleep").arg("60").spawn().unwrap();+ (ExtensionHost { child, writer, incoming, socket_path: PathBuf::new() }, peer)+ }}impl Drop for ExtensionHost {diff --git a/native/crates/exthost/src/session.rs b/native/crates/exthost/src/session.rsindex 2a5cbe089e6..19289c6399c 100644--- a/native/crates/exthost/src/session.rs+++ b/native/crates/exthost/src/session.rs@@ -879,4 +879,34 @@ mod tests {assert_eq!(data["folders"][0]["uri"], file_uri(&file.parent().unwrap().join("a")));std::fs::remove_file(file).unwrap();}++ /// The workbench answers only once its queued host call went out (a debug memory read's `readMemory`), so the+ /// asking side must flush the workbench's queue while it waits.+ #[test]+ fn ask_workbench_sends_the_workbench_host_calls_while_it_waits() {+ struct AnswersOnFlush {+ asked: std::sync::Arc<std::sync::Mutex<Option<u32>>>,+ replies: std::sync::mpsc::Sender<(u32, Result<Value, Value>)>,+ }+ impl MainThread for AnswersOnFlush {+ fn handle(&mut self, _: &mut Session, _: &str, _: &str, _: &[Arg]) -> Option<Result<Reply, Value>> {+ None+ }+ fn flush(&mut self, _: &mut Session) {+ if let Some(req) = self.asked.lock().unwrap().take() {+ self.replies.send((req, Ok(json!("answered")))).unwrap();+ }+ }+ }++ let (host, _peer) = ExtensionHost::silent();+ let mut session = Session::new(host);+ let asked = std::sync::Arc::new(std::sync::Mutex::new(None));+ let (replies, workbench_replies) = std::sync::mpsc::channel();+ session.forward(&[], { let asked = asked.clone(); move |request| *asked.lock().unwrap() = Some(request.req) }, workbench_replies);+ session.main_thread = Some(Box::new(AnswersOnFlush { asked, replies }));+ let (done, answer) = std::sync::mpsc::channel();+ std::thread::spawn(move || done.send(session.ask_workbench("workbench", "debugMemory", Vec::new())).unwrap());+ assert_eq!(answer.recv_timeout(std::time::Duration::from_secs(5)).expect("ask_workbench hung"), Some(Ok(json!("answered"))));+ }}