Reset peer's receive timeout when a message is received

* Make advance_clock more realistic by waking timers in order,
  instead of all at once.
* Don't advance the clock when simulating random delays.

Co-Authored-By: Keith Simmons <keith@zed.dev>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-03-07 15:33:39 -08:00
parent 120f7bbc3d
commit 1f5eab39a9
5 changed files with 67 additions and 32 deletions

View file

@ -330,14 +330,34 @@ impl Deterministic {
}
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);
let new_now = self.state.lock().now + duration;
loop {
self.run_until_parked();
let mut state = self.state.lock();
pending_timers.retain(|(_, wakeup, _)| *wakeup > now);
self.state.lock().pending_timers.extend(pending_timers);
if let Some((_, wakeup_time, _)) = state.pending_timers.first() {
let wakeup_time = *wakeup_time;
if wakeup_time < new_now {
let timer_count = state
.pending_timers
.iter()
.take_while(|(_, t, _)| *t == wakeup_time)
.count();
state.now = wakeup_time;
let timers_to_wake = state
.pending_timers
.drain(0..timer_count)
.collect::<Vec<_>>();
drop(state);
drop(timers_to_wake);
continue;
}
}
break;
}
self.state.lock().now = new_now;
}
}
@ -640,9 +660,6 @@ 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"),