Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/lspkit-server/src/live/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Trigger>) {
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<time::Instant>, config: ScheduleConfig) -> time::Instant {
let now = time::Instant::now();
Expand Down
32 changes: 32 additions & 0 deletions crates/lspkit-server/tests/live_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
Loading