use std::task::Poll;
use super::*;
async fn read_through(file: &Handle<File>) -> Vec<u8> {
let mut got = Vec::new();
loop {
let mut read = Box::pin(file.read_owned(Vec::with_capacity(64 << 10)));
let first = poll_fn(|cx| Poll::Ready(read.as_mut().poll(cx))).await;
let (n, buf) = match first {
Poll::Ready(out) => out,
Poll::Pending => within(read).await,
};
match n.unwrap() {
0 => return got,
_ => got.extend_from_slice(&buf),
}
}
}
#[test]
fn a_lent_read_of_cached_bytes_ends_at_its_first_poll() {
let rt = runtime();
let tmp = Temp::new("cached");
let path = tmp.file("data");
let data = pattern(256 << 10);
std::fs::write(&path, &data).unwrap();
rt.block_on(async {
let file = Handle::new(File::open(&path).unwrap()).unwrap();
let mut got = Vec::new();
loop {
let mut read = Box::pin(file.read_owned(Vec::with_capacity(64 << 10)));
let first = poll_fn(|cx| Poll::Ready(read.as_mut().poll(cx))).await;
let (n, buf) = match first {
Poll::Ready(out) => out,
Poll::Pending => {
assert!(
!file.file().unwrap().tries_cache(),
"a read of cached bytes waited for a job"
);
within(read).await
}
};
match n.unwrap() {
0 => break,
_ => got.extend_from_slice(&buf),
}
}
assert!(got == data, "the bytes read differ from the file's");
});
}
#[test]
fn a_lent_read_of_bytes_not_cached_reads_them_through_its_job() {
use std::os::fd::AsRawFd;
let rt = runtime();
let tmp = Temp::new("uncached");
let path = tmp.file("data");
let data = pattern(1 << 20);
{
let mut file = File::create(&path).unwrap();
std::io::Write::write_all(&mut file, &data).unwrap();
file.sync_all().unwrap();
let rc = unsafe { libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_DONTNEED) };
assert_eq!(rc, 0, "the advice was refused");
}
let got = rt.block_on(async {
let file = Handle::new(File::open(&path).unwrap()).unwrap();
read_through(&file).await
});
assert!(got == data, "the bytes read differ from the file's");
}