Skip to content
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
11 changes: 10 additions & 1 deletion crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,16 @@ impl Runner {
for (i, (_component, component_path)) in test_components.iter().enumerate() {
let main = match last_bytes.take() {
Some(bytes) => {
path = runner_wasm.with_extension(&format!("composition{i}.wasm"));
// Combinations sharing the same runner are composed in
// parallel, so name the intermediate file after the whole
// combination to keep it unique.
let combination = test_components
.iter()
.map(|(_, p)| p.file_stem().unwrap_or_default().to_string_lossy())
.collect::<Vec<_>>()
.join("-");
path =
runner_wasm.with_extension(&format!("composition{i}-{combination}.wasm"));
std::fs::write(&path, &bytes)
.with_context(|| format!("failed to write temporary file {path:?}"))?;
path.as_path()
Expand Down
8 changes: 8 additions & 0 deletions tests/runtime/async-cross-component-await/leaf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <leaf_cpp.h>

// Completes only after the event loop ran `rounds` times.
wit::Task<void> exports::test::cross::wait::Sleep(uint32_t rounds) {
for (uint32_t i = 0; i < rounds; ++i) {
co_await wit::yield_async();
}
}
31 changes: 31 additions & 0 deletions tests/runtime/async-cross-component-await/leaf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include!(env!("BINDINGS"));

use std::task::Poll;

struct Component;

export!(Component);

// Returns pending once after asking to be polled again, so the call only
// completes after the event loop ran.
async fn yield_once() {
let mut yielded = false;
std::future::poll_fn(|cx| {
if yielded {
Poll::Ready(())
} else {
yielded = true;
cx.waker().wake_by_ref();
Poll::Pending
}
})
.await
}

impl crate::exports::test::cross::wait::Guest for Component {
async fn sleep(rounds: u32) {
for _ in 0..rounds {
yield_once().await;
}
}
}
10 changes: 10 additions & 0 deletions tests/runtime/async-cross-component-await/middle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <middle_cpp.h>

wit::Task<wit::string> exports::test::cross::string_delay::Forward(wit::string s) {
if (s.get_view() == "now") {
co_return wit::string::from_view("directly returned");
}
co_await ::test::cross::wait::Sleep(3);
std::string result = "after sleep: " + std::string(s.get_view());
co_return wit::string::from_view(result);
}
15 changes: 15 additions & 0 deletions tests/runtime/async-cross-component-await/middle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include!(env!("BINDINGS"));

struct Component;

export!(Component);

impl crate::exports::test::cross::string_delay::Guest for Component {
async fn forward(s: String) -> String {
if s == "now" {
return "directly returned".to_string();
}
crate::test::cross::wait::sleep(3).await;
format!("after sleep: {s}")
}
}
17 changes: 17 additions & 0 deletions tests/runtime/async-cross-component-await/runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ wasmtime-flags = '-Wcomponent-model-async'

#include <assert.h>
#include <runner_cpp.h>

wit::Task<void> exports::runner::Run() {
{
auto r = co_await test::cross::string_delay::Forward(wit::string::from_view("now"));
assert(r.get_view() == "directly returned");
}
{
auto task = test::cross::string_delay::Forward(wit::string::from_view("x"));
assert(!task.done());
auto r = co_await task;
assert(r.get_view() == "after sleep: x");
}
}
27 changes: 27 additions & 0 deletions tests/runtime/async-cross-component-await/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//@ wasmtime-flags = '-Wcomponent-model-async'

include!(env!("BINDINGS"));

use crate::test::cross::string_delay::forward;
use std::future::Future;
use std::pin::pin;
use std::task::{Context, Poll, Waker};

struct Component;

export!(Component);

impl Guest for Component {
async fn run() {
// Completes without suspending.
assert_eq!(forward("now".into()).await, "directly returned");

// The middle component awaits an import of the leaf component which
// only completes after the event loop ran, so the first poll must
// be pending.
let mut f = pin!(forward("x".into()));
let mut cx = Context::from_waker(Waker::noop());
assert!(matches!(f.as_mut().poll(&mut cx), Poll::Pending));
assert_eq!(f.await, "after sleep: x");
}
}
28 changes: 28 additions & 0 deletions tests/runtime/async-cross-component-await/test.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//@ async = true
//@ dependencies = ['middle', 'leaf']

package test:cross;

interface wait {
// Completes only after the event loop ran a few times.
sleep: async func(rounds: u32);
}

interface string-delay {
forward: async func(s: string) -> string;
}

world runner {
import string-delay;

export run: async func();
}

world middle {
import wait;
export string-delay;
}

world leaf {
export wait;
}
Loading