added a new test for comm

This commit is contained in:
2026-04-24 02:40:05 +00:00
parent 0bc7097c88
commit 759497ee70
7 changed files with 234 additions and 210 deletions

View File

@@ -62,7 +62,7 @@ mod test {
#[test]
fn not_called_if_finished() {
spin_on(false, async {
spin_on(async {
let (mut req_in, mut req_out) = mpsc::channel(0);
let (mut rep_in, mut rep_out) = mpsc::channel(0);
join(

View File

@@ -143,15 +143,26 @@ pub fn eprint_stream_events<'a, S: Stream + 'a>(
)
}
thread_local! {
static WAKE_LODUD: RefCell<bool> = const { RefCell::new(false) };
}
/// Equivalent to [spin_on], but also logs on wake
pub fn spin_on_loud<Fut: Future>(fut: Fut) -> Fut::Output {
let prev = WAKE_LODUD.replace(true);
let ret = spin_on(fut);
WAKE_LODUD.set(prev);
ret
}
struct SpinWaker {
repeat: AtomicBool,
loud: bool,
}
impl Wake for SpinWaker {
fn wake(self: Arc<Self>) {
self.repeat.store(true, Ordering::SeqCst);
if self.loud {
eprintln!("Triggered repeat for spin_on")
if WAKE_LODUD.with_borrow(|k| *k) {
eprintln!("{Label} Triggered repeat for spin_on")
}
}
}
@@ -160,11 +171,13 @@ impl Wake for SpinWaker {
/// keeps synchronously waking itself. This is useful for deterministic tests
/// that don't contain side effects or threading.
///
/// Use [spin_on_loud] to get messages on wake for debugging
///
/// # Panics
///
/// If the future doesn't wake itself and doesn't settle.
pub fn spin_on<Fut: Future>(loud: bool, f: Fut) -> Fut::Output {
let spin_waker = Arc::new(SpinWaker { repeat: AtomicBool::new(false), loud });
pub fn spin_on<Fut: Future>(f: Fut) -> Fut::Output {
let spin_waker = Arc::new(SpinWaker { repeat: AtomicBool::new(false) });
let mut f = pin!(f);
let waker = spin_waker.clone().into();
let mut cx = Context::from_waker(&waker);