Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/misc/component-async-tests/src/round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod non_concurrent_export_bindings {

impl bindings::local::local::baz::HostConcurrent for Ctx {
async fn foo<T>(_: &mut Accessor<T, Self>, s: String) -> wasmtime::Result<String> {
tokio::time::sleep(Duration::from_millis(10)).await;
crate::util::sleep(Duration::from_millis(10)).await;
Ok(format!("{s} - entered host - exited host"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/component-async-tests/src/round_trip_direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod bindings {

impl bindings::RoundTripDirectImportsConcurrent for Ctx {
async fn foo<T>(_: &mut Accessor<T, Self>, s: String) -> wasmtime::Result<String> {
tokio::time::sleep(Duration::from_millis(10)).await;
crate::util::sleep(Duration::from_millis(10)).await;
Ok(format!("{s} - entered host - exited host"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/component-async-tests/src/round_trip_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl bindings::local::local::many::HostConcurrent for Ctx {
Option<Stuff>,
Result<Stuff, ()>,
)> {
tokio::time::sleep(Duration::from_millis(10)).await;
crate::util::sleep(Duration::from_millis(10)).await;
Ok((
format!("{a} - entered host - exited host"),
b,
Expand Down
2 changes: 1 addition & 1 deletion crates/misc/component-async-tests/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wasmtime::component::bindgen!({

impl local::local::sleep::HostConcurrent for Ctx {
async fn sleep_millis<T>(_: &mut Accessor<T, Self>, time_in_millis: u64) {
tokio::time::sleep(Duration::from_millis(time_in_millis)).await;
crate::util::sleep(Duration::from_millis(time_in_millis)).await;
}
}

Expand Down
73 changes: 67 additions & 6 deletions crates/misc/component-async-tests/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,60 @@ pub fn config() -> Config {
config
}

pub async fn sleep(duration: std::time::Duration) {
if cfg!(miri) {
// TODO: We should be able to use `tokio::time::sleep` here, but as of
// this writing the miri-compatible version of `wasmtime-fiber` uses
// threads behind the scenes, which means thread-local storage is not
// preserved when we switch fibers, and that confuses Tokio. If we ever
// fix that we can stop using our own, special version of `sleep` and
// switch back to the Tokio version.

use std::{
future,
sync::{
Arc, Mutex,
atomic::{AtomicU32, Ordering::SeqCst},
},
task::Poll,
thread,
};

let state = Arc::new(AtomicU32::new(0));
let waker = Arc::new(Mutex::new(None));
let mut join_handle = None;
future::poll_fn(move |cx| match state.load(SeqCst) {
0 => {
state.store(1, SeqCst);
let state = state.clone();
*waker.lock().unwrap() = Some(cx.waker().clone());
let waker = waker.clone();
join_handle = Some(thread::spawn(move || {
thread::sleep(duration);
state.store(2, SeqCst);
let waker = waker.lock().unwrap().clone().unwrap();
waker.wake();
}));
Poll::Pending
}
1 => {
*waker.lock().unwrap() = Some(cx.waker().clone());
Poll::Pending
}
2 => {
if let Some(handle) = join_handle.take() {
_ = handle.join();
}
Poll::Ready(())
}
_ => unreachable!(),
})
.await;
} else {
tokio::time::sleep(duration).await;
}
}

/// Compose two components
///
/// a is the "root" component, and b is composed into it
Expand Down Expand Up @@ -166,7 +220,11 @@ pub async fn test_run(components: &[&str]) -> Result<()> {

pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()> {
let mut config = config();
config.epoch_interruption(true);
// As of this writing, miri/pulley/epochs is a problematic combination, so
// we don't test it.
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
config.epoch_interruption(true);
}

let engine = Engine::new(&config)?;

Expand Down Expand Up @@ -198,12 +256,15 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
wakers: Arc::new(std::sync::Mutex::new(None)),
},
);
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
}

let instance = linker.instantiate_async(&mut store, &component).await?;
let yield_host = super::yield_host::bindings::YieldHost::new(&mut store, &instance)?;
Expand Down
20 changes: 14 additions & 6 deletions crates/misc/component-async-tests/tests/scenario/borrowing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::sync::{Arc, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -65,7 +66,11 @@ pub async fn async_borrowing_callee() -> Result<()> {

pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {
let mut config = config();
config.epoch_interruption(true);
// As of this writing, miri/pulley/epochs is a problematic combination, so
// we don't test it.
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
config.epoch_interruption(true);
}

let engine = Engine::new(&config)?;

Expand All @@ -88,12 +93,15 @@ pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {
wakers: Arc::new(Mutex::new(None)),
},
);
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
}

let instance = linker.instantiate_async(&mut store, &component).await?;
let borrowing_host =
Expand Down
19 changes: 13 additions & 6 deletions crates/misc/component-async-tests/tests/scenario/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ pub async fn compose(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
#[allow(unused)]
pub async fn test_run(component: &[u8]) -> Result<()> {
let mut config = config();
config.epoch_interruption(true);
// As of this writing, miri/pulley/epochs is a problematic combination, so
// we don't test it.
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
config.epoch_interruption(true);
}

let engine = Engine::new(&config)?;

Expand Down Expand Up @@ -80,12 +84,15 @@ pub async fn test_run(component: &[u8]) -> Result<()> {
wakers: Arc::new(Mutex::new(None)),
},
);
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
store.set_epoch_deadline(1);

std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(10));
engine.increment_epoch();
});
}

let yield_host = component_async_tests::yield_host::bindings::YieldHost::instantiate_async(
&mut store, &component, &linker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ pub async fn async_error_context() -> Result<()> {
test_run(&[test_programs_artifacts::ASYNC_ERROR_CONTEXT_COMPONENT]).await
}

#[tokio::test]
pub async fn async_error_context_callee() -> Result<()> {
test_run(&[test_programs_artifacts::ASYNC_ERROR_CONTEXT_COMPONENT]).await
}
// No-op function; we only test this by composing it in `async_error_context_caller`
#[allow(
dead_code,
reason = "here only to make the `assert_test_exists` macro happy"
)]
pub fn async_error_context_callee() {}

#[tokio::test]
pub async fn async_error_context_caller() -> Result<()> {
Expand Down
Loading