[pbs-devel] [PATCH proxmox v4 1/2] proxmox/tools: add poll_once module for testing

Dominik Csapak d.csapak at proxmox.com
Fri Feb 12 15:44:31 CET 2021


copied from the pxar crate, intended for polling a future once
this is helpful for testing async code
(this needs to resolve in the first poll otherwise it will
not work)

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 proxmox/src/tools/mod.rs | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs
index 45f46f9..e938d90 100644
--- a/proxmox/src/tools/mod.rs
+++ b/proxmox/src/tools/mod.rs
@@ -227,3 +227,39 @@ pub fn nodename() -> &'static str {
 
     &NODENAME
 }
+
+#[cfg(test)]
+pub mod poll_once {
+    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