From a58fa29dc201f82891f11acb347e363f7cfcbd4e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 28 Jan 2021 16:00:33 +0300 Subject: Easier to debug timeouts in tests --- .../rust-analyzer/tests/rust-analyzer/support.rs | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/rust-analyzer/tests/rust-analyzer/support.rs b/crates/rust-analyzer/tests/rust-analyzer/support.rs index 2658ee185..f59958e8d 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/support.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/support.rs @@ -188,8 +188,8 @@ impl Server { } fn send_request_(&self, r: Request) -> Value { let id = r.id.clone(); - self.client.sender.send(r.into()).unwrap(); - while let Some(msg) = self.recv() { + self.client.sender.send(r.clone().into()).unwrap(); + while let Some(msg) = self.recv().unwrap_or_else(|Timeout| panic!("timeout: {:?}", r)) { match msg { Message::Request(req) => { if req.method == "window/workDoneProgress/create" { @@ -216,7 +216,7 @@ impl Server { } } } - panic!("no response"); + panic!("no response for {:?}", r); } pub(crate) fn wait_until_workspace_is_loaded(self) -> Server { self.wait_for_message_cond(1, &|msg: &Message| match msg { @@ -230,10 +230,15 @@ impl Server { } } _ => false, - }); + }) + .unwrap_or_else(|Timeout| panic!("timeout while waiting for ws to load")); self } - fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> bool) { + fn wait_for_message_cond( + &self, + n: usize, + cond: &dyn Fn(&Message) -> bool, + ) -> Result<(), Timeout> { let mut total = 0; for msg in self.messages.borrow().iter() { if cond(msg) { @@ -241,17 +246,20 @@ impl Server { } } while total < n { - let msg = self.recv().expect("no response"); + let msg = self.recv()?.expect("no response"); if cond(&msg) { total += 1; } } + Ok(()) } - fn recv(&self) -> Option { - recv_timeout(&self.client.receiver).map(|msg| { + fn recv(&self) -> Result, Timeout> { + let msg = recv_timeout(&self.client.receiver)?; + let msg = msg.map(|msg| { self.messages.borrow_mut().push(msg.clone()); msg - }) + }); + Ok(msg) } fn send_notification(&self, not: Notification) { self.client.sender.send(Message::Notification(not)).unwrap(); @@ -269,11 +277,13 @@ impl Drop for Server { } } -fn recv_timeout(receiver: &Receiver) -> Option { +struct Timeout; + +fn recv_timeout(receiver: &Receiver) -> Result, Timeout> { let timeout = if cfg!(target_os = "macos") { Duration::from_secs(300) } else { Duration::from_secs(120) }; select! { - recv(receiver) -> msg => msg.ok(), - recv(after(timeout)) -> _ => panic!("timed out"), + recv(receiver) -> msg => Ok(msg.ok()), + recv(after(timeout)) -> _ => Err(Timeout), } } -- cgit v1.2.3