Path: blob/main/crates/misc/component-async-tests/tests/scenario/borrowing.rs
1693 views
use std::env;1use std::sync::{Arc, Mutex};2use std::time::Duration;34use super::util::{config, make_component};5use anyhow::Result;6use futures::stream::{FuturesUnordered, TryStreamExt};7use wasmtime::component::{Linker, ResourceTable};8use wasmtime::{Engine, Store};9use wasmtime_wasi::WasiCtxBuilder;1011#[tokio::test]12pub async fn async_borrowing_caller() -> Result<()> {13test_run_bool(14&[15test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,16test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT,17],18false,19)20.await21}2223#[tokio::test]24async fn async_borrowing_caller_misbehave() -> Result<()> {25let error = format!(26"{:?}",27test_run_bool(28&[29test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,30test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT31],32true33)34.await35.unwrap_err()36);37assert!(error.contains("unknown handle index"), "{error}");38Ok(())39}4041#[tokio::test]42async fn async_borrowing_callee_misbehave() -> Result<()> {43let error = format!(44"{:?}",45test_run_bool(46&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],47true48)49.await50.unwrap_err()51);52assert!(error.contains("unknown handle index"), "{error}");53Ok(())54}5556#[tokio::test]57pub async fn async_borrowing_callee() -> Result<()> {58test_run_bool(59&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],60false,61)62.await63}6465pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {66let mut config = config();67// As of this writing, miri/pulley/epochs is a problematic combination, so68// we don't test it.69if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {70config.epoch_interruption(true);71}7273let engine = Engine::new(&config)?;7475let component = make_component(&engine, components).await?;7677let mut linker = Linker::new(&engine);7879wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;80component_async_tests::borrowing_host::bindings::local::local::borrowing_types::add_to_linker::<81_,82component_async_tests::Ctx,83>(&mut linker, |ctx| ctx)?;8485let mut store = Store::new(86&engine,87component_async_tests::Ctx {88wasi: WasiCtxBuilder::new().inherit_stdio().build(),89table: ResourceTable::default(),90continue_: false,91wakers: Arc::new(Mutex::new(None)),92},93);9495if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {96store.set_epoch_deadline(1);9798std::thread::spawn(move || {99std::thread::sleep(Duration::from_secs(10));100engine.increment_epoch();101});102}103104let instance = linker.instantiate_async(&mut store, &component).await?;105let borrowing_host =106component_async_tests::borrowing_host::bindings::BorrowingHost::new(&mut store, &instance)?;107108instance109.run_concurrent(&mut store, async move |accessor| {110// Start three concurrent calls and then join them all:111let mut futures = FuturesUnordered::new();112for _ in 0..3 {113futures.push(borrowing_host.local_local_run_bool().call_run(accessor, v));114}115116while let Some(()) = futures.try_next().await? {117// continue118}119120anyhow::Ok(())121})122.await?123}124125126