Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/misc/component-async-tests/tests/scenario/round_trip_direct.rs
3120 views
1
use super::util::{config, make_component};
2
use component_async_tests::Ctx;
3
use component_async_tests::util::yield_times;
4
use futures::stream::{FuturesUnordered, TryStreamExt};
5
use wasmtime::component::{Linker, ResourceTable, Val};
6
use wasmtime::{Engine, Result, Store, format_err};
7
use wasmtime_wasi::WasiCtxBuilder;
8
9
#[tokio::test]
10
pub async fn async_round_trip_direct_stackless() -> Result<()> {
11
test_round_trip_direct_uncomposed(
12
test_programs_artifacts::ASYNC_ROUND_TRIP_DIRECT_STACKLESS_COMPONENT,
13
)
14
.await
15
}
16
17
async fn test_round_trip_direct_uncomposed(component: &str) -> Result<()> {
18
test_round_trip_direct(
19
&[component],
20
"hello, world!",
21
"hello, world! - entered guest - entered host - exited host - exited guest",
22
)
23
.await
24
}
25
26
async fn test_round_trip_direct(
27
components: &[&str],
28
input: &str,
29
expected_output: &str,
30
) -> Result<()> {
31
let engine = Engine::new(&config())?;
32
33
let make_store = || {
34
Store::new(
35
&engine,
36
Ctx {
37
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
38
table: ResourceTable::default(),
39
continue_: false,
40
},
41
)
42
};
43
44
let component = make_component(&engine, components).await?;
45
46
// First, test the `wasmtime-wit-bindgen` static API:
47
{
48
let mut linker = Linker::new(&engine);
49
50
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
51
component_async_tests::round_trip_direct::bindings::RoundTripDirect::add_to_linker_imports::<
52
_,
53
Ctx,
54
>(&mut linker, |ctx| ctx)?;
55
56
let mut store = make_store();
57
58
let round_trip =
59
component_async_tests::round_trip_direct::bindings::RoundTripDirect::instantiate_async(
60
&mut store, &component, &linker,
61
)
62
.await?;
63
64
store
65
.run_concurrent({
66
let input = input.to_owned();
67
let expected_output = expected_output.to_owned();
68
async move |accessor| {
69
// Start three concurrent calls and then join them all:
70
let mut futures = FuturesUnordered::new();
71
for _ in 0..3 {
72
futures.push(round_trip.call_foo(accessor, input.clone()));
73
}
74
75
while let Some(value) = futures.try_next().await? {
76
assert_eq!(expected_output, value);
77
}
78
79
wasmtime::error::Ok(())
80
}
81
})
82
.await??;
83
}
84
85
// Now do it again using the dynamic API (except for WASI, where we stick with the static API):
86
{
87
let mut linker = Linker::new(&engine);
88
89
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
90
linker
91
.root()
92
.func_new_concurrent("foo", |_, _, params, results| {
93
Box::pin(async move {
94
yield_times(10).await;
95
let Some(Val::String(s)) = params.into_iter().next() else {
96
unreachable!()
97
};
98
results[0] = Val::String(format!("{s} - entered host - exited host"));
99
Ok(())
100
})
101
})?;
102
103
let mut store = make_store();
104
105
let instance = linker.instantiate_async(&mut store, &component).await?;
106
let foo_function = instance
107
.get_export_index(&mut store, None, "foo")
108
.ok_or_else(|| format_err!("can't find `foo` in instance"))?;
109
let foo_function = instance
110
.get_func(&mut store, foo_function)
111
.ok_or_else(|| format_err!("can't find `foo` in instance"))?;
112
113
// Start three concurrent calls and then join them all:
114
store
115
.run_concurrent(async |store| -> wasmtime::Result<_> {
116
let mut futures = FuturesUnordered::new();
117
for _ in 0..3 {
118
futures.push(async move {
119
let mut results = vec![Val::Bool(false)];
120
foo_function
121
.call_concurrent(store, &[Val::String(input.to_owned())], &mut results)
122
.await?;
123
wasmtime::error::Ok(results)
124
});
125
}
126
127
while let Some(value) = futures.try_next().await? {
128
let Some(Val::String(value)) = value.into_iter().next() else {
129
unreachable!()
130
};
131
assert_eq!(expected_output, &value);
132
}
133
Ok(())
134
})
135
.await??;
136
}
137
138
Ok(())
139
}
140
141