Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-keyvalue/tests/main.rs
1692 views
1
use anyhow::{Result, anyhow};
2
use test_programs_artifacts::{KEYVALUE_MAIN_COMPONENT, foreach_keyvalue};
3
use wasmtime::{
4
Store,
5
component::{Component, Linker, ResourceTable},
6
};
7
use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView, p2::bindings::Command};
8
use wasmtime_wasi_keyvalue::{WasiKeyValue, WasiKeyValueCtx, WasiKeyValueCtxBuilder};
9
10
struct Ctx {
11
table: ResourceTable,
12
wasi_ctx: WasiCtx,
13
wasi_keyvalue_ctx: WasiKeyValueCtx,
14
}
15
16
impl WasiView for Ctx {
17
fn ctx(&mut self) -> WasiCtxView<'_> {
18
WasiCtxView {
19
ctx: &mut self.wasi_ctx,
20
table: &mut self.table,
21
}
22
}
23
}
24
25
async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> {
26
let engine = test_programs_artifacts::engine(|config| {
27
config.async_support(true);
28
});
29
let mut store = Store::new(&engine, ctx);
30
let component = Component::from_file(&engine, path)?;
31
32
let mut linker = Linker::new(&engine);
33
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
34
wasmtime_wasi_keyvalue::add_to_linker(&mut linker, |h: &mut Ctx| {
35
WasiKeyValue::new(&h.wasi_keyvalue_ctx, &mut h.table)
36
})?;
37
38
let command = Command::instantiate_async(&mut store, &component, &linker).await?;
39
command
40
.wasi_cli_run()
41
.call_run(&mut store)
42
.await?
43
.map_err(|()| anyhow!("command returned with failing exit status"))
44
}
45
46
macro_rules! assert_test_exists {
47
($name:ident) => {
48
#[expect(unused_imports, reason = "just here to assert it exists")]
49
use self::$name as _;
50
};
51
}
52
53
foreach_keyvalue!(assert_test_exists);
54
55
#[tokio::test(flavor = "multi_thread")]
56
async fn keyvalue_main() -> Result<()> {
57
run_wasi(
58
KEYVALUE_MAIN_COMPONENT,
59
Ctx {
60
table: ResourceTable::new(),
61
wasi_ctx: WasiCtx::builder().inherit_stderr().build(),
62
wasi_keyvalue_ctx: WasiKeyValueCtxBuilder::new()
63
.in_memory_data([("atomics_key", "5")])
64
.build(),
65
},
66
)
67
.await
68
}
69
70