mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
fs: add coop test (#2344)
This commit is contained in:
parent
11acfbbea4
commit
8020b02bd0
@ -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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user