From 8020b02bd0aa02e5fb887430fbb986a1376a7a9e Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 26 Mar 2020 22:17:56 -0700 Subject: [PATCH] fs: add coop test (#2344) --- tokio/tests/fs_file.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tokio/tests/fs_file.rs b/tokio/tests/fs_file.rs index 8913a6029..eee9a5b5c 100644 --- a/tokio/tests/fs_file.rs +++ b/tokio/tests/fs_file.rs @@ -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() }