17ad1603dSTrevor Elliott use test_programs::wasi::cli::{stdin, stdout};
27ad1603dSTrevor Elliott 
main()37ad1603dSTrevor Elliott fn main() {
47ad1603dSTrevor Elliott     match std::env::var("PIPED_SIDE")
57ad1603dSTrevor Elliott         .expect("piped tests require the PIPED_SIDE env var")
67ad1603dSTrevor Elliott         .as_str()
77ad1603dSTrevor Elliott     {
87ad1603dSTrevor Elliott         "PRODUCER" => producer(),
97ad1603dSTrevor Elliott         "CONSUMER" => consumer(),
107ad1603dSTrevor Elliott         side => panic!("unknown piped test side: {side}"),
117ad1603dSTrevor Elliott     }
127ad1603dSTrevor Elliott }
137ad1603dSTrevor Elliott 
147ad1603dSTrevor Elliott const CHUNK: &[u8] = &[b'a'; 50];
157ad1603dSTrevor Elliott 
producer()167ad1603dSTrevor Elliott fn producer() {
177ad1603dSTrevor Elliott     let out = stdout::get_stdout();
187ad1603dSTrevor Elliott     let n = out.check_write().unwrap() as usize;
197ad1603dSTrevor Elliott     assert!(n > CHUNK.len());
207ad1603dSTrevor Elliott     out.write(CHUNK).unwrap();
217ad1603dSTrevor Elliott }
227ad1603dSTrevor Elliott 
consumer()237ad1603dSTrevor Elliott fn consumer() {
247ad1603dSTrevor Elliott     let stdin = stdin::get_stdin();
257ad1603dSTrevor Elliott     let stdin_pollable1 = stdin.subscribe();
267ad1603dSTrevor Elliott     let stdin_pollable2 = stdin.subscribe();
277ad1603dSTrevor Elliott 
28*0e9121daSFrankReh     // The two pollables are subscribed to the same resource, and must report the same readiness
297ad1603dSTrevor Elliott     stdin_pollable1.block();
307ad1603dSTrevor Elliott     assert!(stdin_pollable1.ready() && stdin_pollable2.ready());
317ad1603dSTrevor Elliott 
327ad1603dSTrevor Elliott     let bytes = stdin.read(CHUNK.len() as u64).unwrap();
337ad1603dSTrevor Elliott     assert_eq!(&bytes, CHUNK);
347ad1603dSTrevor Elliott }
35