use futures::future::Future; use std::future::poll_fn; use std::io::Write; use std::path::PathBuf; use std::sync::mpsc; use std::task::{Context, Poll, Waker}; use std::time::Duration; use tempfile::NamedTempFile; use tokio::fs::read; use tokio::runtime::{Builder, Runtime}; use tokio_test::assert_pending; use tokio_util::task::TaskTracker;
let (_tmp_files, paths): (Vec<NamedTempFile>, Vec<PathBuf>) = create_tmp_files(NUM_FILES);
rt.block_on(asyncmove { let tracker = TaskTracker::new();
for i in0..10_000 { let path = paths.get(i % NUM_FILES).unwrap().clone();
tracker.spawn(asyncmove { let bytes = read(path).await.unwrap();
assert_eq!(bytes, vec![20; 1023]);
});
}
tracker.close();
tracker.wait().await;
});
}
for rt in rt_combinations() {
run(rt());
}
}
#[tokio::test] asyncfn read_small_large_files() { let (_tmp, path) = create_large_temp_file();
let bytes = read(path).await.unwrap();
assert_eq!(bytes, create_buf(5000));
let (_tmp, path) = create_small_temp_file();
let bytes = read(path).await.unwrap();
assert_eq!(bytes, create_buf(20));
}
#[tokio::test] asyncfn cancel_op_future() { let (_tmp_file, path): (Vec<NamedTempFile>, Vec<PathBuf>) = create_tmp_files(1); let path = path[0].clone();
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let handle = tokio::spawn(asyncmove { let fut = read(path.clone());
tokio::pin!(fut);
poll_fn(move |_| { // If io_uring is enabled (and not falling back to the thread pool), // the first poll should return Pending.
assert_pending!(fut.as_mut().poll(&mut Context::from_waker(Waker::noop())));
tx.send(true).unwrap();
Poll::<()>::Pending
})
.await;
});
// Wait for the first poll
let val = rx.recv().await;
assert!(val.unwrap());
handle.abort();
let res = handle.await.unwrap_err();
assert!(res.is_cancelled());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.