1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![doc = include_str!("../README.md")]
use std::future::Future;
use std::time::Duration;

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 timeout;
pub use timeout::Timeout;

#[derive(Clone)]
pub enum MockableClock {
	Real,
	#[cfg(feature = "mock")]
	Mock(std::sync::Arc<async_time_mock_core::TimerRegistry>),
}

pub enum TimeHandlerGuard {
	Real,
	#[cfg(feature = "mock")]
	Mock(async_time_mock_core::TimeHandlerGuard),
}

#[cfg(feature = "mock")]
impl From<async_time_mock_core::TimeHandlerGuard> for TimeHandlerGuard {
	fn from(guard: async_time_mock_core::TimeHandlerGuard) -> Self {
		Self::Mock(guard)
	}
}

impl MockableClock {
	#[cfg(feature = "mock")]
	pub fn mock() -> (Self, std::sync::Arc<async_time_mock_core::TimerRegistry>) {
		let timer_registry = std::sync::Arc::new(async_time_mock_core::TimerRegistry::default());
		(Self::Mock(timer_registry.clone()), timer_registry)
	}

	pub fn now(&self) -> Instant {
		use MockableClock::*;
		match self {
			Real => tokio::time::Instant::now().into(),
			#[cfg(feature = "mock")]
			Mock(registry) => registry.now().into(),
		}
	}

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

	pub async fn sleep_until(&self, until: Instant) -> TimeHandlerGuard {
		match (self, until) {
			(MockableClock::Real, Instant::Real(until)) => {
				tokio::time::sleep_until(until).await;
				TimeHandlerGuard::Real
			}
			#[cfg(feature = "mock")]
			(MockableClock::Mock(registry), Instant::Mock(until)) => registry.sleep_until(until).await.into(),
			#[cfg(feature = "mock")]
			_ => panic!("Clock and instant weren't compatible, both need to be either real or mocked"),
		}
	}

	pub fn interval(&self, period: Duration) -> Interval {
		use MockableClock::*;
		match self {
			Real => tokio::time::interval(period).into(),
			#[cfg(feature = "mock")]
			Mock(registry) => registry.interval(period).into(),
		}
	}

	pub fn interval_at(&self, start: Instant, period: Duration) -> Interval {
		match (self, start) {
			(MockableClock::Real, Instant::Real(start)) => tokio::time::interval_at(start, period).into(),
			#[cfg(feature = "mock")]
			(MockableClock::Mock(registry), Instant::Mock(start)) => registry.interval_at(start, period).into(),
			#[cfg(feature = "mock")]
			_ => panic!("Clock and instant weren't compatible, both need to be either real or mocked"),
		}
	}

	pub fn timeout<T>(&self, duration: Duration, future: T) -> Timeout<T>
	where
		T: Future,
	{
		use MockableClock::*;
		match self {
			Real => tokio::time::timeout(duration, future).into(),
			#[cfg(feature = "mock")]
			Mock(registry) => registry.timeout(duration, future).into(),
		}
	}

	pub fn timeout_at<T>(&self, deadline: Instant, future: T) -> Timeout<T>
	where
		T: Future,
	{
		match (self, deadline) {
			(MockableClock::Real, Instant::Real(deadline)) => tokio::time::timeout_at(deadline, future).into(),
			#[cfg(feature = "mock")]
			(MockableClock::Mock(registry), Instant::Mock(deadline)) => registry.timeout_at(deadline, future).into(),
			#[cfg(feature = "mock")]
			_ => panic!("Clock and instant weren't compatible, both need to be either real or mocked"),
		}
	}
}