| 298 | |
| 299 | #[tokio::test] |
| 300 | async fn async_stdin_stream() { |
| 301 | // A StdinStream has the property that there are multiple |
| 302 | // InputStreams created, using the stream() method which are each |
| 303 | // views on the same shared state underneath. Consuming input on one |
| 304 | // stream results in consuming that input on all streams. |
| 305 | // |
| 306 | // AsyncStdinStream is a slightly more complex impl of StdinStream |
| 307 | // than the MemoryInputPipe above. We can create an AsyncReadStream |
| 308 | // from a file on the disk, and an AsyncStdinStream from that common |
| 309 | // stream, then check that the same property holds as above. |
| 310 | |
| 311 | let dir = tempfile::tempdir().unwrap(); |
| 312 | let mut path = std::path::PathBuf::from(dir.path()); |
| 313 | path.push("file"); |
| 314 | std::fs::write(&path, "the quick brown fox jumped over the three lazy dogs").unwrap(); |
| 315 | |
| 316 | let file = tokio::fs::File::open(&path) |
| 317 | .await |
| 318 | .expect("open created file"); |
| 319 | let stdin_stream = super::AsyncStdinStream::new(file); |
| 320 | |
| 321 | use super::StdinStream; |
| 322 | |
| 323 | let mut view1 = stdin_stream.p2_stream(); |
| 324 | let mut view2 = stdin_stream.p2_stream(); |
| 325 | |
| 326 | view1.ready().await; |
| 327 | |
| 328 | let read1 = view1.read(10).expect("read first 10 bytes"); |
| 329 | assert_eq!(read1, "the quick ".as_bytes(), "first 10 bytes"); |
| 330 | let read2 = view2.read(10).expect("read second 10 bytes"); |
| 331 | assert_eq!(read2, "brown fox ".as_bytes(), "second 10 bytes"); |
| 332 | let read3 = view1.read(10).expect("read third 10 bytes"); |
| 333 | assert_eq!(read3, "jumped ove".as_bytes(), "third 10 bytes"); |
| 334 | let read4 = view2.read(10).expect("read fourth 10 bytes"); |
| 335 | assert_eq!(read4, "r the thre".as_bytes(), "fourth 10 bytes"); |
| 336 | } |
| 337 | |
| 338 | #[tokio::test] |
| 339 | async fn async_stdout_stream_unblocks() { |