diff --git a/crates/lspkit-server/src/live/scheduler.rs b/crates/lspkit-server/src/live/scheduler.rs index 6e656dc..fe21222 100644 --- a/crates/lspkit-server/src/live/scheduler.rs +++ b/crates/lspkit-server/src/live/scheduler.rs @@ -150,11 +150,23 @@ async fn run( match step { Step::Event(event) => burst.push(event), Step::Quiet => burst.ready = true, - Step::Ended => return, + Step::Ended => return deliver_ready(&mut burst, &tx).await, } } } +/// Hand a ready burst to the consumer once the events end: its window elapsed, +/// so it is owed even when the consumer was still busy. A consumer that already +/// left needs no last trigger. [MEM-LIFETIME-SCHEDULER-BACKLOG] +async fn deliver_ready(burst: &mut Burst, tx: &mpsc::Sender) { + if !burst.ready { + return; + } + if let Ok(permit) = tx.reserve().await { + permit.send(burst.take()); + } +} + /// Bounds the next quiet-window deadline by the burst's maximum wait. fn compute_deadline(first: Option, config: ScheduleConfig) -> time::Instant { let now = time::Instant::now(); diff --git a/crates/lspkit-server/tests/live_scheduler.rs b/crates/lspkit-server/tests/live_scheduler.rs index 07c72b8..035dc8d 100644 --- a/crates/lspkit-server/tests/live_scheduler.rs +++ b/crates/lspkit-server/tests/live_scheduler.rs @@ -286,3 +286,35 @@ async fn a_backlog_past_the_bound_collapses_to_one_rescan() { let fresh = triggers.recv().await.expect("trigger after the rescan"); assert_eq!(paths_of(&fresh), vec![PathBuf::from("fresh.rs")]); } + +/// [MEM-LIFETIME-SCHEDULER-BACKLOG] A burst whose window has elapsed is owed to +/// the consumer even when the events end while the consumer is still busy: the +/// scheduler delivers it before closing, as it does for a free consumer. +#[tokio::test(flavor = "current_thread", start_paused = true)] +async fn events_ending_behind_a_busy_consumer_still_deliver_the_ready_burst() { + let (tx, mut triggers) = build(); + send_and_quiet(&tx, touched("first.rs")) + .await + .expect("send the burst the consumer is still holding"); + send_and_quiet(&tx, removed("second.rs")) + .await + .expect("send the burst that becomes ready behind it"); + drop(tx); + settle().await; + + let first = triggers.recv().await.expect("the held trigger"); + assert!( + matches!(first.events.as_slice(), [ChangeEvent::Touched(p)] if p == &vec![PathBuf::from("first.rs")]) + ); + let second = triggers + .recv() + .await + .expect("the ready burst survives the end of the events"); + assert!( + matches!(second.events.as_slice(), [ChangeEvent::Removed(p)] if p == &vec![PathBuf::from("second.rs")]) + ); + assert!( + triggers.recv().await.is_none(), + "the channel closes after the last burst" + ); +}