|
| 1 | +//! Uring file operations tests. |
| 2 | +
|
| 3 | +#![cfg(all( |
| 4 | + tokio_unstable, |
| 5 | + feature = "io-uring", |
| 6 | + feature = "rt", |
| 7 | + feature = "fs", |
| 8 | + target_os = "linux" |
| 9 | +))] |
| 10 | + |
| 11 | +use futures::future::FutureExt; |
| 12 | +use std::io::Write; |
| 13 | +use std::sync::mpsc; |
| 14 | +use std::task::Poll; |
| 15 | +use std::time::Duration; |
| 16 | +use std::{future::poll_fn, path::PathBuf}; |
| 17 | +use tempfile::NamedTempFile; |
| 18 | +use tokio::{ |
| 19 | + fs::read, |
| 20 | + runtime::{Builder, Runtime}, |
| 21 | +}; |
| 22 | +use tokio_util::task::TaskTracker; |
| 23 | + |
| 24 | +fn multi_rt(n: usize) -> Box<dyn Fn() -> Runtime> { |
| 25 | + Box::new(move || { |
| 26 | + Builder::new_multi_thread() |
| 27 | + .worker_threads(n) |
| 28 | + .enable_all() |
| 29 | + .build() |
| 30 | + .unwrap() |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +fn current_rt() -> Box<dyn Fn() -> Runtime> { |
| 35 | + Box::new(|| Builder::new_current_thread().enable_all().build().unwrap()) |
| 36 | +} |
| 37 | + |
| 38 | +fn rt_combinations() -> Vec<Box<dyn Fn() -> Runtime>> { |
| 39 | + vec![ |
| 40 | + current_rt(), |
| 41 | + multi_rt(1), |
| 42 | + multi_rt(2), |
| 43 | + multi_rt(8), |
| 44 | + multi_rt(64), |
| 45 | + multi_rt(256), |
| 46 | + ] |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn shutdown_runtime_while_performing_io_uring_ops() { |
| 51 | + fn run(rt: Runtime) { |
| 52 | + let (tx, rx) = mpsc::channel(); |
| 53 | + let (done_tx, done_rx) = mpsc::channel(); |
| 54 | + |
| 55 | + let (_tmp, path) = create_tmp_files(1); |
| 56 | + rt.spawn(async move { |
| 57 | + let path = path[0].clone(); |
| 58 | + |
| 59 | + // spawning a bunch of uring operations. |
| 60 | + loop { |
| 61 | + let path = path.clone(); |
| 62 | + tokio::spawn(async move { |
| 63 | + let bytes = read(path).await.unwrap(); |
| 64 | + |
| 65 | + assert_eq!(bytes, vec![20; 2]); |
| 66 | + }); |
| 67 | + |
| 68 | + // Avoid busy looping. |
| 69 | + tokio::task::yield_now().await; |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + std::thread::spawn(move || { |
| 74 | + let rt: Runtime = rx.recv().unwrap(); |
| 75 | + rt.shutdown_timeout(Duration::from_millis(300)); |
| 76 | + done_tx.send(()).unwrap(); |
| 77 | + }); |
| 78 | + |
| 79 | + tx.send(rt).unwrap(); |
| 80 | + done_rx.recv().unwrap(); |
| 81 | + } |
| 82 | + |
| 83 | + for rt in rt_combinations() { |
| 84 | + run(rt()); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn read_many_files() { |
| 90 | + fn run(rt: Runtime) { |
| 91 | + const NUM_FILES: usize = 512; |
| 92 | + |
| 93 | + let (_tmp_files, paths): (Vec<NamedTempFile>, Vec<PathBuf>) = create_tmp_files(NUM_FILES); |
| 94 | + |
| 95 | + rt.block_on(async move { |
| 96 | + let tracker = TaskTracker::new(); |
| 97 | + |
| 98 | + for i in 0..10_000 { |
| 99 | + let path = paths.get(i % NUM_FILES).unwrap().clone(); |
| 100 | + tracker.spawn(async move { |
| 101 | + let bytes = read(path).await.unwrap(); |
| 102 | + assert_eq!(bytes, vec![20; 2]); |
| 103 | + }); |
| 104 | + } |
| 105 | + tracker.close(); |
| 106 | + tracker.wait().await; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + for rt in rt_combinations() { |
| 111 | + run(rt()); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[tokio::test] |
| 116 | +async fn cancel_op_future() { |
| 117 | + let (_tmp_file, path): (Vec<NamedTempFile>, Vec<PathBuf>) = create_tmp_files(1); |
| 118 | + |
| 119 | + let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); |
| 120 | + let handle = tokio::spawn(async move { |
| 121 | + poll_fn(|cx| { |
| 122 | + let fut = read(&path[0]); |
| 123 | + |
| 124 | + // If io_uring is enabled (and not falling back to the thread pool), |
| 125 | + // the first poll should return Pending. |
| 126 | + let _pending = Box::pin(fut).poll_unpin(cx); |
| 127 | + |
| 128 | + tx.send(()).unwrap(); |
| 129 | + |
| 130 | + Poll::<()>::Pending |
| 131 | + }) |
| 132 | + .await; |
| 133 | + }); |
| 134 | + |
| 135 | + // Wait for the first poll |
| 136 | + rx.recv().await.unwrap(); |
| 137 | + |
| 138 | + handle.abort(); |
| 139 | + |
| 140 | + let res = handle.await.unwrap_err(); |
| 141 | + assert!(res.is_cancelled()); |
| 142 | +} |
| 143 | + |
| 144 | +fn create_tmp_files(num_files: usize) -> (Vec<NamedTempFile>, Vec<PathBuf>) { |
| 145 | + let mut files = Vec::with_capacity(num_files); |
| 146 | + for _ in 0..num_files { |
| 147 | + let mut tmp = NamedTempFile::new().unwrap(); |
| 148 | + let buf = vec![20; 2]; |
| 149 | + tmp.write_all(&buf).unwrap(); |
| 150 | + let path = tmp.path().to_path_buf(); |
| 151 | + files.push((tmp, path)); |
| 152 | + } |
| 153 | + |
| 154 | + files.into_iter().unzip() |
| 155 | +} |
0 commit comments