diff options
author | Aleksey Kladov <[email protected]> | 2021-01-28 13:00:33 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-01-28 13:00:33 +0000 |
commit | a58fa29dc201f82891f11acb347e363f7cfcbd4e (patch) | |
tree | 44a32268ab35ba0770e6ffbb0b5e2835a595824b | |
parent | ad3cb2125dee4379c11f447216f510d6544c10c5 (diff) |
Easier to debug timeouts in tests
-rw-r--r-- | crates/rust-analyzer/tests/rust-analyzer/support.rs | 34 |
1 files 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 { | |||
188 | } | 188 | } |
189 | fn send_request_(&self, r: Request) -> Value { | 189 | fn send_request_(&self, r: Request) -> Value { |
190 | let id = r.id.clone(); | 190 | let id = r.id.clone(); |
191 | self.client.sender.send(r.into()).unwrap(); | 191 | self.client.sender.send(r.clone().into()).unwrap(); |
192 | while let Some(msg) = self.recv() { | 192 | while let Some(msg) = self.recv().unwrap_or_else(|Timeout| panic!("timeout: {:?}", r)) { |
193 | match msg { | 193 | match msg { |
194 | Message::Request(req) => { | 194 | Message::Request(req) => { |
195 | if req.method == "window/workDoneProgress/create" { | 195 | if req.method == "window/workDoneProgress/create" { |
@@ -216,7 +216,7 @@ impl Server { | |||
216 | } | 216 | } |
217 | } | 217 | } |
218 | } | 218 | } |
219 | panic!("no response"); | 219 | panic!("no response for {:?}", r); |
220 | } | 220 | } |
221 | pub(crate) fn wait_until_workspace_is_loaded(self) -> Server { | 221 | pub(crate) fn wait_until_workspace_is_loaded(self) -> Server { |
222 | self.wait_for_message_cond(1, &|msg: &Message| match msg { | 222 | self.wait_for_message_cond(1, &|msg: &Message| match msg { |
@@ -230,10 +230,15 @@ impl Server { | |||
230 | } | 230 | } |
231 | } | 231 | } |
232 | _ => false, | 232 | _ => false, |
233 | }); | 233 | }) |
234 | .unwrap_or_else(|Timeout| panic!("timeout while waiting for ws to load")); | ||
234 | self | 235 | self |
235 | } | 236 | } |
236 | fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> bool) { | 237 | fn wait_for_message_cond( |
238 | &self, | ||
239 | n: usize, | ||
240 | cond: &dyn Fn(&Message) -> bool, | ||
241 | ) -> Result<(), Timeout> { | ||
237 | let mut total = 0; | 242 | let mut total = 0; |
238 | for msg in self.messages.borrow().iter() { | 243 | for msg in self.messages.borrow().iter() { |
239 | if cond(msg) { | 244 | if cond(msg) { |
@@ -241,17 +246,20 @@ impl Server { | |||
241 | } | 246 | } |
242 | } | 247 | } |
243 | while total < n { | 248 | while total < n { |
244 | let msg = self.recv().expect("no response"); | 249 | let msg = self.recv()?.expect("no response"); |
245 | if cond(&msg) { | 250 | if cond(&msg) { |
246 | total += 1; | 251 | total += 1; |
247 | } | 252 | } |
248 | } | 253 | } |
254 | Ok(()) | ||
249 | } | 255 | } |
250 | fn recv(&self) -> Option<Message> { | 256 | fn recv(&self) -> Result<Option<Message>, Timeout> { |
251 | recv_timeout(&self.client.receiver).map(|msg| { | 257 | let msg = recv_timeout(&self.client.receiver)?; |
258 | let msg = msg.map(|msg| { | ||
252 | self.messages.borrow_mut().push(msg.clone()); | 259 | self.messages.borrow_mut().push(msg.clone()); |
253 | msg | 260 | msg |
254 | }) | 261 | }); |
262 | Ok(msg) | ||
255 | } | 263 | } |
256 | fn send_notification(&self, not: Notification) { | 264 | fn send_notification(&self, not: Notification) { |
257 | self.client.sender.send(Message::Notification(not)).unwrap(); | 265 | self.client.sender.send(Message::Notification(not)).unwrap(); |
@@ -269,11 +277,13 @@ impl Drop for Server { | |||
269 | } | 277 | } |
270 | } | 278 | } |
271 | 279 | ||
272 | fn recv_timeout(receiver: &Receiver<Message>) -> Option<Message> { | 280 | struct Timeout; |
281 | |||
282 | fn recv_timeout(receiver: &Receiver<Message>) -> Result<Option<Message>, Timeout> { | ||
273 | let timeout = | 283 | let timeout = |
274 | if cfg!(target_os = "macos") { Duration::from_secs(300) } else { Duration::from_secs(120) }; | 284 | if cfg!(target_os = "macos") { Duration::from_secs(300) } else { Duration::from_secs(120) }; |
275 | select! { | 285 | select! { |
276 | recv(receiver) -> msg => msg.ok(), | 286 | recv(receiver) -> msg => Ok(msg.ok()), |
277 | recv(after(timeout)) -> _ => panic!("timed out"), | 287 | recv(after(timeout)) -> _ => Err(Timeout), |
278 | } | 288 | } |
279 | } | 289 | } |