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
use crate::time::MockSleepProvider;
use tor_rtcompat::{Runtime, SleepProvider, SpawnBlocking, TcpProvider, TlsProvider};
use async_trait::async_trait;
use futures::task::{FutureObj, Spawn, SpawnError};
use futures::Future;
use std::io::Result as IoResult;
use std::net::SocketAddr;
use std::time::{Duration, Instant, SystemTime};
#[derive(Clone)]
pub struct MockSleepRuntime<R: Runtime> {
runtime: R,
sleep: MockSleepProvider,
}
impl<R: Runtime> MockSleepRuntime<R> {
pub fn new(runtime: R) -> Self {
let sleep = MockSleepProvider::new(SystemTime::now());
MockSleepRuntime { runtime, sleep }
}
pub fn inner(&self) -> &R {
&self.runtime
}
pub fn mock_sleep(&self) -> &MockSleepProvider {
&self.sleep
}
pub async fn advance(&self, dur: Duration) {
self.sleep.advance(dur).await;
}
pub fn jump_to(&self, new_wallclock: SystemTime) {
self.sleep.jump_to(new_wallclock);
}
}
impl<R: Runtime> Spawn for MockSleepRuntime<R> {
fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
self.runtime.spawn_obj(future)
}
}
impl<R: Runtime> SpawnBlocking for MockSleepRuntime<R> {
fn block_on<F: Future>(&self, future: F) -> F::Output {
self.runtime.block_on(future)
}
}
#[async_trait]
impl<R: Runtime> TcpProvider for MockSleepRuntime<R> {
type TcpStream = R::TcpStream;
type TcpListener = R::TcpListener;
async fn connect(&self, addr: &SocketAddr) -> IoResult<Self::TcpStream> {
self.runtime.connect(addr).await
}
async fn listen(&self, addr: &SocketAddr) -> IoResult<Self::TcpListener> {
self.runtime.listen(addr).await
}
}
impl<R: Runtime> TlsProvider for MockSleepRuntime<R> {
type Connector = R::Connector;
type TlsStream = R::TlsStream;
fn tls_connector(&self) -> Self::Connector {
self.runtime.tls_connector()
}
}
impl<R: Runtime> SleepProvider for MockSleepRuntime<R> {
type SleepFuture = crate::time::Sleeping;
fn sleep(&self, dur: Duration) -> Self::SleepFuture {
self.sleep.sleep(dur)
}
fn now(&self) -> Instant {
self.sleep.now()
}
fn wallclock(&self) -> SystemTime {
self.sleep.wallclock()
}
}