From 6ae167efc9ac0e9a0422ba8b4e7ecd97fdd05685 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 15 Nov 2026 09:53:29 +0800 Subject: [PATCH] test(lsp): wait for merged injection diagnostics instead of reading them at once (#3470) The SQL server's launch fails with the test launcher and reports its status from a runtime thread; while that thread drains the server's event queue it delivers the diagnostics the test emits, so reading the sink straight after emitting could find no host event (24/200 failures alone). The test now waits for the expected host diagnostics at each step (0/300). --- crates/ide-lsp/src/injections.rs | 34 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/crates/ide-lsp/src/injections.rs b/crates/ide-lsp/src/injections.rs index 4f67a9e4b..84659b978 100644 --- a/crates/ide-lsp/src/injections.rs +++ b/crates/ide-lsp/src/injections.rs @@ -715,33 +715,35 @@ mod tests { server.set_diagnostics(a.path.clone(), vec![diag(2, 5, "a"), diag(0, 1, "outside"), diag(4, 6, "crosses")]); let items = server.set_diagnostics(b.path.clone(), vec![diag(7, 10, "b")]); mgr.emit_diagnostics(&server, b.path, items, Encoding::Utf16); - let latest = || { - events - .lock() - .iter() - .rev() - .find_map(|e| match e { + // The SQL server's failed launch reports its status from a runtime thread; while that + // thread drains the server's event queue, events emitted here reach the sink from it. + let latest = |want: &dyn Fn(&[ide_core::Diagnostic]) -> bool| { + let deadline = std::time::Instant::now() + Duration::from_secs(5); + loop { + let items = events.lock().iter().rev().find_map(|e| match e { ide_core::IdeEvent::Diagnostics { path: p, source, items } if p.0 == path && source == &server.name => { Some(items.clone()) } _ => None, - }) - .unwrap() + }); + match items { + Some(items) if want(&items) => return items, + _ if std::time::Instant::now() > deadline => panic!("latest host diagnostics: {items:?}"), + _ => std::thread::sleep(Duration::from_millis(5)), + } + } }; - assert_eq!(latest().iter().map(|d| d.message.as_str()).collect::>().len(), 3); + let messages = |items: &[ide_core::Diagnostic]| items.iter().map(|d| d.message.clone()).collect::>(); + let merged = latest(&|items| items.len() == 3); assert!( - latest() - .iter() - .filter(|d| d.message != "native") - .all(|d| d.source.as_deref() == Some(server.name.as_str())) + merged.iter().filter(|d| d.message != "native").all(|d| d.source.as_deref() == Some(server.name.as_str())) ); mgr.set_injections(host, 1, vec![injection(7, 10)]); - assert_eq!(latest().iter().map(|d| d.message.clone()).collect::>(), ["native", "b"]); + latest(&|items| messages(items) == ["native", "b"]); mgr.set_injections(host, 1, vec![]); - assert_eq!(latest()[0].message, "native"); - assert_eq!(latest().len(), 1); + latest(&|items| messages(items) == ["native"]); assert!( events.lock().iter().all(|e| !matches!(e, ide_core::IdeEvent::Diagnostics { path, .. } if reserved(path))) );