mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-07 02:57:34 +00:00
Advance clock when simulating random delay
This commit is contained in:
parent
be7a4770fb
commit
51345cf1e1
1 changed files with 21 additions and 9 deletions
|
@ -223,6 +223,12 @@ impl Deterministic {
|
||||||
if state.scheduled_from_foreground.is_empty()
|
if state.scheduled_from_foreground.is_empty()
|
||||||
&& state.scheduled_from_background.is_empty()
|
&& state.scheduled_from_background.is_empty()
|
||||||
{
|
{
|
||||||
|
if let Some(main_task) = main_task {
|
||||||
|
if let Poll::Ready(result) = main_task.poll(&mut cx) {
|
||||||
|
return Some(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,6 +306,17 @@ impl Deterministic {
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn advance_clock(&self, duration: Duration) {
|
||||||
|
let mut state = self.state.lock();
|
||||||
|
state.now += duration;
|
||||||
|
let now = state.now;
|
||||||
|
let mut pending_timers = mem::take(&mut state.pending_timers);
|
||||||
|
drop(state);
|
||||||
|
|
||||||
|
pending_timers.retain(|(_, wakeup, _)| *wakeup > now);
|
||||||
|
self.state.lock().pending_timers.extend(pending_timers);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
|
@ -467,15 +484,7 @@ impl Foreground {
|
||||||
match self {
|
match self {
|
||||||
Self::Deterministic { executor, .. } => {
|
Self::Deterministic { executor, .. } => {
|
||||||
executor.run_until_parked();
|
executor.run_until_parked();
|
||||||
|
executor.advance_clock(duration);
|
||||||
let mut state = executor.state.lock();
|
|
||||||
state.now += duration;
|
|
||||||
let now = state.now;
|
|
||||||
let mut pending_timers = mem::take(&mut state.pending_timers);
|
|
||||||
drop(state);
|
|
||||||
|
|
||||||
pending_timers.retain(|(_, wakeup, _)| *wakeup > now);
|
|
||||||
executor.state.lock().pending_timers.extend(pending_timers);
|
|
||||||
}
|
}
|
||||||
_ => panic!("this method can only be called on a deterministic executor"),
|
_ => panic!("this method can only be called on a deterministic executor"),
|
||||||
}
|
}
|
||||||
|
@ -604,6 +613,9 @@ impl Background {
|
||||||
for _ in 0..yields {
|
for _ in 0..yields {
|
||||||
yield_now().await;
|
yield_now().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let delay = Duration::from_millis(executor.state.lock().rng.gen_range(0..100));
|
||||||
|
executor.advance_clock(delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => panic!("this method can only be called on a deterministic executor"),
|
_ => panic!("this method can only be called on a deterministic executor"),
|
||||||
|
|
Loading…
Reference in a new issue