1 use test_programs::wasi::cli::{stdin, stdout};
2 
main()3 fn main() {
4     match std::env::var("PIPED_SIDE")
5         .expect("piped tests require the PIPED_SIDE env var")
6         .as_str()
7     {
8         "PRODUCER" => producer(),
9         "CONSUMER" => consumer(),
10         side => panic!("unknown piped test side: {side}"),
11     }
12 }
13 
producer()14 fn producer() {
15     let out = stdout::get_stdout();
16     let out_pollable = out.subscribe();
17 
18     for i in 1..100 {
19         let message = format!("{i}");
20         loop {
21             let available = out.check_write().unwrap() as usize;
22             if available >= message.len() {
23                 break;
24             }
25 
26             out_pollable.block();
27             assert!(out_pollable.ready());
28         }
29 
30         out.write(message.as_bytes()).unwrap()
31     }
32 
33     drop(out_pollable);
34 }
35 
consumer()36 fn consumer() {
37     let stdin = stdin::get_stdin();
38     let stdin_pollable = stdin.subscribe();
39 
40     for i in 1..100 {
41         let expected = format!("{i}");
42 
43         stdin_pollable.block();
44         assert!(stdin_pollable.ready());
45 
46         let bytes = stdin.read(expected.len() as u64).unwrap();
47         assert_eq!(&bytes, expected.as_bytes());
48     }
49 
50     drop(stdin_pollable);
51 }
52