Co-Authored-By: Max <max@zed.dev>
Co-Authored-By: Conrad <conrad.irwin@gmail.com>
Co-Authored-By: Kyle <kyle@zed.dev>
This commit is contained in:
Antonio Scandurra
2023-10-25 19:17:50 +02:00
co-authored by Max Conrad Kyle
parent 9986e526e5
commit 412c0ff7dc
9 changed files with 1810 additions and 10 deletions
+2 -2
View File
@@ -207,8 +207,8 @@ impl Executor {
}
#[cfg(any(test, feature = "test-support"))]
pub async fn simulate_random_delay(&self) {
todo!("simulate_random_delay")
pub fn simulate_random_delay(&self) -> impl Future<Output = ()> {
self.dispatcher.as_test().unwrap().simulate_random_delay()
}
pub fn num_cpus(&self) -> usize {
+4 -1
View File
@@ -162,8 +162,11 @@ pub trait PlatformDispatcher: Send + Sync {
fn dispatch_on_main_thread(&self, runnable: Runnable);
fn dispatch_after(&self, duration: Duration, runnable: Runnable);
fn poll(&self) -> bool;
#[cfg(any(test, feature = "test-support"))]
fn advance_clock(&self, duration: Duration);
fn as_test(&self) -> Option<&TestDispatcher> {
None
}
}
pub trait PlatformTextSystem: Send + Sync {
@@ -71,11 +71,6 @@ impl PlatformDispatcher for MacDispatcher {
fn poll(&self) -> bool {
false
}
#[cfg(any(test, feature = "test-support"))]
fn advance_clock(&self, _: Duration) {
unimplemented!()
}
}
extern "C" fn trampoline(runnable: *mut c_void) {
+33 -2
View File
@@ -4,7 +4,10 @@ use collections::{BTreeMap, HashMap, VecDeque};
use parking_lot::Mutex;
use rand::prelude::*;
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::{Duration, Instant},
};
use util::post_inc;
@@ -44,6 +47,34 @@ impl TestDispatcher {
state: Arc::new(Mutex::new(state)),
}
}
pub fn advance_clock(&self, by: Duration) {
self.state.lock().time += by;
}
pub fn simulate_random_delay(&self) -> impl Future<Output = ()> {
pub struct YieldNow {
count: usize,
}
impl Future for YieldNow {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
if self.count > 0 {
self.count -= 1;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
}
}
YieldNow {
count: self.state.lock().random.gen_range(0..10),
}
}
}
impl Clone for TestDispatcher {
@@ -131,8 +162,8 @@ impl PlatformDispatcher for TestDispatcher {
true
}
fn advance_clock(&self, by: Duration) {
self.state.lock().time += by;
fn as_test(&self) -> Option<&TestDispatcher> {
Some(self)
}
}