Skip to content

Commit

Permalink
Return named future type from async-time-mock-tokio sleep methods
Browse files Browse the repository at this point in the history
Uses the async-time-mock-macros create_mock_future macro.
  • Loading branch information
FSMaxB committed Jan 7, 2025
1 parent 2817d51 commit 5193794
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions async-time-mock-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod create_mock_future;

#[doc(hidden)]
pub use pin_project_lite::pin_project;
9 changes: 5 additions & 4 deletions async-time-mock-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ rust-version = "1.70"


[dependencies]
async-time-mock-core = {version = "0.1", path = "../async-time-mock-core", optional = true}
async-time-mock-core = { version = "0.1", path = "../async-time-mock-core", optional = true }
async-time-mock-macros = { version = "0.1", path = "../async-time-mock-macros" }
futures-core = { version = "0.3", optional = true }
pin-project = "1"
tokio = {version = "1", features = ["time"]}
futures-core = {version = "0.3", optional = true}
tokio = { version = "1", features = ["time"] }

[dev-dependencies]
tokio = {version = "1", features = ["macros", "rt-multi-thread"]}
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[features]
default = ["stream"]
Expand Down
27 changes: 12 additions & 15 deletions async-time-mock-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
use std::future::Future;
use std::time::{Duration, SystemTime};

#[cfg(feature = "mock")]
pub use async_time_mock_core as core;

mod instant;
use crate::interval::Interval;
pub use instant::Instant;

#[cfg(feature = "mock")]
pub use async_time_mock_core as core;

mod elapsed;
mod interval;
mod sleep;
mod timeout;

pub use sleep::Sleep;
pub use timeout::Timeout;

#[derive(Clone)]
Expand Down Expand Up @@ -59,26 +62,20 @@ impl MockableClock {
}
}

pub async fn sleep(&self, duration: Duration) -> TimeHandlerGuard {
pub fn sleep(&self, duration: Duration) -> Sleep {
use MockableClock::*;
match self {
Real => {
tokio::time::sleep(duration).await;
TimeHandlerGuard::Real
}
Real => tokio::time::sleep(duration).into(),
#[cfg(feature = "mock")]
Mock(registry) => registry.sleep(duration).await.into(),
Mock(registry) => registry.sleep(duration).into(),
}
}

pub async fn sleep_until(&self, until: Instant) -> TimeHandlerGuard {
pub fn sleep_until(&self, until: Instant) -> Sleep {
match (self, until) {
(MockableClock::Real, Instant::Real(until)) => {
tokio::time::sleep_until(until).await;
TimeHandlerGuard::Real
}
(MockableClock::Real, Instant::Real(until)) => tokio::time::sleep_until(until).into(),
#[cfg(feature = "mock")]
(MockableClock::Mock(registry), Instant::Mock(until)) => registry.sleep_until(until).await.into(),
(MockableClock::Mock(registry), Instant::Mock(until)) => registry.sleep_until(until).into(),
#[cfg(feature = "mock")]
_ => panic!("Clock and instant weren't compatible, both need to be either real or mocked"),
}
Expand Down
13 changes: 13 additions & 0 deletions async-time-mock-tokio/src/sleep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::TimeHandlerGuard;
#[cfg(feature = "mock")]
use async_time_mock_core::TimerListener;
use async_time_mock_macros::create_mock_future;

create_mock_future!(
Sleep,
TimeHandlerGuard,
tokio::time::Sleep,
TimerListener,
|()| TimeHandlerGuard::Real,
TimeHandlerGuard::from
);

0 comments on commit 5193794

Please sign in to comment.