Skip to content

Commit e017bc6

Browse files
committed
add unit stream tests
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 499aade commit e017bc6

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/misc/component-async-tests/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,20 @@ mod test {
743743
test_run(&compose(caller, callee).await?).await
744744
}
745745

746+
// No-op function; we only test this by composing it in `async_unit_stream_caller`
747+
#[allow(
748+
dead_code,
749+
reason = "here only to make the `assert_test_exists` macro happy"
750+
)]
751+
fn async_unit_stream_callee() {}
752+
753+
#[tokio::test]
754+
async fn async_unit_stream_caller() -> Result<()> {
755+
let caller = &fs::read(test_programs_artifacts::ASYNC_UNIT_STREAM_CALLER_COMPONENT).await?;
756+
let callee = &fs::read(test_programs_artifacts::ASYNC_UNIT_STREAM_CALLEE_COMPONENT).await?;
757+
test_run(&compose(caller, callee).await?).await
758+
}
759+
746760
async fn test_run_bool(component: &[u8], v: bool) -> Result<()> {
747761
init_logger();
748762

crates/misc/component-async-tests/wit/test.wit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ interface run-result {
7373
run-pass: func() -> result<_, error-context>;
7474
}
7575

76+
interface unit-stream {
77+
run: func(count: u32) -> stream;
78+
}
79+
7680
world yield-caller {
7781
import continue;
7882
import ready;
@@ -153,3 +157,12 @@ world error-context-caller {
153157
import run-result;
154158
export run;
155159
}
160+
161+
world unit-stream-caller {
162+
import unit-stream;
163+
export run;
164+
}
165+
166+
world unit-stream-callee {
167+
export unit-stream;
168+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
mod bindings {
2+
wit_bindgen::generate!({
3+
path: "../misc/component-async-tests/wit",
4+
world: "unit-stream-callee",
5+
async: {
6+
exports: [
7+
"local:local/unit-stream#run",
8+
],
9+
}
10+
});
11+
12+
use super::Component;
13+
export!(Component);
14+
}
15+
16+
use {
17+
bindings::{exports::local::local::unit_stream::Guest, wit_stream},
18+
futures::SinkExt,
19+
wit_bindgen_rt::async_support::{self, StreamReader},
20+
};
21+
22+
struct Component;
23+
24+
impl Guest for Component {
25+
async fn run(count: u32) -> StreamReader<()> {
26+
let (mut tx, rx) = wit_stream::new();
27+
28+
async_support::spawn(async move {
29+
let mut sent = 0;
30+
let mut chunk_size = 1;
31+
while sent < count {
32+
let n = (count - sent).min(chunk_size);
33+
tx.send(vec![(); usize::try_from(n).unwrap()])
34+
.await
35+
.unwrap();
36+
sent += n;
37+
chunk_size *= 2;
38+
}
39+
});
40+
41+
rx
42+
}
43+
}
44+
45+
// Unused function; required since this file is built as a `bin`:
46+
fn main() {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
mod bindings {
2+
wit_bindgen::generate!({
3+
path: "../misc/component-async-tests/wit",
4+
world: "unit-stream-caller",
5+
async: {
6+
imports: [
7+
"local:local/unit-stream#run",
8+
],
9+
exports: [
10+
"local:local/run#run",
11+
],
12+
}
13+
});
14+
15+
use super::Component;
16+
export!(Component);
17+
}
18+
19+
use {
20+
bindings::{exports::local::local::run::Guest, local::local::unit_stream},
21+
futures::StreamExt,
22+
};
23+
24+
struct Component;
25+
26+
impl Guest for Component {
27+
async fn run() {
28+
let count = 42;
29+
let mut rx = unit_stream::run(count).await;
30+
31+
let mut received = 0;
32+
while let Some(chunk) = rx.next().await {
33+
received += chunk.len();
34+
}
35+
36+
assert_eq!(count, u32::try_from(received).unwrap());
37+
}
38+
}
39+
40+
// Unused function; required since this file is built as a `bin`:
41+
fn main() {}

0 commit comments

Comments
 (0)