Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit a575114

Browse files
committed
snapshot
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 32a062c commit a575114

10 files changed

Lines changed: 98 additions & 30 deletions

File tree

crates/misc/component-async-tests/src/round_trip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub mod non_concurrent_export_bindings {
2525

2626
impl bindings::local::local::baz::HostConcurrent for Ctx {
2727
async fn foo<T>(_: &mut Accessor<T, Self>, s: String) -> wasmtime::Result<String> {
28-
tokio::time::sleep(Duration::from_millis(10)).await;
28+
crate::util::sleep(Duration::from_millis(10)).await;
2929
Ok(format!("{s} - entered host - exited host"))
3030
}
3131
}

crates/misc/component-async-tests/src/round_trip_direct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod bindings {
1515

1616
impl bindings::RoundTripDirectImportsConcurrent for Ctx {
1717
async fn foo<T>(_: &mut Accessor<T, Self>, s: String) -> wasmtime::Result<String> {
18-
tokio::time::sleep(Duration::from_millis(10)).await;
18+
crate::util::sleep(Duration::from_millis(10)).await;
1919
Ok(format!("{s} - entered host - exited host"))
2020
}
2121
}

crates/misc/component-async-tests/src/round_trip_many.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl bindings::local::local::many::HostConcurrent for Ctx {
4747
Option<Stuff>,
4848
Result<Stuff, ()>,
4949
)> {
50-
tokio::time::sleep(Duration::from_millis(10)).await;
50+
crate::util::sleep(Duration::from_millis(10)).await;
5151
Ok((
5252
format!("{a} - entered host - exited host"),
5353
b,

crates/misc/component-async-tests/src/sleep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ wasmtime::component::bindgen!({
1616

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

crates/misc/component-async-tests/src/util.rs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ pub fn config() -> Config {
3030
config.memory_reservation(1 << 20);
3131
config.memory_guard_size(0);
3232
config.signals_based_traps(false);
33-
config.debug_info(false);
3433
} else {
3534
config.cranelift_debug_verifier(true);
36-
config.debug_info(true);
3735
}
3836
config.wasm_component_model(true);
3937
config.wasm_component_model_async(true);
@@ -44,6 +42,54 @@ pub fn config() -> Config {
4442
config
4543
}
4644

45+
pub async fn sleep(duration: std::time::Duration) {
46+
if cfg!(miri) {
47+
// TODO: We should be able to use `tokio::time::sleep` here, but as of
48+
// this writing the miri-compatible version of `wasmtime-fiber` uses
49+
// threads behind the scenes, which means thread-local storage is not
50+
// preserved when we switch fibers, and that confuses Tokio. If we ever
51+
// fix that we can stop using our own, special version of `sleep` and
52+
// switch back to the Tokio version.
53+
54+
use std::{
55+
future,
56+
sync::{
57+
Arc, Mutex,
58+
atomic::{AtomicU32, Ordering::SeqCst},
59+
},
60+
task::Poll,
61+
thread,
62+
};
63+
64+
let state = Arc::new(AtomicU32::new(0));
65+
let waker = Arc::new(Mutex::new(None));
66+
future::poll_fn(move |cx| match state.load(SeqCst) {
67+
0 => {
68+
state.store(1, SeqCst);
69+
let state = state.clone();
70+
*waker.lock().unwrap() = Some(cx.waker().clone());
71+
let waker = waker.clone();
72+
thread::spawn(move || {
73+
thread::sleep(duration);
74+
state.store(2, SeqCst);
75+
let waker = waker.lock().unwrap().clone().unwrap();
76+
waker.wake();
77+
});
78+
Poll::Pending
79+
}
80+
1 => {
81+
*waker.lock().unwrap() = Some(cx.waker().clone());
82+
Poll::Pending
83+
}
84+
2 => Poll::Ready(()),
85+
_ => unreachable!(),
86+
})
87+
.await;
88+
} else {
89+
tokio::time::sleep(duration).await;
90+
}
91+
}
92+
4793
/// Compose two components
4894
///
4995
/// a is the "root" component, and b is composed into it
@@ -168,7 +214,11 @@ pub async fn test_run(components: &[&str]) -> Result<()> {
168214

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

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

@@ -200,12 +250,15 @@ pub async fn test_run_with_count(components: &[&str], count: usize) -> Result<()
200250
wakers: Arc::new(std::sync::Mutex::new(None)),
201251
},
202252
);
203-
store.set_epoch_deadline(1);
204253

205-
std::thread::spawn(move || {
206-
std::thread::sleep(Duration::from_secs(10));
207-
engine.increment_epoch();
208-
});
254+
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
255+
store.set_epoch_deadline(1);
256+
257+
std::thread::spawn(move || {
258+
std::thread::sleep(Duration::from_secs(10));
259+
engine.increment_epoch();
260+
});
261+
}
209262

210263
let instance = linker.instantiate_async(&mut store, &component).await?;
211264
let yield_host = super::yield_host::bindings::YieldHost::new(&mut store, &instance)?;

crates/misc/component-async-tests/tests/scenario/borrowing.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::sync::{Arc, Mutex};
23
use std::time::Duration;
34

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

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

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

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

93-
std::thread::spawn(move || {
94-
std::thread::sleep(Duration::from_secs(10));
95-
engine.increment_epoch();
96-
});
97+
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
98+
store.set_epoch_deadline(1);
99+
100+
std::thread::spawn(move || {
101+
std::thread::sleep(Duration::from_secs(10));
102+
engine.increment_epoch();
103+
});
104+
}
97105

98106
let instance = linker.instantiate_async(&mut store, &component).await?;
99107
let borrowing_host =

crates/misc/component-async-tests/tests/scenario/common.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ pub async fn compose(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
4949
#[allow(unused)]
5050
pub async fn test_run(component: &[u8]) -> Result<()> {
5151
let mut config = config();
52-
config.epoch_interruption(true);
52+
// As of this writing, miri/pulley/epochs is a problematic combination, so
53+
// we don't test it.
54+
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
55+
config.epoch_interruption(true);
56+
}
5357

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

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

85-
std::thread::spawn(move || {
86-
std::thread::sleep(Duration::from_secs(10));
87-
engine.increment_epoch();
88-
});
88+
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
89+
store.set_epoch_deadline(1);
90+
91+
std::thread::spawn(move || {
92+
std::thread::sleep(Duration::from_secs(10));
93+
engine.increment_epoch();
94+
});
95+
}
8996

9097
let yield_host = component_async_tests::yield_host::bindings::YieldHost::instantiate_async(
9198
&mut store, &component, &linker,

crates/misc/component-async-tests/tests/scenario/round_trip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use wasmtime_wasi::p2::WasiCtxBuilder;
1717

1818
use component_async_tests::Ctx;
1919

20-
pub use component_async_tests::util::{config, make_component};
20+
pub use component_async_tests::util::{config, make_component, sleep};
2121

2222
#[tokio::test]
2323
pub async fn async_round_trip_stackful() -> Result<()> {
@@ -364,7 +364,7 @@ pub async fn test_round_trip(
364364
.instance("local:local/baz")?
365365
.func_new_concurrent("[async]foo", |_, params| {
366366
Box::pin(async move {
367-
tokio::time::sleep(Duration::from_millis(10)).await;
367+
sleep(Duration::from_millis(10)).await;
368368
let Some(Val::String(s)) = params.into_iter().next() else {
369369
unreachable!()
370370
};

crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use wasmtime::{Engine, Store};
99
use wasmtime_wasi::p2::WasiCtxBuilder;
1010

1111
use component_async_tests::Ctx;
12-
use component_async_tests::util::config;
12+
use component_async_tests::util::{config, sleep};
1313

1414
#[tokio::test]
1515
pub async fn async_direct_stackless() -> Result<()> {
@@ -90,7 +90,7 @@ async fn test_round_trip_direct(
9090
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
9191
linker.root().func_new_concurrent("foo", |_, params| {
9292
Box::pin(async move {
93-
tokio::time::sleep(Duration::from_millis(10)).await;
93+
sleep(Duration::from_millis(10)).await;
9494
let Some(Val::String(s)) = params.into_iter().next() else {
9595
unreachable!()
9696
};

crates/misc/component-async-tests/tests/scenario/round_trip_many.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use wasmtime::{Engine, Store};
1212
use wasmtime_wasi::p2::WasiCtxBuilder;
1313

1414
use component_async_tests::Ctx;
15-
use component_async_tests::util::{config, make_component};
15+
use component_async_tests::util::{config, make_component, sleep};
1616

1717
#[tokio::test]
1818
pub async fn async_round_trip_many_stackless() -> Result<()> {
@@ -321,7 +321,7 @@ async fn test_round_trip_many(
321321
.instance("local:local/many")?
322322
.func_new_concurrent("[async]foo", |_, params| {
323323
Box::pin(async move {
324-
tokio::time::sleep(Duration::from_millis(10)).await;
324+
sleep(Duration::from_millis(10)).await;
325325
let mut params = params.into_iter();
326326
let Some(Val::String(s)) = params.next() else {
327327
unreachable!()

0 commit comments

Comments
 (0)