[pbs-devel] [PATCH proxmox v5 1/2] proxmox: add test/{io, task} modules

Dominik Csapak d.csapak at proxmox.com
Wed Feb 17 14:13:20 CET 2021


contains:
* AsyncBlocking{Reader,Writer} for dummy async code
  by wrapping a 'standard reader/writer'
* poll_result_once for pulling a future once (copied from pxar)

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 proxmox/src/lib.rs       |  3 ++
 proxmox/src/test/io.rs   | 94 ++++++++++++++++++++++++++++++++++++++++
 proxmox/src/test/mod.rs  |  2 +
 proxmox/src/test/task.rs | 32 ++++++++++++++
 4 files changed, 131 insertions(+)
 create mode 100644 proxmox/src/test/io.rs
 create mode 100644 proxmox/src/test/mod.rs
 create mode 100644 proxmox/src/test/task.rs

diff --git a/proxmox/src/lib.rs b/proxmox/src/lib.rs
index b74b399..6e95906 100644
--- a/proxmox/src/lib.rs
+++ b/proxmox/src/lib.rs
@@ -8,6 +8,9 @@ pub mod api;
 pub mod sys;
 pub mod tools;
 
+#[cfg(test)]
+pub mod test;
+
 /// An identity (nop) macro. Used by the `#[sortable]` proc macro.
 #[cfg(feature = "sortable-macro")]
 #[macro_export]
diff --git a/proxmox/src/test/io.rs b/proxmox/src/test/io.rs
new file mode 100644
index 0000000..919aac1
--- /dev/null
+++ b/proxmox/src/test/io.rs
@@ -0,0 +1,94 @@
+use std::pin::Pin;
+use std::task::{Context, Poll};
+
+use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
+
+pub struct AsyncBlockingReader<R> {
+    inner: R,
+}
+
+impl<W> AsyncBlockingReader<W> {
+    pub fn new(inner: W) -> Self {
+        Self {
+            inner
+        }
+    }
+
+    pub fn inner(&self) -> &W {
+        &self.inner
+    }
+}
+
+pub struct AsyncBlockingWriter<W> {
+    inner: W,
+    seek_pos: u64,
+}
+
+impl<W> AsyncBlockingWriter<W> {
+    pub fn new(inner: W) -> Self {
+        Self {
+            inner,
+            seek_pos: 0,
+        }
+    }
+
+    pub fn inner(&self) -> &W {
+        &self.inner
+    }
+}
+
+impl<R: std::io::Read + Unpin> AsyncRead for AsyncBlockingReader<R> {
+    fn poll_read(
+        self: Pin<&mut Self>,
+        _cx: &mut Context<'_>,
+        buf: &mut ReadBuf<'_>,
+    ) -> Poll<std::io::Result<()>> {
+        let this = Pin::get_mut(self);
+        let mut read_buf = buf.initialize_unfilled();
+        match this.inner.read(&mut read_buf) {
+            Ok(len) => {
+                buf.advance(len);
+                Poll::Ready(Ok(()))
+            }
+            Err(err) => Poll::Ready(Err(err)),
+        }
+    }
+}
+
+impl<R: std::io::Write + Unpin> AsyncWrite for AsyncBlockingWriter<R> {
+    fn poll_write(
+        self: Pin<&mut Self>,
+        _cx: &mut Context<'_>,
+        buf: &[u8],
+    ) -> Poll<std::io::Result<usize>> {
+        let this = Pin::get_mut(self);
+        match this.inner.write(buf) {
+            Ok(len) => Poll::Ready(Ok(len)),
+            Err(err) => Poll::Ready(Err(err)),
+        }
+    }
+
+    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
+        Poll::Ready(Ok(()))
+    }
+
+    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
+        Poll::Ready(Ok(()))
+    }
+}
+
+impl<R: std::io::Seek + Unpin> AsyncSeek for AsyncBlockingWriter<R> {
+    fn start_seek(self: Pin<&mut Self>, position: std::io::SeekFrom) -> std::io::Result<()> {
+        let this = Pin::get_mut(self);
+        this.seek_pos = this.inner.seek(position)?;
+        Ok(())
+    }
+
+    fn poll_complete(
+        self: Pin<&mut Self>,
+        _cx: &mut Context<'_>,
+    ) -> Poll<std::io::Result<u64>> {
+        let this = Pin::get_mut(self);
+        Poll::Ready(Ok(this.seek_pos))
+    }
+}
diff --git a/proxmox/src/test/mod.rs b/proxmox/src/test/mod.rs
new file mode 100644
index 0000000..82ac3e2
--- /dev/null
+++ b/proxmox/src/test/mod.rs
@@ -0,0 +1,2 @@
+pub mod io;
+pub mod task;
diff --git a/proxmox/src/test/task.rs b/proxmox/src/test/task.rs
new file mode 100644
index 0000000..4f5eca6
--- /dev/null
+++ b/proxmox/src/test/task.rs
@@ -0,0 +1,32 @@
+use std::future::Future;
+use std::pin::Pin;
+use std::task::{Context, Poll};
+
+pub fn poll_result_once<T, R>(mut fut: T) -> std::io::Result<R>
+where
+    T: Future<Output = std::io::Result<R>>,
+{
+    let waker = std::task::RawWaker::new(std::ptr::null(), &WAKER_VTABLE);
+    let waker = unsafe { std::task::Waker::from_raw(waker) };
+    let mut cx = Context::from_waker(&waker);
+    unsafe {
+        match Pin::new_unchecked(&mut fut).poll(&mut cx) {
+            Poll::Pending => Err(crate::sys::error::io_err_other(
+                    "got Poll::Pending synchronous context",
+            )),
+            Poll::Ready(r) => r,
+        }
+    }
+}
+
+const WAKER_VTABLE: std::task::RawWakerVTable =
+std::task::RawWakerVTable::new(forbid_clone, forbid_wake, forbid_wake, ignore_drop);
+
+unsafe fn forbid_clone(_: *const ()) -> std::task::RawWaker {
+    panic!("tried to clone waker for synchronous task");
+}
+
+unsafe fn forbid_wake(_: *const ()) {
+    panic!("tried to wake synchronous task");
+}
+unsafe fn ignore_drop(_: *const ()) {}
-- 
2.20.1






More information about the pbs-devel mailing list