forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
250 lines (199 loc) · 7.54 KB
/
mod.rs
File metadata and controls
250 lines (199 loc) · 7.54 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::num::IntErrorKind;
use std::time::Duration;
pub type DurationSinceUnixEpoch = Duration;
#[derive(Debug)]
pub enum ClockType {
WorkingClock,
StoppedClock,
}
#[derive(Debug)]
pub struct Clock<const T: usize>;
pub type WorkingClock = Clock<{ ClockType::WorkingClock as usize }>;
pub type StoppedClock = Clock<{ ClockType::StoppedClock as usize }>;
#[cfg(not(test))]
pub type DefaultClock = WorkingClock;
#[cfg(test)]
pub type DefaultClock = StoppedClock;
pub trait Time: Sized {
fn now() -> DurationSinceUnixEpoch;
}
pub trait TimeNow: Time {
fn add(add_time: &Duration) -> Option<DurationSinceUnixEpoch> {
Self::now().checked_add(*add_time)
}
fn sub(sub_time: &Duration) -> Option<DurationSinceUnixEpoch> {
Self::now().checked_sub(*sub_time)
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
use crate::protocol::clock::{DefaultClock, StoppedClock, Time, WorkingClock};
#[test]
fn it_should_be_the_stopped_clock_as_default_when_testing() {
// We are testing, so we should default to the fixed time.
assert_eq!(TypeId::of::<StoppedClock>(), TypeId::of::<DefaultClock>());
assert_eq!(StoppedClock::now(), DefaultClock::now())
}
#[test]
fn it_should_have_different_times() {
assert_ne!(TypeId::of::<StoppedClock>(), TypeId::of::<WorkingClock>());
assert_ne!(StoppedClock::now(), WorkingClock::now())
}
}
mod working_clock {
use std::time::SystemTime;
use super::{DurationSinceUnixEpoch, Time, TimeNow, WorkingClock};
impl Time for WorkingClock {
fn now() -> DurationSinceUnixEpoch {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
}
}
impl TimeNow for WorkingClock {}
}
pub trait StoppedTime: TimeNow {
fn local_set(unix_time: &DurationSinceUnixEpoch);
fn local_set_to_unix_epoch() {
Self::local_set(&DurationSinceUnixEpoch::ZERO)
}
fn local_set_to_app_start_time();
fn local_set_to_system_time_now();
fn local_add(duration: &Duration) -> Result<(), IntErrorKind>;
fn local_sub(duration: &Duration) -> Result<(), IntErrorKind>;
fn local_reset();
}
mod stopped_clock {
use std::num::IntErrorKind;
use std::time::Duration;
use super::{DurationSinceUnixEpoch, StoppedClock, StoppedTime, Time, TimeNow};
impl Time for StoppedClock {
fn now() -> DurationSinceUnixEpoch {
detail::FIXED_TIME.with(|time| {
return *time.borrow();
})
}
}
impl TimeNow for StoppedClock {}
impl StoppedTime for StoppedClock {
fn local_set(unix_time: &DurationSinceUnixEpoch) {
detail::FIXED_TIME.with(|time| {
*time.borrow_mut() = *unix_time;
})
}
fn local_set_to_app_start_time() {
Self::local_set(&detail::get_app_start_time())
}
fn local_set_to_system_time_now() {
Self::local_set(&detail::get_app_start_time())
}
fn local_add(duration: &Duration) -> Result<(), IntErrorKind> {
detail::FIXED_TIME.with(|time| {
let time_borrowed = *time.borrow();
*time.borrow_mut() = match time_borrowed.checked_add(*duration) {
Some(time) => time,
None => {
return Err(IntErrorKind::PosOverflow);
}
};
Ok(())
})
}
fn local_sub(duration: &Duration) -> Result<(), IntErrorKind> {
detail::FIXED_TIME.with(|time| {
let time_borrowed = *time.borrow();
*time.borrow_mut() = match time_borrowed.checked_sub(*duration) {
Some(time) => time,
None => {
return Err(IntErrorKind::NegOverflow);
}
};
Ok(())
})
}
fn local_reset() {
Self::local_set(&detail::get_default_fixed_time())
}
}
#[cfg(test)]
mod tests {
use std::thread;
use std::time::Duration;
use crate::protocol::clock::{DurationSinceUnixEpoch, StoppedClock, StoppedTime, Time, TimeNow, WorkingClock};
#[test]
fn it_should_default_to_zero_when_testing() {
assert_eq!(StoppedClock::now(), DurationSinceUnixEpoch::ZERO)
}
#[test]
fn it_should_possible_to_set_the_time() {
// Check we start with ZERO.
assert_eq!(StoppedClock::now(), Duration::ZERO);
// Set to Current Time and Check
let timestamp = WorkingClock::now();
StoppedClock::local_set(×tamp);
assert_eq!(StoppedClock::now(), timestamp);
// Elapse the Current Time and Check
StoppedClock::local_add(×tamp).unwrap();
assert_eq!(StoppedClock::now(), timestamp + timestamp);
// Reset to ZERO and Check
StoppedClock::local_reset();
assert_eq!(StoppedClock::now(), Duration::ZERO);
}
#[test]
fn it_should_default_to_zero_on_thread_exit() {
assert_eq!(StoppedClock::now(), Duration::ZERO);
let after5 = WorkingClock::add(&Duration::from_secs(5)).unwrap();
StoppedClock::local_set(&after5);
assert_eq!(StoppedClock::now(), after5);
let t = thread::spawn(move || {
// each thread starts out with the initial value of ZERO
assert_eq!(StoppedClock::now(), Duration::ZERO);
// and gets set to the current time.
let timestamp = WorkingClock::now();
StoppedClock::local_set(×tamp);
assert_eq!(StoppedClock::now(), timestamp);
});
// wait for the thread to complete and bail out on panic
t.join().unwrap();
// we retain our original value of current time + 5sec despite the child thread
assert_eq!(StoppedClock::now(), after5);
// Reset to ZERO and Check
StoppedClock::local_reset();
assert_eq!(StoppedClock::now(), Duration::ZERO);
}
}
mod detail {
use std::cell::RefCell;
use std::time::SystemTime;
use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::static_time;
pub fn get_app_start_time() -> DurationSinceUnixEpoch {
(*static_time::TIME_AT_APP_START)
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
}
#[cfg(not(test))]
pub fn get_default_fixed_time() -> DurationSinceUnixEpoch {
get_app_start_time()
}
#[cfg(test)]
pub fn get_default_fixed_time() -> DurationSinceUnixEpoch {
DurationSinceUnixEpoch::ZERO
}
thread_local!(pub static FIXED_TIME: RefCell<DurationSinceUnixEpoch> = RefCell::new(get_default_fixed_time()));
#[cfg(test)]
mod tests {
use std::time::Duration;
use crate::protocol::clock::stopped_clock::detail::{get_app_start_time, get_default_fixed_time};
#[test]
fn it_should_get_the_zero_start_time_when_testing() {
assert_eq!(get_default_fixed_time(), Duration::ZERO);
}
#[test]
fn it_should_get_app_start_time() {
const TIME_AT_WRITING_THIS_TEST: Duration = Duration::new(1662983731, 22312);
assert!(get_app_start_time() > TIME_AT_WRITING_THIS_TEST);
}
}
}
}
pub mod time_extent;