From 4f4920582914ab1a40bf7354f5ffbfdf66839d4c Mon Sep 17 00:00:00 2001
From: Egor Ivkov <e.o.ivkov@gmail.com>
Date: Thu, 13 Jul 2023 13:37:29 +0300
Subject: [PATCH] feat: add sleep_async fn

---
 src/util.rs | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/util.rs b/src/util.rs
index dfa6f52569..6c6f813664 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -3,6 +3,8 @@ use std::io::BufRead as _;
 use std::io::BufReader;
 use std::io::Write as _;
 use std::os::unix::io::AsRawFd as _;
+use tarantool::fiber;
+use tarantool::fiber::r#async::timeout::IntoTimeout;
 pub use Either::{Left, Right};
 
 use crate::traft::error::Error;
@@ -12,11 +14,18 @@ use std::time::{Duration, Instant};
 
 const INFINITY: Duration = Duration::from_secs(30 * 365 * 24 * 60 * 60);
 
+// TODO: move to tarantool_module when we have custom `Instance` there
 pub fn instant_saturating_add(t: Instant, d: Duration) -> Instant {
     t.checked_add(d)
         .unwrap_or_else(|| t.checked_add(INFINITY).expect("that's too much, man"))
 }
 
+// TODO: move to tarantool_module
+pub async fn sleep_async(time: Duration) {
+    let (_, rx) = fiber::r#async::oneshot::channel::<()>();
+    rx.timeout(time).await.unwrap_err();
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 /// A generic enum that contains exactly one of two possible types. Equivalent
 /// to `std::result::Result`, but is more intuitive in some cases.
@@ -552,3 +561,12 @@ mod tests {
         );
     }
 }
+
+mod tarantool_tests {
+    use std::time::Duration;
+
+    #[::tarantool::test]
+    async fn sleep_wakes_up() {
+        super::sleep_async(Duration::from_millis(10)).await;
+    }
+}
-- 
GitLab