summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio/tests/time_rt.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/tokio/tests/time_rt.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tokio/tests/time_rt.rs')
-rw-r--r--third_party/rust/tokio/tests/time_rt.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/third_party/rust/tokio/tests/time_rt.rs b/third_party/rust/tokio/tests/time_rt.rs
new file mode 100644
index 0000000000..23367be418
--- /dev/null
+++ b/third_party/rust/tokio/tests/time_rt.rs
@@ -0,0 +1,89 @@
+#![warn(rust_2018_idioms)]
+#![cfg(feature = "full")]
+
+use tokio::time::*;
+
+use std::sync::mpsc;
+
+#[test]
+fn timer_with_threaded_runtime() {
+ use tokio::runtime::Runtime;
+
+ let rt = Runtime::new().unwrap();
+ let (tx, rx) = mpsc::channel();
+
+ rt.spawn(async move {
+ let when = Instant::now() + Duration::from_millis(10);
+
+ sleep_until(when).await;
+ assert!(Instant::now() >= when);
+
+ tx.send(()).unwrap();
+ });
+
+ rx.recv().unwrap();
+}
+
+#[test]
+fn timer_with_basic_scheduler() {
+ use tokio::runtime::Builder;
+
+ let rt = Builder::new_current_thread().enable_all().build().unwrap();
+ let (tx, rx) = mpsc::channel();
+
+ rt.block_on(async move {
+ let when = Instant::now() + Duration::from_millis(10);
+
+ sleep_until(when).await;
+ assert!(Instant::now() >= when);
+
+ tx.send(()).unwrap();
+ });
+
+ rx.recv().unwrap();
+}
+
+#[tokio::test]
+async fn starving() {
+ use std::future::Future;
+ use std::pin::Pin;
+ use std::task::{Context, Poll};
+
+ struct Starve<T: Future<Output = ()> + Unpin>(T, u64);
+
+ impl<T: Future<Output = ()> + Unpin> Future for Starve<T> {
+ type Output = u64;
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<u64> {
+ if Pin::new(&mut self.0).poll(cx).is_ready() {
+ return Poll::Ready(self.1);
+ }
+
+ self.1 += 1;
+
+ cx.waker().wake_by_ref();
+
+ Poll::Pending
+ }
+ }
+
+ let when = Instant::now() + Duration::from_millis(10);
+ let starve = Starve(Box::pin(sleep_until(when)), 0);
+
+ starve.await;
+ assert!(Instant::now() >= when);
+}
+
+#[tokio::test]
+async fn timeout_value() {
+ use tokio::sync::oneshot;
+
+ let (_tx, rx) = oneshot::channel::<()>();
+
+ let now = Instant::now();
+ let dur = Duration::from_millis(10);
+
+ let res = timeout(dur, rx).await;
+ assert!(res.is_err());
+ assert!(Instant::now() >= now + dur);
+}