use camino::{Utf8Path, Utf8PathBuf};
use camino_tempfile::Utf8TempDir;
use futures::channel::{mpsc, oneshot};
use futures::{Sink, Stream, StreamExt};
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::process::Command;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll};
use std::time::Duration;
use wasm_rquickjs::{
EmbeddingMode, GenerationTarget, JsModuleSpec, generate_wrapper_crate_with_target,
};
use wasmtime::component::{
Access, Accessor, Component, Destination, FutureConsumer, FutureProducer, FutureReader,
HasSelf, Lift, Linker, Lower, Source, StreamConsumer, StreamProducer, StreamReader,
StreamResult, bindgen,
};
use wasmtime::{Config, Engine, Result, Store, StoreContextMut};
use wasmtime_wasi::p2::pipe::MemoryOutputPipe;
use wasmtime_wasi::{ResourceTable, WasiCtxBuilder, WasiCtxView};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpView};
mod p3_build;
mod export_bindings {
use wasmtime::component::bindgen;
bindgen!({
world: "async-values",
path: "examples/p3/async-values/wit",
with: {
"test:async-values/observer.wrapped-stream": super::WrappedStream,
},
exports: { default: async },
});
}
use export_bindings::AsyncValues;
bindgen!({
world: "async-values-import",
path: "examples/p3/async-values-import/wit",
imports: {
"test:async-values-import/host.make-future": store,
"test:async-values-import/host.make-stream": store,
},
});
struct Host {
wasi: wasmtime_wasi::WasiCtx,
http: WasiHttpCtx,
table: ResourceTable,
stored_future: Option<oneshot::Receiver<u32>>,
stored_optional_future: Option<oneshot::Receiver<Option<u32>>>,
pending_optional_future: Option<oneshot::Sender<Option<u32>>>,
export_gates: Vec<Option<ExportGate>>,
cleanup_notification: Option<oneshot::Sender<()>>,
nested_cleanup_calls: usize,
wrapped_drops: usize,
byte_echo_calls: usize,
stderr: MemoryOutputPipe,
#[cfg(feature = "use-golem-wasmtime")]
io_ctx: wasmtime_wasi::IoCtx,
}
struct ExportGate {
reached: oneshot::Sender<()>,
release: oneshot::Receiver<()>,
}
pub struct WrappedStream(StreamReader<u8>);
impl wasmtime_wasi::WasiView for Host {
fn ctx(&mut self) -> WasiCtxView<'_> {
WasiCtxView {
ctx: &mut self.wasi,
table: &mut self.table,
#[cfg(feature = "use-golem-wasmtime")]
io_ctx: &mut self.io_ctx,
}
}
}
impl WasiHttpView for Host {
fn http(&mut self) -> WasiHttpCtxView<'_> {
WasiHttpCtxView {
ctx: &mut self.http,
table: &mut self.table,
hooks: wasmtime_wasi_http::default_hooks(),
}
}
}
impl test::async_values_import::host::Host for Host {
fn cleanup_complete(&mut self) {
self.nested_cleanup_calls += 1;
}
}
impl export_bindings::test::async_values::observer::Host for Host {
fn cleanup_complete(&mut self) {
self.nested_cleanup_calls += 1;
if let Some(notification) = self.cleanup_notification.take() {
let _ = notification.send(());
}
}
}
impl<T> export_bindings::test::async_values::observer::HostWithStore<T> for HasSelf<Host> {
async fn gate(accessor: &Accessor<T, Self>, id: u8) {
let gate = accessor.with(|mut access| {
access
.get()
.export_gates
.get_mut(id as usize)
.and_then(Option::take)
});
let gate = gate.unwrap_or_else(|| panic!("export gate {id} was not configured"));
let _ = gate.reached.send(());
gate.release
.await
.unwrap_or_else(|_| panic!("export gate {id} release was dropped"));
}
}
impl export_bindings::test::async_values::observer::HostWrappedStream for Host {
fn drop(&mut self, rep: wasmtime::component::Resource<WrappedStream>) -> Result<()> {
self.table.delete(rep)?;
self.wrapped_drops += 1;
Ok(())
}
}
impl<T> export_bindings::test::async_values::observer::HostWrappedStreamWithStore<T>
for HasSelf<Host>
{
async fn wrap(
accessor: &Accessor<T, Self>,
reader: StreamReader<u8>,
) -> wasmtime::component::Resource<WrappedStream> {
accessor
.with(|mut access| access.get().table.push(WrappedStream(reader)))
.expect("failed to store wrapped stream")
}
}
impl<T> test::async_values_import::host::HostWithStore<T> for HasSelf<Host> {
async fn make_pending_future(
accessor: &Accessor<T, Self>,
) -> test::async_values_import::host::OptionalFuture {
accessor.with(|mut access| {
let (tx, rx) = oneshot::channel();
access.get().pending_optional_future = Some(tx);
let value = FutureReader::new(&mut access, OneshotProducer::new(rx))
.expect("failed to create pending future");
test::async_values_import::host::OptionalFuture { value }
})
}
async fn consume_optional_future(
accessor: &Accessor<T, Self>,
f: FutureReader<Option<u32>>,
) -> Option<u32> {
let (tx, rx) = oneshot::channel();
accessor
.with(|access| f.pipe(access, OneshotConsumer::new(tx)))
.expect("failed to pipe optional host future");
rx.await.expect("optional host future did not resolve")
}
async fn consume_nested_optional_future(
accessor: &Accessor<T, Self>,
f: test::async_values_import::host::OptionalFuture,
) -> Option<u32> {
Self::consume_optional_future(accessor, f.value).await
}
async fn store_optional_future(accessor: &Accessor<T, Self>, f: FutureReader<Option<u32>>) {
let (tx, rx) = oneshot::channel();
accessor
.with(|mut access| {
access.get().stored_optional_future = Some(rx);
f.pipe(access, OneshotConsumer::new(tx))
})
.expect("failed to store optional host future");
}
async fn echo_bytes(accessor: &Accessor<T, Self>, bytes: Vec<u8>) -> Vec<u8> {
accessor.with(|mut access| access.get().byte_echo_calls += 1);
tokio::time::sleep(Duration::from_millis(10)).await;
bytes
}
fn make_future(mut host: Access<T, Self>, x: u32) -> FutureReader<u32> {
let (tx, rx) = oneshot::channel();
let _ = tx.send(x);
FutureReader::new(&mut host, OneshotProducer::new(rx)).expect("failed to create future")
}
fn make_stream(mut host: Access<T, Self>, n: u32) -> StreamReader<u8> {
let items: Vec<u8> = (1..=n).map(|i| i as u8).collect();
StreamReader::new(&mut host, PipeProducer::new(futures::stream::iter(items)))
.expect("failed to create stream")
}
async fn consume_future(accessor: &Accessor<T, Self>, f: FutureReader<u32>) -> u32 {
let (tx, rx) = oneshot::channel();
accessor
.with(|access| f.pipe(access, OneshotConsumer::new(tx)))
.expect("failed to pipe host future");
rx.await.expect("host future did not resolve")
}
async fn consume_stream(accessor: &Accessor<T, Self>, s: StreamReader<u8>) -> u32 {
let (tx, rx) = mpsc::channel::<u8>(16);
accessor
.with(|access| s.pipe(access, PipeConsumer::new(tx)))
.expect("failed to pipe host stream");
rx.map(|b| b as u32)
.fold(0u32, |acc, v| async move { acc + v })
.await
}
async fn check_future(
accessor: &Accessor<T, Self>,
f: FutureReader<u32>,
) -> Result<u32, String> {
let value = Self::consume_future(accessor, f).await;
if value == 13 {
Err("unlucky".to_string())
} else {
Ok(value + 1)
}
}
async fn delayed_future(accessor: &Accessor<T, Self>, f: FutureReader<u32>) -> u32 {
let value = Self::consume_future(accessor, f).await;
tokio::time::sleep(Duration::from_millis(100)).await;
value
}
async fn consume_stream_prefix(accessor: &Accessor<T, Self>, s: StreamReader<u8>) -> u8 {
let (tx, rx) = oneshot::channel();
accessor
.with(|access| s.pipe(access, PrefixConsumer::new(tx)))
.expect("failed to pipe prefix stream");
rx.await.expect("prefix stream did not yield an item")
}
async fn consume_stream_with_backpressure(accessor: &Accessor<T, Self>, s: StreamReader<u8>) {
accessor
.with(|access| s.pipe(access, BackpressureDropConsumer::default()))
.expect("failed to pipe backpressure stream");
}
async fn store_future(accessor: &Accessor<T, Self>, f: FutureReader<u32>) {
let (tx, rx) = oneshot::channel();
accessor
.with(|mut access| {
access.get().stored_future = Some(rx);
f.pipe(access, OneshotConsumer::new(tx))
})
.expect("failed to pipe stored host future");
}
async fn read_stored_future(accessor: &Accessor<T, Self>) -> u32 {
let rx = accessor
.with(|mut access| access.get().stored_future.take())
.expect("no stored host future");
rx.await.expect("stored host future did not resolve")
}
}
fn new_store(engine: &Engine) -> Store<Host> {
let stderr = MemoryOutputPipe::new(1024 * 1024);
let mut builder = WasiCtxBuilder::new();
builder.inherit_stdout().stderr(stderr.clone());
builder
.preopened_dir("examples/p3", "/", wasmtime_wasi::FsPerms::ReadOnly)
.expect("failed to preopen fixture directory");
#[cfg(feature = "use-golem-wasmtime")]
let (wasi, io_ctx) = builder.build();
#[cfg(not(feature = "use-golem-wasmtime"))]
let wasi = builder.build();
Store::new(
engine,
Host {
wasi,
http: WasiHttpCtx::new(),
table: ResourceTable::new(),
stored_future: None,
stored_optional_future: None,
pending_optional_future: None,
export_gates: Vec::new(),
cleanup_notification: None,
nested_cleanup_calls: 0,
wrapped_drops: 0,
byte_echo_calls: 0,
stderr,
#[cfg(feature = "use-golem-wasmtime")]
io_ctx,
},
)
}
fn engine() -> Result<Engine> {
let mut config = Config::new();
config.wasm_component_model(true);
config.wasm_component_model_async(true);
config.consume_fuel(false);
Engine::new(&config)
}
fn base_linker(engine: &Engine) -> Result<Linker<Host>> {
let mut linker = Linker::<Host>::new(engine);
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
wasmtime_wasi::p3::add_to_linker(&mut linker)?;
wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
export_bindings::test::async_values::observer::add_to_linker::<Host, HasSelf<Host>>(
&mut linker,
|host| host,
)?;
add_wasi_logging_stub(&mut linker)?;
Ok(linker)
}
#[derive(wasmtime::component::ComponentType, wasmtime::component::Lift)]
#[component(enum)]
#[repr(u8)]
#[allow(dead_code)]
enum LogLevel {
#[component(name = "trace")]
Trace,
#[component(name = "debug")]
Debug,
#[component(name = "info")]
Info,
#[component(name = "warn")]
Warn,
#[component(name = "error")]
Error,
#[component(name = "critical")]
Critical,
}
fn add_wasi_logging_stub(linker: &mut Linker<Host>) -> Result<()> {
let mut logging = linker.instance("wasi:logging/logging")?;
logging.func_wrap(
"log",
|_ctx: StoreContextMut<'_, Host>,
(_level, _context, _message): (LogLevel, String, String)|
-> Result<(), wasmtime::Error> { Ok(()) },
)?;
Ok(())
}
async fn run_export_world(
component_path: &Utf8Path,
) -> Result<(
u32,
Vec<u8>,
String,
u32,
Vec<u8>,
Vec<u8>,
String,
u32,
u32,
u32,
)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let run_future = {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<u32> {
let reader: FutureReader<u32> = bindings.call_run_future(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, OneshotConsumer::new(tx)))?;
Ok(rx.await?)
})
.await??
};
let checkpoint_count = {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<u32> {
let reader: FutureReader<u32> =
bindings.call_run_checkpoint_future(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, OneshotConsumer::new(tx)))?;
assert_eq!(rx.await?, 7, "checkpoint future should resolve to 7");
bindings.call_read_checkpoint_count(accessor).await
})
.await??
};
let run_stream = {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<Vec<u8>> {
let reader: StreamReader<u8> = bindings.call_run_stream(accessor).await?;
let (tx, rx) = mpsc::channel::<u8>(16);
accessor.with(|access| reader.pipe(access, PipeConsumer::new(tx)))?;
Ok(rx.collect::<Vec<u8>>().await)
})
.await??
};
let (nested_label, nested_future, nested_stdout, nested_stderr) = {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(
async move |accessor| -> Result<(String, u32, Vec<u8>, Vec<u8>)> {
let nested = bindings
.call_run_nested(accessor)
.await?
.expect("run-nested should return the success arm");
let (future_tx, future_rx) = oneshot::channel();
accessor.with(|access| {
nested
.future_value
.pipe(access, OneshotConsumer::new(future_tx))
})?;
let stdout = nested.stdout.expect("nested stdout should be present");
let (stdout_tx, stdout_rx) = mpsc::channel::<u8>(16);
accessor.with(|access| stdout.pipe(access, PipeConsumer::new(stdout_tx)))?;
let stderr = nested.stderr.expect("nested stderr should be present");
let (stderr_tx, stderr_rx) = mpsc::channel::<u8>(16);
accessor.with(|access| stderr.pipe(access, PipeConsumer::new(stderr_tx)))?;
let (future_value, stdout, stderr) = futures::join!(
future_rx,
stdout_rx.collect::<Vec<_>>(),
stderr_rx.collect::<Vec<_>>(),
);
Ok((nested.label, future_value?, stdout, stderr))
},
)
.await??
};
let nested_error = {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<String> {
match bindings.call_run_nested_error(accessor).await? {
Ok(_) => panic!("run-nested-error should return the error arm"),
Err(error) => Ok(error),
}
})
.await??
};
let take_future = {
let mut store = new_store(&engine);
let (tx, rx) = oneshot::channel::<u32>();
tx.send(41).expect("oneshot send failed");
let reader = FutureReader::new(&mut store, OneshotProducer::new(rx))?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| bindings.call_take_future(accessor, reader).await)
.await??
};
let take_stream = {
let mut store = new_store(&engine);
let reader = StreamReader::new(
&mut store,
PipeProducer::new(futures::stream::iter(vec![10u8, 20, 30])),
)?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| bindings.call_take_stream(accessor, reader).await)
.await??
};
Ok((
run_future,
run_stream,
nested_label,
nested_future,
nested_stdout,
nested_stderr,
nested_error,
take_future,
take_stream,
checkpoint_count,
))
}
async fn run_export_stream_lifecycle(
component_path: &Utf8Path,
) -> Result<(
String,
u8,
String,
String,
String,
usize,
usize,
usize,
usize,
usize,
)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let completion = {
let mut store = new_store(&engine);
let reader = StreamReader::new(
&mut store,
PipeProducer::new(futures::stream::iter(vec![1u8, 2, 3])),
)?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
bindings
.call_inspect_stream_completion(accessor, reader)
.await
})
.await??
};
let (early_break, break_drops) = {
let observation = Arc::new(ProducerObservation::default());
let mut store = new_store(&engine);
let reader = StreamReader::new(&mut store, ObservedProducer::new(observation.clone()))?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let value = store
.run_concurrent(async move |accessor| {
bindings.call_break_stream(accessor, reader).await
})
.await??;
(value, observation.drops.load(Ordering::SeqCst))
};
let (returned, return_drops) = {
let observation = Arc::new(ProducerObservation::default());
let mut store = new_store(&engine);
let reader = StreamReader::new(&mut store, ObservedProducer::new(observation.clone()))?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let value = store
.run_concurrent(async move |accessor| {
bindings.call_return_stream(accessor, reader).await
})
.await??;
(value, observation.drops.load(Ordering::SeqCst))
};
let (thrown, throw_drops) = {
let observation = Arc::new(ProducerObservation::default());
let mut store = new_store(&engine);
let reader = StreamReader::new(&mut store, ObservedProducer::new(observation.clone()))?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let value = store
.run_concurrent(async move |accessor| {
bindings.call_throw_stream(accessor, reader).await
})
.await??;
(value, observation.drops.load(Ordering::SeqCst))
};
let (pending_close, pending_polls, pending_drops) = {
let observation = Arc::new(ProducerObservation::default());
let mut store = new_store(&engine);
let reader = StreamReader::new(
&mut store,
PendingObservedProducer::new(observation.clone()),
)?;
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let value = store
.run_concurrent(async move |accessor| {
bindings.call_close_pending_stream(accessor, reader).await
})
.await??;
(
value,
observation.polls.load(Ordering::SeqCst),
observation.drops.load(Ordering::SeqCst),
)
};
Ok((
completion,
early_break,
returned,
thrown,
pending_close,
break_drops,
return_drops,
throw_drops,
pending_polls,
pending_drops,
))
}
async fn run_export_stream_producer_cleanup(component_path: &Utf8Path) -> Result<(u8, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
let reader = bindings.call_run_observed_stream(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, PrefixConsumer::new(tx)))?;
let first = rx.await?;
let state = bindings.call_read_observed_stream_state(accessor).await?;
Ok((first, state))
})
.await?
}
async fn run_export_stream_conversion_failure(
component_path: &Utf8Path,
) -> Result<(Result<Vec<u8>>, usize, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let result = match store
.run_concurrent(async move |accessor| -> Result<Vec<u8>> {
let reader = bindings.call_run_invalid_observed_stream(accessor).await?;
let (tx, rx) = mpsc::channel(16);
accessor.with(|access| reader.pipe(access, PipeConsumer::new(tx)))?;
Ok(rx.collect().await)
})
.await
{
Ok(result) => result,
Err(error) => Err(error),
};
let cleanup_calls = store.data().nested_cleanup_calls;
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((result, cleanup_calls, stderr))
}
async fn run_export_sibling_streams(component_path: &Utf8Path) -> Result<(Vec<u8>, Vec<u8>)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
let (first, second) = bindings.call_run_sibling_streams(accessor).await?;
let (first_tx, first_rx) = mpsc::channel(16);
let (second_tx, second_rx) = mpsc::channel(16);
accessor.with(|access| first.pipe(access, PipeConsumer::new(first_tx)))?;
accessor.with(|access| second.pipe(access, PipeConsumer::new(second_tx)))?;
Ok(futures::future::join(first_rx.collect(), second_rx.collect()).await)
})
.await?
}
async fn run_export_wrapped_sibling_streams(
component_path: &Utf8Path,
) -> Result<(Vec<u8>, Vec<u8>)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
let (first, second) = bindings.call_run_wrapped_sibling_streams(accessor).await?;
let (first, second) = accessor.with(|mut access| {
let first = access.get().table.delete(first)?.0;
let second = access.get().table.delete(second)?.0;
Result::<_>::Ok((first, second))
})?;
let (first_tx, first_rx) = mpsc::channel(16);
let (second_tx, second_rx) = mpsc::channel(16);
accessor.with(|access| first.pipe(access, PipeConsumer::new(first_tx)))?;
accessor.with(|access| second.pipe(access, PipeConsumer::new(second_tx)))?;
Ok(futures::future::join(first_rx.collect(), second_rx.collect()).await)
})
.await?
}
async fn run_overlapping_export_wrapped_streams(
component_path: &Utf8Path,
) -> Result<(Vec<u8>, Vec<u8>)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let (a_reached_tx, a_reached_rx) = oneshot::channel();
let (a_release_tx, a_release_rx) = oneshot::channel();
let (b_reached_tx, b_reached_rx) = oneshot::channel();
let (b_release_tx, b_release_rx) = oneshot::channel();
store.data_mut().export_gates = vec![
Some(ExportGate {
reached: a_reached_tx,
release: a_release_rx,
}),
Some(ExportGate {
reached: b_reached_tx,
release: b_release_rx,
}),
];
store
.run_concurrent(async move |accessor| {
let (a_done_tx, a_done_rx) = oneshot::channel();
let call_a = async {
let result = bindings
.call_run_wrapped_stream_after_gate(accessor, 0)
.await;
let _ = a_done_tx.send(());
result
};
let call_b = async {
a_reached_rx.await?;
bindings
.call_run_wrapped_stream_after_gate(accessor, 1)
.await
};
let orchestrate = async {
b_reached_rx.await?;
a_release_tx
.send(())
.unwrap_or_else(|_| panic!("export A release receiver was dropped"));
a_done_rx.await?;
b_release_tx
.send(())
.unwrap_or_else(|_| panic!("export B release receiver was dropped"));
Result::<()>::Ok(())
};
let (first, second, ordered) = futures::join!(call_a, call_b, orchestrate);
ordered?;
let (first, second) = (first?, second?);
let (first, second) = accessor.with(|mut access| {
let first = access.get().table.delete(first)?.0;
let second = access.get().table.delete(second)?.0;
Result::<_>::Ok((first, second))
})?;
let (first_tx, first_rx) = mpsc::channel(16);
let (second_tx, second_rx) = mpsc::channel(16);
accessor.with(|access| first.pipe(access, PipeConsumer::new(first_tx)))?;
accessor.with(|access| second.pipe(access, PipeConsumer::new(second_tx)))?;
Ok(futures::future::join(first_rx.collect(), second_rx.collect()).await)
})
.await?
}
async fn run_writer_completion_drains_overlapping_work(
component_path: &Utf8Path,
) -> Result<(Vec<u8>, Vec<u8>)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let (cleanup_tx, cleanup_rx) = oneshot::channel();
store.data_mut().cleanup_notification = Some(cleanup_tx);
let (timer_reached_tx, timer_reached_rx) = oneshot::channel();
let (timer_release_tx, timer_release_rx) = oneshot::channel();
store.data_mut().export_gates = vec![Some(ExportGate {
reached: timer_reached_tx,
release: timer_release_rx,
})];
store
.run_concurrent(async move |accessor| {
let (first, second) = bindings.call_run_wrapped_sibling_streams(accessor).await?;
bindings.call_schedule_cleanup(accessor, 0, 1).await?;
timer_reached_rx.await?;
let (first, second) = accessor.with(|mut access| {
let first = access.get().table.delete(first)?.0;
let second = access.get().table.delete(second)?.0;
Result::<_>::Ok((first, second))
})?;
let (first_tx, first_rx) = mpsc::channel(16);
let (second_tx, second_rx) = mpsc::channel(16);
accessor.with(|access| first.pipe(access, PipeConsumer::new(first_tx)))?;
accessor.with(|access| second.pipe(access, PipeConsumer::new(second_tx)))?;
let streams = futures::future::join(first_rx.collect(), second_rx.collect()).await;
timer_release_tx
.send(())
.unwrap_or_else(|_| panic!("timer gate release receiver was dropped"));
cleanup_rx.await?;
Ok(streams)
})
.await?
}
async fn run_streaming_constructor(
component_path: &Utf8Path,
deferred: bool,
) -> Result<(bool, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let trapped = if deferred {
bindings
.test_async_values_constructor_api()
.deferred_streaming_constructor()
.call_constructor(&mut store)
.await
.is_err()
} else {
bindings
.test_async_values_constructor_api()
.streaming_constructor()
.call_constructor(&mut store)
.await
.is_err()
};
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((trapped, stderr))
}
async fn run_nested_export_stream_cleanup(
component_path: &Utf8Path,
cleanup_failure: bool,
) -> Result<(Result<u8>, usize, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let result = match store
.run_concurrent(async move |accessor| -> Result<u8> {
let nested = bindings
.call_run_nested_observed_stream(accessor, cleanup_failure)
.await?
.expect("run-nested-observed-stream should return the success arm");
assert_eq!(nested.label, "nested-observed");
assert!(nested.stderr.is_none());
let (future_tx, future_rx) = oneshot::channel();
accessor.with(|access| {
nested
.future_value
.pipe(access, OneshotConsumer::new(future_tx))
})?;
assert_eq!(future_rx.await?, 1);
let stdout = nested.stdout.expect("nested stdout should be present");
let (stdout_tx, stdout_rx) = oneshot::channel();
accessor.with(|access| stdout.pipe(access, PrefixConsumer::new(stdout_tx)))?;
let first = stdout_rx.await?;
tokio::time::sleep(Duration::from_millis(20)).await;
Ok(first)
})
.await
{
Ok(result) => result,
Err(error) => Err(error),
};
let cleanup_calls = store.data().nested_cleanup_calls;
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((result, cleanup_calls, stderr))
}
async fn run_import_world(component_path: &Utf8Path) -> Result<String> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = store
.run_concurrent(async move |accessor| bindings.call_run(accessor).await)
.await??;
Ok(result)
}
async fn run_nested_imports(component_path: &Utf8Path) -> Result<String> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = store
.run_concurrent(async move |accessor| bindings.call_run_nested_imports(accessor).await)
.await
.and_then(|result| result);
if result.is_err() {
eprintln!(
"guest stderr:\n{}",
String::from_utf8_lossy(&store.data().stderr.contents())
);
}
result
}
async fn run_import_world_promise_stream_items(component_path: &Utf8Path) -> Result<String> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = store
.run_concurrent(async move |accessor| {
bindings.call_run_promise_stream_items(accessor).await
})
.await??;
Ok(result)
}
async fn run_import_world_stored_future(component_path: &Utf8Path) -> Result<String> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = store
.run_concurrent(async move |accessor| bindings.call_run_stored_future(accessor).await)
.await??;
Ok(result)
}
async fn run_import_world_stream_lifecycle(component_path: &Utf8Path) -> Result<(String, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let readable_drop = {
let mut store = new_store(&engine);
let bindings =
AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
bindings.call_run_stream_readable_drop(accessor).await
})
.await??
};
let backpressure = {
let mut store = new_store(&engine);
let bindings =
AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| {
bindings.call_run_stream_backpressure(accessor).await
})
.await??
};
Ok((readable_drop, backpressure))
}
async fn run_import_world_stream_failure(
component_path: &Utf8Path,
cleanup_failure: bool,
) -> Result<(Result<String>, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = match store
.run_concurrent(async move |accessor| {
if cleanup_failure {
bindings.call_run_stream_cleanup_failure(accessor).await
} else {
bindings.call_run_stream_failure(accessor).await
}
})
.await
{
Ok(result) => result,
Err(error) => Err(error),
};
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((result, stderr))
}
async fn run_import_world_stream_conversion_failure(
component_path: &Utf8Path,
) -> Result<(Result<String>, usize, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = match store
.run_concurrent(async move |accessor| {
bindings.call_run_stream_conversion_failure(accessor).await
})
.await
{
Ok(result) => result,
Err(error) => Err(error),
};
let cleanup_calls = store.data().nested_cleanup_calls;
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((result, cleanup_calls, stderr))
}
async fn run_import_world_sync_stream_promise_rejection(
component_path: &Utf8Path,
) -> Result<(Result<String>, usize, String)> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let result = match store
.run_concurrent(async move |accessor| {
bindings
.call_run_sync_stream_promise_rejection(accessor)
.await
})
.await
{
Ok(result) => result,
Err(error) => Err(error),
};
let cleanup_calls = store.data().nested_cleanup_calls;
let stderr = String::from_utf8_lossy(&store.data().stderr.contents()).into_owned();
Ok((result, cleanup_calls, stderr))
}
async fn run_import_world_checkpoint(component_path: &Utf8Path) -> Result<u32> {
let engine = engine()?;
let component = Component::from_file(&engine, component_path)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(&mut linker, |h| h)?;
let mut store = new_store(&engine);
let bindings = AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<u32> {
let reader: FutureReader<u32> =
bindings.call_run_import_checkpoint_future(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, OneshotConsumer::new(tx)))?;
assert_eq!(rx.await?, 9, "checkpoint import future should resolve to 9");
bindings.call_read_import_checkpoint_count(accessor).await
})
.await?
}
fn generate_and_build(
temp: &Utf8TempDir,
example: &str,
wasm_name: &str,
) -> anyhow::Result<Utf8PathBuf> {
generate_and_build_with_features(temp, example, wasm_name, "p3,crypto,zlib,encoding")
}
fn generate_and_build_with_features(
temp: &Utf8TempDir,
example: &str,
wasm_name: &str,
features: &str,
) -> anyhow::Result<Utf8PathBuf> {
let manifest_dir = Utf8PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let example_dir = manifest_dir.join("examples").join("p3").join(example);
let wit_dir = example_dir.join("wit");
let js = example_dir.join("src").join(format!("{example}.js"));
let out = temp.path().join("out");
generate_wrapper_crate_with_target(
&wit_dir,
&[JsModuleSpec {
name: "module".to_string(),
mode: EmbeddingMode::EmbedFile(js),
}],
&out,
None,
GenerationTarget::WasiP3,
)?;
let cache = p3_build::BuildCache::new()?;
let mut build_cmd = Command::new("cargo");
cache.configure(&mut build_cmd);
let build = build_cmd
.arg("build")
.arg("--manifest-path")
.arg(out.join("Cargo.toml"))
.arg("--no-default-features")
.arg("--features")
.arg(features)
.arg("--target")
.arg("wasm32-wasip2")
.output()?;
assert!(
build.status.success(),
"generated P3 crate for `{example}` should build; stdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&build.stdout),
String::from_utf8_lossy(&build.stderr)
);
let mut metadata_cmd = Command::new("cargo");
cache.configure(&mut metadata_cmd);
let metadata = metadata_cmd
.arg("metadata")
.arg("--no-deps")
.arg("--format-version")
.arg("1")
.arg("--manifest-path")
.arg(out.join("Cargo.toml"))
.output()?;
assert!(metadata.status.success(), "cargo metadata should succeed");
let metadata: serde_json::Value = serde_json::from_slice(&metadata.stdout)?;
let target_dir = metadata["target_directory"]
.as_str()
.ok_or_else(|| anyhow_msg("missing target_directory in cargo metadata"))?;
let wasm = Utf8PathBuf::from(target_dir)
.join("wasm32-wasip2")
.join("debug")
.join(format!("{wasm_name}.wasm"));
Ok(
Utf8PathBuf::from_path_buf(cache.finish(wasm.as_std_path(), out.as_std_path())?)
.expect("generated paths are UTF-8"),
)
}
fn block_on_with_timeout<F, T, E>(secs: u64, fut: F) -> T
where
F: Future<Output = std::result::Result<T, E>>,
E: std::fmt::Debug,
{
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("failed to build tokio runtime");
rt.block_on(async {
match tokio::time::timeout(Duration::from_secs(secs), fut).await {
Ok(result) => result.expect("guest call failed"),
Err(_) => panic!(
"guest call did not complete within {secs}s (future/stream deadlock regression)"
),
}
})
}
fn anyhow_msg(msg: &'static str) -> anyhow::Error {
anyhow::anyhow!(msg)
}
#[test]
fn p3_optional_future_missing_values_resolve_none() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let linker = base_linker(&engine)?;
for nested in [false, true] {
for (mode, expected) in [Some(23), None].into_iter().enumerate() {
let mut store = new_store(&engine);
let bindings =
AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let actual = store
.run_concurrent(async move |accessor| -> Result<Option<u32>> {
let reader = if nested {
bindings
.call_run_nested_optional_future(accessor, mode as u8)
.await?
.value
} else {
bindings
.call_run_optional_future(accessor, mode as u8)
.await?
};
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, OneshotConsumer::new(tx)))?;
Ok(rx.await?)
})
.await??;
assert_eq!(actual, expected, "export nested={nested}, mode={mode}");
}
}
Ok::<(), anyhow::Error>(())
});
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(
&mut linker,
|host| host,
)?;
let mut store = new_store(&engine);
let bindings =
AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
let bindings = &bindings;
let actual = store
.run_concurrent(async move |accessor| {
bindings.call_run_optional_futures(accessor).await
})
.await??;
assert_eq!(actual, "[23,null,null,23,null,null]");
store
.run_concurrent(async move |accessor| {
bindings.call_run_dropped_optional_future(accessor).await
})
.await??;
let stored = store
.data_mut()
.stored_optional_future
.take()
.expect("stored future");
let actual = store
.run_concurrent(async move |_accessor| stored.await)
.await??;
assert_eq!(
actual, None,
"unresolved writer defaults after its export returns"
);
Ok::<(), anyhow::Error>(())
});
}
#[test]
fn p3_stream_roots_survive_automatic_gc() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build_with_features(
&temp,
"async-values",
"async_values",
"p3,crypto,zlib,encoding,test-observability",
)
.expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<()> {
let reader = bindings.call_run_gc_stream(accessor).await?;
let (tx, rx) = mpsc::channel::<u8>(16);
accessor.with(|access| reader.pipe(access, PipeConsumer::new(tx)))?;
assert_eq!(rx.collect::<Vec<_>>().await, vec![42, 43, 44]);
assert_eq!(
accessor.with(|mut access| access.get().nested_cleanup_calls),
1
);
let (cleanup_tx, cleanup_rx) = oneshot::channel();
accessor.with(|mut access| access.get().cleanup_notification = Some(cleanup_tx));
let reader = bindings.call_run_gc_stream(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, PrefixConsumer::new(tx)))?;
assert_eq!(rx.await?, 42);
cleanup_rx.await?;
let future = bindings.call_run_future(accessor).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| future.pipe(access, OneshotConsumer::new(tx)))?;
assert_eq!(rx.await?, 42);
assert_eq!(
accessor.with(|mut access| access.get().nested_cleanup_calls),
2
);
Ok(())
})
.await??;
Ok::<(), anyhow::Error>(())
});
}
#[test]
fn p3_async_import_owns_bytes_before_transfer() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(
&mut linker,
|host| host,
)?;
let mut store = new_store(&engine);
let bindings =
AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<()> {
assert!(bindings.call_run_buffer_transfer(accessor).await?);
assert_eq!(accessor.with(|mut access| access.get().byte_echo_calls), 1);
Ok(())
})
.await??;
Ok::<(), anyhow::Error>(())
});
}
#[test]
fn p3_imported_resources_use_engine_dispose_symbol() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let linker = base_linker(&engine)?;
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async move |accessor| -> Result<()> {
assert!(bindings.call_dispose_wrapped_resources(accessor).await?);
assert_eq!(accessor.with(|mut access| access.get().wrapped_drops), 2);
Ok(())
})
.await??;
Ok::<(), anyhow::Error>(())
});
}
#[test]
fn p3_unused_imported_future_does_not_retain_guest_tasks() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let mut linker = base_linker(&engine)?;
test::async_values_import::host::add_to_linker::<Host, HasSelf<Host>>(
&mut linker,
|host| host,
)?;
let mut store = new_store(&engine);
let bindings =
AsyncValuesImport::instantiate_async(&mut store, &component, &linker).await?;
store
.run_concurrent(async |accessor| -> Result<()> {
assert_eq!(bindings.call_run_unused_future(accessor).await?, 42);
tokio::time::timeout(
Duration::from_secs(30),
futures::future::poll_fn(|cx| accessor.poll_no_interesting_tasks(cx)),
)
.await
.expect("an unused imported future must not retain the guest task");
Ok(())
})
.await??;
store
.data_mut()
.pending_optional_future
.take()
.expect("pending future writer")
.send(Some(29))
.expect("unused future reader must remain alive");
assert_eq!(
store
.run_concurrent(async move |accessor| {
bindings.call_read_retained_future(accessor).await
})
.await??,
Some(29)
);
Ok::<(), anyhow::Error>(())
});
}
#[test]
fn p3_future_readers_start_on_observation_and_support_forwarding() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
block_on_with_timeout(120, async {
let engine = engine()?;
let component = Component::from_file(&engine, wasm)?;
let linker = base_linker(&engine)?;
for (mode, expected) in [(0, 42), (1, 17), (2, 257)] {
let mut store = new_store(&engine);
let bindings = AsyncValues::instantiate_async(&mut store, &component, &linker).await?;
let reads = Arc::new(AtomicUsize::new(0));
let (tx, rx) = oneshot::channel();
let reader = FutureReader::new(
&mut store,
ObservedFutureProducer {
inner: OneshotProducer::new(rx),
reads: reads.clone(),
},
)?;
let pending = if mode == 0 {
Some(tx)
} else {
tx.send(17).expect("future receiver");
None
};
store
.run_concurrent(async move |accessor| -> Result<()> {
let reader = bindings.call_inspect_future(accessor, mode, reader).await?;
let (tx, rx) = oneshot::channel();
accessor.with(|access| reader.pipe(access, OneshotConsumer::new(tx)))?;
assert_eq!(rx.await?, expected);
tokio::time::timeout(
Duration::from_secs(30),
futures::future::poll_fn(|cx| accessor.poll_no_interesting_tasks(cx)),
)
.await
.expect("an unused future must not retain the guest task");
Ok(())
})
.await??;
if mode == 0 {
assert_eq!(reads.load(Ordering::SeqCst), 0, "unused future was read");
} else {
assert!(
reads.load(Ordering::SeqCst) > 0,
"observed future was not read"
);
}
drop(pending);
}
Ok::<(), anyhow::Error>(())
});
}
struct ObservedFutureProducer {
inner: OneshotProducer<u32>,
reads: Arc<AtomicUsize>,
}
impl FutureProducer<Host> for ObservedFutureProducer {
type Item = u32;
fn poll_produce(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
store: StoreContextMut<Host>,
finish: bool,
) -> Poll<Result<Option<u32>>> {
let this = self.get_mut();
if !finish {
this.reads.fetch_add(1, Ordering::SeqCst);
}
Pin::new(&mut this.inner).poll_produce(cx, store, finish)
}
}
#[test]
fn p3_async_values_export_boundaries_roundtrip() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (
run_future,
run_stream,
nested_label,
nested_future,
nested_stdout,
nested_stderr,
nested_error,
take_future,
take_stream,
checkpoint_count,
) = block_on_with_timeout(120, run_export_world(&wasm));
assert_eq!(run_future, 42, "run-future should return 42");
assert_eq!(
run_stream,
vec![1, 2, 3, 4, 5],
"run-stream should yield 1..=5"
);
assert_eq!(nested_label, "nested-ok");
assert_eq!(nested_future, 99);
assert_eq!(nested_stdout, vec![6, 7, 8]);
assert_eq!(nested_stderr, vec![9, 10]);
assert_eq!(nested_error, "nested-error");
assert_eq!(take_future, 42, "take-future(41) should return 42");
assert_eq!(take_stream, 60, "take-stream([10,20,30]) should return 60");
assert_eq!(
checkpoint_count, 1,
"the raw future export should checkpoint before the next host call"
);
}
#[test]
fn p3_async_values_import_boundaries_roundtrip() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let result = block_on_with_timeout(120, run_import_world(&wasm));
assert_eq!(result, "41|1,2,3,4|7|10");
}
#[test]
fn p3_async_values_nested_imports_settle_in_originating_runtime() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let result = block_on_with_timeout(120, run_nested_imports(&wasm));
let report: serde_json::Value = serde_json::from_str(&result).expect("JSON report");
assert_eq!(
report,
serde_json::json!({
"results": [
{ "future": 17, "stream": 12, "checked": 24, "error": "unlucky" },
{ "future": 18, "stream": 13, "checked": 25, "error": "unlucky" },
{ "future": 19, "stream": 14, "checked": 26, "error": "unlucky" },
],
"timedOut": true,
"cancelled": true,
"control": 53,
})
);
}
#[test]
fn p3_async_values_import_stream_accepts_promise_items_from_sync_iterable() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let result = block_on_with_timeout(120, run_import_world_promise_stream_items(&wasm));
assert_eq!(result, "10");
}
#[test]
fn p3_async_values_import_future_param_can_be_consumed_after_import_returns() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let result = block_on_with_timeout(120, run_import_world_stored_future(&wasm));
assert_eq!(result, "99");
}
#[test]
fn p3_component_stream_js_iterator_lifecycle() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (
completion,
early_break,
returned,
thrown,
pending_close,
break_drops,
return_drops,
throw_drops,
pending_polls,
pending_drops,
) = block_on_with_timeout(120, run_export_stream_lifecycle(&wasm));
assert_eq!(completion, "1,2,3|true|true");
assert_eq!(early_break, 1);
assert_eq!(returned, "1|true|true");
assert_eq!(thrown, "1|consumer-stop");
assert_eq!(
pending_close,
"true|closed|true|true|true|closed-again|true|true"
);
assert_eq!(break_drops, 1, "early break must drop the readable end");
assert_eq!(return_drops, 1, "return() must drop the readable end");
assert_eq!(throw_drops, 1, "throw() must drop the readable end");
assert!(
pending_polls > 0,
"the close regression must exercise a genuinely pending component read"
);
assert_eq!(
pending_drops, 1,
"closing any iterator instance must abort the active pull and drop the shared readable end once"
);
}
#[test]
fn p3_exported_js_stream_peer_drop_runs_iterator_cleanup() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (first, state) = block_on_with_timeout(120, run_export_stream_producer_cleanup(&wasm));
assert_eq!(first, 1);
assert_eq!(
state, "2|1|1",
"peer drop after one accepted item must stop further pulls and await return() exactly once"
);
}
#[test]
fn p3_exported_js_stream_conversion_failure_closes_iterator() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (result, cleanup_calls, stderr) =
block_on_with_timeout(120, run_export_stream_conversion_failure(&wasm));
result.expect_err("invalid exported stream payload must fail the active stream session");
assert_eq!(
cleanup_calls, 1,
"payload conversion failure must close the JavaScript iterator exactly once"
);
assert!(
stderr.contains("Failed to convert a JavaScript value"),
"conversion failure should preserve its diagnostic, got: {stderr}"
);
assert!(
!stderr.contains("secondary-export-cleanup-failed"),
"secondary cleanup failure must not replace the primary conversion failure: {stderr}"
);
}
#[test]
fn p3_exported_sibling_streams_can_exceed_buffer_capacity() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (first, second) = block_on_with_timeout(120, run_export_sibling_streams(&wasm));
assert_eq!(first, vec![1, 2]);
assert_eq!(second, (0..64).collect::<Vec<_>>());
}
#[test]
fn p3_exported_wrapped_sibling_streams_can_exceed_buffer_capacity() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (first, second) = block_on_with_timeout(120, run_export_wrapped_sibling_streams(&wasm));
assert_eq!(first, vec![1, 2]);
assert_eq!(second, (0..64).collect::<Vec<_>>());
}
#[test]
fn p3_overlapping_exports_keep_wrapped_stream_writers_alive() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (first, second) = block_on_with_timeout(120, run_overlapping_export_wrapped_streams(&wasm));
assert_eq!(first, (0..64).collect::<Vec<_>>());
assert_eq!(second, (0..64).collect::<Vec<_>>());
}
#[test]
fn p3_writer_completion_drains_work_scheduled_by_overlapping_export() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (first, second) =
block_on_with_timeout(120, run_writer_completion_drains_overlapping_work(&wasm));
assert_eq!(first, vec![1, 2]);
assert_eq!(second, (0..64).collect::<Vec<_>>());
}
#[test]
fn p3_exported_resource_constructor_rejects_stream_writers() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (trapped, stderr) = block_on_with_timeout(120, run_streaming_constructor(&wasm, false));
assert!(
trapped,
"the immediate synchronous writer must trap promptly"
);
assert!(
stderr.contains("Constructors cannot be asynchronous"),
"the trap should explain how to move the work to an asynchronous operation, got: {stderr}"
);
let (trapped, stderr) = block_on_with_timeout(120, run_streaming_constructor(&wasm, true));
assert!(
trapped,
"a synchronous constructor whose checkpoint creates a writer must trap promptly"
);
assert!(
stderr.contains("Constructors cannot be asynchronous"),
"the deferred trap should explain how to move the work to an asynchronous operation, got: {stderr}"
);
}
#[test]
fn p3_nested_exported_stream_keeps_scheduler_until_iterator_cleanup_settles() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values", "async_values").expect("generate + build");
let (result, cleanup_calls, _stderr) =
block_on_with_timeout(120, run_nested_export_stream_cleanup(&wasm, false));
assert_eq!(result.expect("nested stream cleanup should succeed"), 1);
assert_eq!(
cleanup_calls, 1,
"nested stream cleanup must finish exactly once before its scheduler driver is released"
);
let (result, cleanup_calls, stderr) =
block_on_with_timeout(120, run_nested_export_stream_cleanup(&wasm, true));
result.expect_err("rejecting nested iterator return() must fail the active stream session");
assert_eq!(
cleanup_calls, 1,
"rejecting cleanup must still run exactly once"
);
assert!(
stderr.contains("nested-cleanup-failed"),
"nested cleanup failure should preserve its diagnostic, got: {stderr}"
);
}
#[test]
fn p3_js_stream_peer_drop_runs_iterator_cleanup_with_backpressure() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let (readable_drop, backpressure) =
block_on_with_timeout(120, run_import_world_stream_lifecycle(&wasm));
assert_eq!(
(readable_drop.as_str(), backpressure.as_str()),
("1|2|1|1", "1|1|1"),
"an accepted write may permit one more pull, a pending write must gate it, and peer drop must await return() exactly once"
);
}
#[test]
fn p3_js_stream_producer_failure_is_not_clean_eof() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let (result, stderr) =
block_on_with_timeout(120, run_import_world_stream_failure(&wasm, false));
result.expect_err("throwing producer must fail the active stream consumer");
assert!(
stderr.contains("producer-failed"),
"producer failure should preserve its diagnostic, got: {stderr}"
);
}
#[test]
fn p3_js_stream_cleanup_rejection_fails_active_consumer() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let (result, stderr) = block_on_with_timeout(120, run_import_world_stream_failure(&wasm, true));
result.expect_err("rejecting iterator return() must fail the active stream consumer");
assert!(
stderr.contains("cleanup-failed"),
"cleanup failure should preserve its diagnostic, got: {stderr}"
);
}
#[test]
fn p3_js_stream_conversion_failure_closes_iterator_and_preserves_primary_error() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let (result, cleanup_calls, stderr) =
block_on_with_timeout(120, run_import_world_stream_conversion_failure(&wasm));
result.expect_err("invalid imported stream payload must fail the active stream consumer");
assert_eq!(
cleanup_calls, 1,
"payload conversion failure must close the JavaScript iterator exactly once"
);
assert!(
stderr.contains("Failed to convert a JavaScript value"),
"primary conversion failure should remain visible, got: {stderr}"
);
assert!(
!stderr.contains("secondary-cleanup-failed"),
"secondary cleanup failure must not replace the primary conversion failure: {stderr}"
);
}
#[test]
fn p3_sync_stream_rejected_item_does_not_close_iterator() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let (result, cleanup_calls, stderr) =
block_on_with_timeout(120, run_import_world_sync_stream_promise_rejection(&wasm));
result.expect_err("rejected synchronous stream item must fail the active consumer");
assert_eq!(
cleanup_calls, 0,
"AsyncFromSyncIterator next() rejection must not call iterator.return()"
);
assert!(
stderr.contains("sync-item-failed"),
"the rejected item diagnostic should remain visible, got: {stderr}"
);
}
#[test]
fn p3_async_values_import_settlement_runs_rejection_checkpoint() {
let temp = Utf8TempDir::new().expect("temp dir");
let wasm = generate_and_build(&temp, "async-values-import", "async_values_import")
.expect("generate + build");
let checkpoint_count = block_on_with_timeout(120, run_import_world_checkpoint(&wasm));
assert_eq!(
checkpoint_count, 1,
"async import settlement should checkpoint before the next host call"
);
}
struct PipeProducer<S>(S);
impl<S> PipeProducer<S> {
fn new(rx: S) -> Self {
Self(rx)
}
}
#[derive(Default)]
struct ProducerObservation {
drops: AtomicUsize,
polls: AtomicUsize,
}
struct ObservedProducer {
next: u8,
observation: Arc<ProducerObservation>,
}
impl ObservedProducer {
fn new(observation: Arc<ProducerObservation>) -> Self {
Self {
next: 1,
observation,
}
}
}
impl Drop for ObservedProducer {
fn drop(&mut self) {
self.observation.drops.fetch_add(1, Ordering::SeqCst);
}
}
impl<D> StreamProducer<D> for ObservedProducer {
type Item = u8;
type Buffer = Option<u8>;
fn poll_produce<'a>(
mut self: Pin<&mut Self>,
_: &mut Context<'_>,
_: StoreContextMut<D>,
mut destination: Destination<'a, Self::Item, Self::Buffer>,
finish: bool,
) -> Poll<Result<StreamResult>> {
if finish {
return Poll::Ready(Ok(StreamResult::Cancelled));
}
let value = self.next;
self.next = self.next.wrapping_add(1);
destination.set_buffer(Some(value));
Poll::Ready(Ok(StreamResult::Completed))
}
}
struct PendingObservedProducer {
observation: Arc<ProducerObservation>,
}
impl PendingObservedProducer {
fn new(observation: Arc<ProducerObservation>) -> Self {
Self { observation }
}
}
impl Drop for PendingObservedProducer {
fn drop(&mut self) {
self.observation.drops.fetch_add(1, Ordering::SeqCst);
}
}
impl<D> StreamProducer<D> for PendingObservedProducer {
type Item = u8;
type Buffer = Option<u8>;
fn poll_produce<'a>(
self: Pin<&mut Self>,
_: &mut Context<'_>,
_: StoreContextMut<D>,
_: Destination<'a, Self::Item, Self::Buffer>,
finish: bool,
) -> Poll<Result<StreamResult>> {
self.observation.polls.fetch_add(1, Ordering::SeqCst);
if finish {
Poll::Ready(Ok(StreamResult::Cancelled))
} else {
Poll::Pending
}
}
}
impl<D, T: Send + Sync + Lower + 'static, S: Stream<Item = T> + Send + 'static> StreamProducer<D>
for PipeProducer<S>
{
type Item = T;
type Buffer = Option<T>;
fn poll_produce<'a>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
_: StoreContextMut<D>,
mut destination: Destination<'a, Self::Item, Self::Buffer>,
finish: bool,
) -> Poll<Result<StreamResult>> {
let stream = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
match stream.poll_next(cx) {
Poll::Pending => {
if finish {
Poll::Ready(Ok(StreamResult::Cancelled))
} else {
Poll::Pending
}
}
Poll::Ready(Some(item)) => {
destination.set_buffer(Some(item));
Poll::Ready(Ok(StreamResult::Completed))
}
Poll::Ready(None) => Poll::Ready(Ok(StreamResult::Dropped)),
}
}
}
struct PipeConsumer<T, S>(S, PhantomData<fn() -> T>);
impl<T, S> PipeConsumer<T, S> {
fn new(tx: S) -> Self {
Self(tx, PhantomData)
}
}
struct PrefixConsumer<T>(Option<oneshot::Sender<T>>);
impl<T> PrefixConsumer<T> {
fn new(tx: oneshot::Sender<T>) -> Self {
Self(Some(tx))
}
}
impl<D, T: Lift + Send + 'static> StreamConsumer<D> for PrefixConsumer<T> {
type Item = T;
fn poll_consume(
self: Pin<&mut Self>,
_: &mut Context<'_>,
store: StoreContextMut<D>,
mut source: Source<Self::Item>,
_: bool,
) -> Poll<Result<StreamResult>> {
let value = &mut None;
source.read(store, value)?;
let value = value.take().expect("prefix stream write had no item");
let _ = self.get_mut().0.take().unwrap().send(value);
Poll::Ready(Ok(StreamResult::Dropped))
}
}
#[derive(Default)]
struct BackpressureDropConsumer {
delayed_once: bool,
}
impl<D> StreamConsumer<D> for BackpressureDropConsumer {
type Item = u8;
fn poll_consume(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
_: StoreContextMut<D>,
_: Source<Self::Item>,
finish: bool,
) -> Poll<Result<StreamResult>> {
if finish {
return Poll::Ready(Ok(StreamResult::Cancelled));
}
if self.delayed_once {
Poll::Ready(Ok(StreamResult::Dropped))
} else {
self.delayed_once = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
impl<D, T: Lift + 'static, S: Sink<T, Error: std::error::Error + Send + Sync> + Send + 'static>
StreamConsumer<D> for PipeConsumer<T, S>
{
type Item = T;
fn poll_consume(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
store: StoreContextMut<D>,
mut source: Source<Self::Item>,
finish: bool,
) -> Poll<Result<StreamResult>> {
let mut sink = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
let on_pending = || {
if finish {
Poll::Ready(Ok(StreamResult::Cancelled))
} else {
Poll::Pending
}
};
match sink.as_mut().poll_flush(cx) {
Poll::Pending => on_pending(),
Poll::Ready(result) => {
result?;
match sink.as_mut().poll_ready(cx) {
Poll::Pending => on_pending(),
Poll::Ready(result) => {
result?;
let item = &mut None;
source.read(store, item)?;
sink.start_send(item.take().unwrap())?;
Poll::Ready(Ok(StreamResult::Completed))
}
}
}
}
}
}
struct OneshotProducer<T>(oneshot::Receiver<T>);
impl<T> OneshotProducer<T> {
fn new(rx: oneshot::Receiver<T>) -> Self {
Self(rx)
}
}
impl<D, T: Send + 'static> FutureProducer<D> for OneshotProducer<T> {
type Item = T;
fn poll_produce(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
_: StoreContextMut<D>,
finish: bool,
) -> Poll<Result<Option<T>>> {
match Pin::new(&mut self.get_mut().0).poll(cx) {
Poll::Pending if finish => Poll::Ready(Ok(None)),
Poll::Pending => Poll::Pending,
Poll::Ready(result) => Poll::Ready(Ok(Some(result?))),
}
}
}
struct OneshotConsumer<T>(Option<oneshot::Sender<T>>);
impl<T> OneshotConsumer<T> {
fn new(tx: oneshot::Sender<T>) -> Self {
Self(Some(tx))
}
}
impl<D, T: Lift + Send + 'static> FutureConsumer<D> for OneshotConsumer<T> {
type Item = T;
fn poll_consume(
self: Pin<&mut Self>,
_: &mut Context<'_>,
store: StoreContextMut<D>,
mut source: Source<'_, T>,
_: bool,
) -> Poll<Result<()>> {
let value = &mut None;
source.read(store, value)?;
_ = self.get_mut().0.take().unwrap().send(value.take().unwrap());
Poll::Ready(Ok(()))
}
}