Path: blob/main/crates/test-programs/src/bin/async_backpressure_caller.rs
3069 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "backpressure-caller",4});56use super::Component;7export!(Component);8}910use {11bindings::{12exports::local::local::run::Guest,13local::local::{backpressure, run},14},15futures::future,16std::{17future::Future,18pin::Pin,19task::{Context, Poll},20},21};2223struct Component;2425impl Guest for Component {26async fn run() {27test(28|| backpressure::set_backpressure(true),29|| backpressure::set_backpressure(false),30)31.await;32test(33|| backpressure::inc_backpressure(),34|| backpressure::dec_backpressure(),35)36.await;37}38}3940async fn test(enable: impl Fn(), disable: impl Fn()) {41enable();4243let mut a = Some(Box::pin(run::run()));44let mut b = Some(Box::pin(run::run()));45let mut c = Some(Box::pin(run::run()));4647let mut backpressure_is_set = true;48future::poll_fn(move |cx| {49let a_ready = is_ready(cx, &mut a);50let b_ready = is_ready(cx, &mut b);51let c_ready = is_ready(cx, &mut c);5253if backpressure_is_set {54assert!(!a_ready);55assert!(!b_ready);56assert!(!c_ready);5758disable();59backpressure_is_set = false;6061Poll::Pending62} else if a_ready && b_ready && c_ready {63Poll::Ready(())64} else {65Poll::Pending66}67})68.await69}7071fn is_ready(cx: &mut Context, fut: &mut Option<Pin<Box<impl Future<Output = ()>>>>) -> bool {72if let Some(v) = fut.as_mut() {73if v.as_mut().poll(cx).is_ready() {74*fut = None;75true76} else {77false78}79} else {80true81}82}8384// Unused function; required since this file is built as a `bin`:85fn main() {}868788