From 4508d94a3ef5d3d873d65e130e1fbfa9dd1bf74b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 6 Oct 2022 17:03:23 -0700 Subject: [PATCH] In deterministic executor, ensure fake timers are ordered by wake time Previously, advancing the clock would fail to wake a timer that was set *after* another time whose wake time had not yet arrived. --- crates/gpui/src/executor.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/gpui/src/executor.rs b/crates/gpui/src/executor.rs index 980da91167..0639445b0d 100644 --- a/crates/gpui/src/executor.rs +++ b/crates/gpui/src/executor.rs @@ -325,7 +325,12 @@ impl Deterministic { let mut state = self.state.lock(); let wakeup_at = state.now + duration; let id = util::post_inc(&mut state.next_timer_id); - state.pending_timers.push((id, wakeup_at, tx)); + match state + .pending_timers + .binary_search_by_key(&wakeup_at, |e| e.1) + { + Ok(ix) | Err(ix) => state.pending_timers.insert(ix, (id, wakeup_at, tx)), + } let state = self.state.clone(); Timer::Deterministic(DeterministicTimer { rx, id, state }) }