From 27e373ec1f9accc0e9ada37445de80624a4f8da7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 7 Jan 2025 13:43:54 +0100 Subject: [PATCH] Return named future type from async-time-mock-tokio sleep methods Uses the async-time-mock-shared create_mock_future macro. --- Cargo.lock | 1 + async-time-mock-tokio/Cargo.toml | 9 +++++---- async-time-mock-tokio/src/lib.rs | 27 ++++++++++++--------------- async-time-mock-tokio/src/sleep.rs | 13 +++++++++++++ 4 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 async-time-mock-tokio/src/sleep.rs diff --git a/Cargo.lock b/Cargo.lock index ac70d1e..1e7e83d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,6 +242,7 @@ name = "async-time-mock-tokio" version = "0.1.1" dependencies = [ "async-time-mock-core", + "async-time-mock-shared", "futures-core", "pin-project", "tokio", diff --git a/async-time-mock-tokio/Cargo.toml b/async-time-mock-tokio/Cargo.toml index 9f32323..7be3df2 100644 --- a/async-time-mock-tokio/Cargo.toml +++ b/async-time-mock-tokio/Cargo.toml @@ -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-shared = { version = "0.1", path = "../async-time-mock-shared" } +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"] diff --git a/async-time-mock-tokio/src/lib.rs b/async-time-mock-tokio/src/lib.rs index aab3955..b8e863e 100644 --- a/async-time-mock-tokio/src/lib.rs +++ b/async-time-mock-tokio/src/lib.rs @@ -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)] @@ -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"), } diff --git a/async-time-mock-tokio/src/sleep.rs b/async-time-mock-tokio/src/sleep.rs new file mode 100644 index 0000000..fbac030 --- /dev/null +++ b/async-time-mock-tokio/src/sleep.rs @@ -0,0 +1,13 @@ +use crate::TimeHandlerGuard; +#[cfg(feature = "mock")] +use async_time_mock_core::TimerListener; +use async_time_mock_shared::create_mock_future; + +create_mock_future!( + Sleep, + TimeHandlerGuard, + tokio::time::Sleep, + TimerListener, + |()| TimeHandlerGuard::Real, + TimeHandlerGuard::from +);