Skip to content

Commit

Permalink
Test that file EOF is not permanent: reading again should give new data
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtriplett committed Jun 30, 2022
1 parent 64b7791 commit dfdf56c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,4 +918,40 @@ mod tests {
assert_eq!(format!("{}", expect), format!("{}", actual));
})
}

#[test]
fn file_eof_is_not_permanent() -> crate::io::Result<()> {
let tempdir = tempfile::Builder::new()
.prefix("async-std-file-eof-test")
.tempdir()?;
let path = tempdir.path().join("testfile");

crate::task::block_on(async {
let mut file_w = File::create(&path).await?;
let mut file_r = File::open(&path).await?;

file_w.write_all(b"data").await?;
file_w.flush().await?;

let mut buf = [0u8; 4];
let mut len = file_r.read(&mut buf).await?;
assert_eq!(len, 4);
assert_eq!(&buf, b"data");

len = file_r.read(&mut buf).await?;
assert_eq!(len, 0);

file_w.write_all(b"more").await?;
file_w.flush().await?;

len = file_r.read(&mut buf).await?;
assert_eq!(len, 4);
assert_eq!(&buf, b"more");

len = file_r.read(&mut buf).await?;
assert_eq!(len, 0);

Ok(())
})
}
}

0 comments on commit dfdf56c

Please sign in to comment.