Path: blob/main/crates/misc/component-async-tests/tests/scenario/borrowing.rs
3089 views
use std::env;1use std::time::Duration;23use super::util::{config, make_component};4use futures::stream::{FuturesUnordered, TryStreamExt};5use wasmtime::Result;6use wasmtime::component::{Linker, ResourceTable};7use wasmtime::{Engine, Store};8use wasmtime_wasi::WasiCtxBuilder;910#[tokio::test]11pub async fn async_borrowing_caller() -> Result<()> {12test_run_bool(13&[14test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,15test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT,16],17false,18)19.await20}2122#[tokio::test]23async fn async_borrowing_caller_misbehave() -> Result<()> {24let error = format!(25"{:?}",26test_run_bool(27&[28test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,29test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT30],31true32)33.await34.unwrap_err()35);36assert!(error.contains("unknown handle index"), "{error}");37Ok(())38}3940#[tokio::test]41async fn async_borrowing_callee_misbehave() -> Result<()> {42let error = format!(43"{:?}",44test_run_bool(45&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],46true47)48.await49.unwrap_err()50);51assert!(error.contains("unknown handle index"), "{error}");52Ok(())53}5455#[tokio::test]56pub async fn async_borrowing_callee() -> Result<()> {57test_run_bool(58&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],59false,60)61.await62}6364pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {65let mut config = config();66// As of this writing, miri/pulley/epochs is a problematic combination, so67// we don't test it.68if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {69config.epoch_interruption(true);70}7172let engine = Engine::new(&config)?;7374let component = make_component(&engine, components).await?;7576let mut linker = Linker::new(&engine);7778wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;79component_async_tests::borrowing_host::bindings::local::local::borrowing_types::add_to_linker::<80_,81component_async_tests::Ctx,82>(&mut linker, |ctx| ctx)?;8384let mut store = Store::new(85&engine,86component_async_tests::Ctx {87wasi: WasiCtxBuilder::new().inherit_stdio().build(),88table: ResourceTable::default(),89continue_: false,90},91);9293if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {94store.set_epoch_deadline(1);9596std::thread::spawn(move || {97std::thread::sleep(Duration::from_secs(10));98engine.increment_epoch();99});100}101102let borrowing_host =103component_async_tests::borrowing_host::bindings::BorrowingHost::instantiate_async(104&mut store, &component, &linker,105)106.await?;107108store109.run_concurrent(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}119120wasmtime::error::Ok(())121})122.await?123}124125126