fs: add coop test (#2344)

This commit is contained in:
Carl Lerche 2020-03-26 22:17:56 -07:00 committed by GitHub
parent 11acfbbea4
commit 8020b02bd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@
use tokio::fs::File;
use tokio::prelude::*;
use tokio_test::task;
use std::io::prelude::*;
use tempfile::NamedTempFile;
@ -36,6 +37,31 @@ async fn basic_write() {
assert_eq!(file, HELLO);
}
#[tokio::test]
async fn coop() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
let mut task = task::spawn(async {
let mut file = File::open(tempfile.path()).await.unwrap();
let mut buf = [0; 1024];
loop {
file.read(&mut buf).await.unwrap();
file.seek(std::io::SeekFrom::Start(0)).await.unwrap();
}
});
for _ in 0..1_000 {
if task.poll().is_pending() {
return;
}
}
panic!("did not yield");
}
fn tempfile() -> NamedTempFile {
NamedTempFile::new().unwrap()
}