2020-01-02 21:32:52 +00:00
|
|
|
// Copyright (C) 2020 François Laignel <fengalin@free.fr>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
|
|
|
|
// Boston, MA 02110-1335, USA.
|
|
|
|
|
|
|
|
//! Wrappers for the underlying runtime specific time related Futures.
|
|
|
|
|
|
|
|
use futures::prelude::*;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-09-29 16:21:08 +00:00
|
|
|
use super::Context;
|
|
|
|
|
2020-01-02 21:32:52 +00:00
|
|
|
/// Wait until the given `delay` has elapsed.
|
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
2021-09-29 16:21:08 +00:00
|
|
|
///
|
|
|
|
/// When throttling is activated (i.e. when using a non-`0` `wait`
|
|
|
|
/// duration in `Context::acquire`), timer entries are assigned to
|
|
|
|
/// the nearest time frame, meaning that the delay might elapse
|
|
|
|
/// `wait` / 2 ms earlier or later than the expected instant.
|
|
|
|
///
|
|
|
|
/// Use [`delay_for_at_least`] when it's preferable not to return
|
|
|
|
/// before the expected instant.
|
2020-01-02 21:32:52 +00:00
|
|
|
pub async fn delay_for(delay: Duration) {
|
2021-09-29 16:21:08 +00:00
|
|
|
if delay > Duration::ZERO {
|
2020-10-19 15:03:10 +00:00
|
|
|
tokio::time::delay_for(delay).map(drop).await;
|
|
|
|
}
|
2020-01-02 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 16:21:08 +00:00
|
|
|
/// Wait until at least the given `delay` has elapsed.
|
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
|
|
|
///
|
|
|
|
/// See [`delay_for`] for details. This method won't return before
|
|
|
|
/// the expected delay has elapsed.
|
|
|
|
pub async fn delay_for_at_least(delay: Duration) {
|
|
|
|
if delay > Duration::ZERO {
|
|
|
|
tokio::time::delay_for(
|
|
|
|
delay + Context::current().map_or(Duration::ZERO, |ctx| ctx.wait_duration() / 2),
|
|
|
|
)
|
|
|
|
.map(drop)
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 21:32:52 +00:00
|
|
|
/// Builds a `Stream` that yields at `interval.
|
|
|
|
///
|
|
|
|
/// This must be called from within the target runtime environment.
|
|
|
|
pub fn interval(interval: Duration) -> impl Stream<Item = ()> {
|
|
|
|
tokio::time::interval(interval).map(drop)
|
|
|
|
}
|
2021-09-29 16:21:08 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
use crate::runtime::Context;
|
|
|
|
|
|
|
|
const MAX_THROTTLING: Duration = Duration::from_millis(10);
|
|
|
|
const DELAY: Duration = Duration::from_millis(12);
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn delay_for() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
let context = Context::acquire("delay_for", MAX_THROTTLING).unwrap();
|
|
|
|
|
|
|
|
let elapsed = crate::runtime::executor::block_on(context.spawn(async {
|
|
|
|
let now = Instant::now();
|
|
|
|
crate::runtime::time::delay_for(DELAY).await;
|
|
|
|
now.elapsed()
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Due to throttling, timer may be fired earlier
|
|
|
|
assert!(elapsed + MAX_THROTTLING / 2 >= DELAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn delay_for_at_least() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
|
|
|
let context = Context::acquire("delay_for_at_least", MAX_THROTTLING).unwrap();
|
|
|
|
|
|
|
|
let elapsed = crate::runtime::executor::block_on(context.spawn(async {
|
|
|
|
let now = Instant::now();
|
|
|
|
crate::runtime::time::delay_for_at_least(DELAY).await;
|
|
|
|
now.elapsed()
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Never returns earlier that DELAY
|
|
|
|
assert!(elapsed >= DELAY);
|
|
|
|
}
|
|
|
|
}
|