Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/misc/component-async-tests/tests/scenario/borrowing.rs
1693 views
1
use std::env;
2
use std::sync::{Arc, Mutex};
3
use std::time::Duration;
4
5
use super::util::{config, make_component};
6
use anyhow::Result;
7
use futures::stream::{FuturesUnordered, TryStreamExt};
8
use wasmtime::component::{Linker, ResourceTable};
9
use wasmtime::{Engine, Store};
10
use wasmtime_wasi::WasiCtxBuilder;
11
12
#[tokio::test]
13
pub async fn async_borrowing_caller() -> Result<()> {
14
test_run_bool(
15
&[
16
test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,
17
test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT,
18
],
19
false,
20
)
21
.await
22
}
23
24
#[tokio::test]
25
async fn async_borrowing_caller_misbehave() -> Result<()> {
26
let error = format!(
27
"{:?}",
28
test_run_bool(
29
&[
30
test_programs_artifacts::ASYNC_BORROWING_CALLER_COMPONENT,
31
test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT
32
],
33
true
34
)
35
.await
36
.unwrap_err()
37
);
38
assert!(error.contains("unknown handle index"), "{error}");
39
Ok(())
40
}
41
42
#[tokio::test]
43
async fn async_borrowing_callee_misbehave() -> Result<()> {
44
let error = format!(
45
"{:?}",
46
test_run_bool(
47
&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],
48
true
49
)
50
.await
51
.unwrap_err()
52
);
53
assert!(error.contains("unknown handle index"), "{error}");
54
Ok(())
55
}
56
57
#[tokio::test]
58
pub async fn async_borrowing_callee() -> Result<()> {
59
test_run_bool(
60
&[test_programs_artifacts::ASYNC_BORROWING_CALLEE_COMPONENT],
61
false,
62
)
63
.await
64
}
65
66
pub async fn test_run_bool(components: &[&str], v: bool) -> Result<()> {
67
let mut config = config();
68
// As of this writing, miri/pulley/epochs is a problematic combination, so
69
// we don't test it.
70
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
71
config.epoch_interruption(true);
72
}
73
74
let engine = Engine::new(&config)?;
75
76
let component = make_component(&engine, components).await?;
77
78
let mut linker = Linker::new(&engine);
79
80
wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
81
component_async_tests::borrowing_host::bindings::local::local::borrowing_types::add_to_linker::<
82
_,
83
component_async_tests::Ctx,
84
>(&mut linker, |ctx| ctx)?;
85
86
let mut store = Store::new(
87
&engine,
88
component_async_tests::Ctx {
89
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
90
table: ResourceTable::default(),
91
continue_: false,
92
wakers: Arc::new(Mutex::new(None)),
93
},
94
);
95
96
if env::var_os("MIRI_TEST_CWASM_DIR").is_none() {
97
store.set_epoch_deadline(1);
98
99
std::thread::spawn(move || {
100
std::thread::sleep(Duration::from_secs(10));
101
engine.increment_epoch();
102
});
103
}
104
105
let instance = linker.instantiate_async(&mut store, &component).await?;
106
let borrowing_host =
107
component_async_tests::borrowing_host::bindings::BorrowingHost::new(&mut store, &instance)?;
108
109
instance
110
.run_concurrent(&mut store, async move |accessor| {
111
// Start three concurrent calls and then join them all:
112
let mut futures = FuturesUnordered::new();
113
for _ in 0..3 {
114
futures.push(borrowing_host.local_local_run_bool().call_run(accessor, v));
115
}
116
117
while let Some(()) = futures.try_next().await? {
118
// continue
119
}
120
121
anyhow::Ok(())
122
})
123
.await?
124
}
125
126