Insert random delays when sending and receiving websocket messages in tests

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-01-24 14:38:24 -08:00
parent d241ab6370
commit 2b8685c1a2
6 changed files with 53 additions and 21 deletions

View file

@ -5,7 +5,7 @@ use collections::HashMap;
use parking_lot::Mutex;
use postage::{barrier, prelude::Stream as _};
use rand::prelude::*;
use smol::{channel, prelude::*, Executor, Timer};
use smol::{channel, future::yield_now, prelude::*, Executor, Timer};
use std::{
any::Any,
fmt::{self, Debug, Display},
@ -528,6 +528,17 @@ impl Background {
task.await;
}
}
pub async fn simulate_random_delay(&self) {
match self {
Self::Deterministic { executor, .. } => {
if executor.state.lock().rng.gen_range(0..100) < 20 {
yield_now().await;
}
}
_ => panic!("this method can only be called on a deterministic executor"),
}
}
}
pub struct Scope<'a> {