Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/test-programs/src/bin/piped_multiple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use test_programs::wasi::cli::{stdin, stdout};

fn main() {
match std::env::var("PIPED_SIDE")
.expect("piped tests require the PIPED_SIDE env var")
.as_str()
{
"PRODUCER" => producer(),
"CONSUMER" => consumer(),
side => panic!("unknown piped test side: {side}"),
}
}

const CHUNK: &[u8] = &[b'a'; 50];

fn producer() {
let out = stdout::get_stdout();
let n = out.check_write().unwrap() as usize;
assert!(n > CHUNK.len());
out.write(CHUNK).unwrap();
}

fn consumer() {
let stdin = stdin::get_stdin();
let stdin_pollable1 = stdin.subscribe();
let stdin_pollable2 = stdin.subscribe();

// The two pollables are subscribed to the same resource, and must report the same readyness
stdin_pollable1.block();
assert!(stdin_pollable1.ready() && stdin_pollable2.ready());

let bytes = stdin.read(CHUNK.len() as u64).unwrap();
assert_eq!(&bytes, CHUNK);
}
33 changes: 15 additions & 18 deletions tests/all/piped_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@

use super::cli_tests::get_wasmtime_command;
use anyhow::Result;
use std::process::{ChildStdout, Command, Stdio};
use std::process::Stdio;

fn producer_command(component_path: &str) -> Result<Command> {
let mut cmd = get_wasmtime_command()?;
cmd.arg("run")
pub fn run_wasmtime_piped(component_path: &str) -> Result<()> {
let mut producer = get_wasmtime_command()?
.arg("run")
.arg("-Wcomponent-model")
.arg("--env")
.arg("PIPED_SIDE=PRODUCER")
.arg(component_path)
.stdout(Stdio::piped());
Ok(cmd)
}
.stdout(Stdio::piped())
.spawn()?;

fn consumer_command(component_path: &str, input: ChildStdout) -> Result<Command> {
let mut cmd = get_wasmtime_command()?;
cmd.arg("run")
let mut consumer = get_wasmtime_command()?
.arg("run")
.arg("-Wcomponent-model")
.arg("--env")
.arg("PIPED_SIDE=CONSUMER")
.arg(component_path)
.stdin(input);
Ok(cmd)
}

pub fn run_wasmtime_piped(component_path: &str) -> Result<()> {
let mut producer = producer_command(component_path)?.spawn()?;
let stdout = producer.stdout.take().unwrap();
let mut consumer = consumer_command(component_path, stdout)?.spawn()?;
.stdin(producer.stdout.take().unwrap())
.spawn()?;

let producer = producer.wait()?;
if !producer.success() {
Expand Down Expand Up @@ -65,6 +57,11 @@ mod test_programs {
run_wasmtime_piped(PIPED_SIMPLE_COMPONENT).unwrap()
}

#[test]
fn piped_multiple() {
run_wasmtime_piped(PIPED_MULTIPLE_COMPONENT).unwrap()
}

#[test]
fn piped_polling() {
run_wasmtime_piped(PIPED_POLLING_COMPONENT).unwrap()
Expand Down