public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v4 1/2] proxmox/tools: add poll_once module for testing
Date: Fri, 12 Feb 2021 15:44:31 +0100	[thread overview]
Message-ID: <20210212144433.30668-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20210212144433.30668-1-d.csapak@proxmox.com>

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@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





  reply	other threads:[~2021-02-12 14:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-12 14:44 [pbs-devel] [PATCH proxmox v4 0/2] restore files from pxar sparsely Dominik Csapak
2021-02-12 14:44 ` Dominik Csapak [this message]
2021-02-12 14:44 ` [pbs-devel] [PATCH proxmox v4 2/2] proxmox: add sparse_copy(_async) to tools::io Dominik Csapak
2021-02-12 14:44 ` [pbs-devel] [PATCH proxmox-backup v4 1/1] pxar/extract: if possible create files sparesly Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210212144433.30668-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal