Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/misc/component-async-tests/tests/scenario/common.rs
1693 views
1
use std::sync::{Arc, Mutex, Once};
2
use std::time::Duration;
3
4
use anyhow::Result;
5
use tokio::fs;
6
use wasm_compose::composer::ComponentComposer;
7
use wasmtime::component::{Component, Linker, PromisesUnordered, ResourceTable};
8
use wasmtime::{Config, Engine, Store};
9
use wasmtime_wasi::WasiCtxBuilder;
10
11
use component_async_tests::Ctx;
12
13
pub fn annotate<T, F>(val: F) -> F
14
where
15
F: Fn(&mut T) -> &mut T,
16
{
17
val
18
}
19
20
pub fn init_logger() {
21
static ONCE: Once = Once::new();
22
ONCE.call_once(pretty_env_logger::init);
23
}
24
25
/// Compose two components
26
///
27
/// a is the "root" component, and b is composed into it
28
#[allow(unused)]
29
pub async fn compose(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
30
let dir = tempfile::tempdir()?;
31
32
let a_file = dir.path().join("a.wasm");
33
fs::write(&a_file, a).await?;
34
35
let b_file = dir.path().join("b.wasm");
36
fs::write(&b_file, b).await?;
37
38
ComponentComposer::new(
39
&a_file,
40
&wasm_compose::config::Config {
41
dir: dir.path().to_owned(),
42
definitions: vec![b_file.to_owned()],
43
..Default::default()
44
},
45
)
46
.compose()
47
}
48
49
#[allow(unused)]
50
pub async fn test_run(component: &[u8]) -> Result<()> {
51
let mut config = config();
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
}
57
58
let engine = Engine::new(&config)?;
59
60
let component = Component::new(&engine, component)?;
61
62
let mut linker = Linker::new(&engine);
63
64
wasmtime_wasi::add_to_linker_async(&mut linker)?;
65
component_async_tests::yield_host::bindings::local::local::continue_::add_to_linker_get_host(
66
&mut linker,
67
annotate(|ctx| ctx),
68
)?;
69
component_async_tests::yield_host::bindings::local::local::ready::add_to_linker_get_host(
70
&mut linker,
71
annotate(|ctx| ctx),
72
)?;
73
component_async_tests::resource_stream::bindings::local::local::resource_stream::add_to_linker_get_host(
74
&mut linker,
75
annotate(|ctx| ctx),
76
)?;
77
78
let mut store = Store::new(
79
&engine,
80
Ctx {
81
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
82
table: ResourceTable::default(),
83
continue_: false,
84
wakers: Arc::new(Mutex::new(None)),
85
},
86
);
87
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
}
96
97
let yield_host = component_async_tests::yield_host::bindings::YieldHost::instantiate_async(
98
&mut store, &component, &linker,
99
)
100
.await?;
101
102
// Start three concurrent calls and then join them all:
103
let mut promises = PromisesUnordered::new();
104
for _ in 0..3 {
105
promises.push(yield_host.local_local_run().call_run(&mut store).await?);
106
}
107
108
while let Some(()) = promises.next(&mut store).await? {
109
// continue
110
}
111
112
Ok(())
113
}
114
115