Advance clock when simulating random delay

This commit is contained in:
Antonio Scandurra 2022-03-02 10:39:46 +01:00
parent be7a4770fb
commit 51345cf1e1

View File

@ -223,6 +223,12 @@ impl Deterministic {
if state.scheduled_from_foreground.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;
}
@ -300,6 +306,17 @@ impl Deterministic {
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"))]
@ -467,15 +484,7 @@ impl Foreground {
match self {
Self::Deterministic { executor, .. } => {
executor.run_until_parked();
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);
executor.advance_clock(duration);
}
_ => panic!("this method can only be called on a deterministic executor"),
}
@ -604,6 +613,9 @@ impl Background {
for _ in 0..yields {
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"),