Skip to content

Commit 087b785

Browse files
committed
dev: extract clock to new package
1 parent 3b2d281 commit 087b785

File tree

38 files changed

+1050
-792
lines changed

38 files changed

+1050
-792
lines changed

Cargo.lock

+53-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ tdyne-peer-id-registry = "0"
6666
thiserror = "1"
6767
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
6868
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "packages/configuration" }
69+
torrust-tracker-clock = { version = "3.0.0-alpha.12-develop", path = "packages/clock" }
6970
torrust-tracker-contrib-bencode = { version = "3.0.0-alpha.12-develop", path = "contrib/bencode" }
7071
torrust-tracker-located-error = { version = "3.0.0-alpha.12-develop", path = "packages/located-error" }
7172
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "packages/primitives" }

cSpell.json

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"completei",
3333
"connectionless",
3434
"Containerfile",
35+
"conv",
3536
"curr",
3637
"Cyberneering",
3738
"dashmap",
@@ -109,6 +110,7 @@
109110
"ringsize",
110111
"rngs",
111112
"routable",
113+
"rstest",
112114
"rusqlite",
113115
"RUSTDOCFLAGS",
114116
"RUSTFLAGS",

packages/clock/Cargo.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
description = "A library to a clock for the torrust tracker."
3+
keywords = ["library", "clock", "torrents"]
4+
name = "torrust-tracker-clock"
5+
readme = "README.md"
6+
7+
authors.workspace = true
8+
categories.workspace = true
9+
documentation.workspace = true
10+
edition.workspace = true
11+
homepage.workspace = true
12+
license.workspace = true
13+
publish.workspace = true
14+
repository.workspace = true
15+
rust-version.workspace = true
16+
version.workspace = true
17+
18+
[dependencies]
19+
lazy_static = "1"
20+
chrono = { version = "0", default-features = false, features = ["clock"] }
21+
22+
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }
23+
24+
[dev-dependencies]

packages/clock/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Torrust Tracker Configuration
2+
3+
A library to provide torrent repository to the [Torrust Tracker](https://github.com/torrust/torrust-tracker).
4+
5+
## Documentation
6+
7+
[Crate documentation](https://docs.rs/torrust-tracker-torrent-repository).
8+
9+
## License
10+
11+
The project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE](./LICENSE).

packages/clock/src/clock/mod.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::time::Duration;
2+
3+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
4+
5+
use self::stopped::StoppedClock;
6+
use self::working::WorkingClock;
7+
8+
pub mod stopped;
9+
pub mod working;
10+
11+
/// A generic structure that represents a clock.
12+
///
13+
/// It can be either the working clock (production) or the stopped clock
14+
/// (testing). It implements the `Time` trait, which gives you the current time.
15+
#[derive(Debug)]
16+
pub struct Clock<T> {
17+
clock: std::marker::PhantomData<T>,
18+
}
19+
20+
/// The working clock. It returns the current time.
21+
pub type Working = Clock<WorkingClock>;
22+
/// The stopped clock. It returns always the same fixed time.
23+
pub type Stopped = Clock<StoppedClock>;
24+
25+
/// Trait for types that can be used as a timestamp clock.
26+
pub trait Time: Sized {
27+
fn now() -> DurationSinceUnixEpoch;
28+
29+
fn dbg_clock_type() -> String;
30+
31+
#[must_use]
32+
fn now_add(add_time: &Duration) -> Option<DurationSinceUnixEpoch> {
33+
Self::now().checked_add(*add_time)
34+
}
35+
#[must_use]
36+
fn now_sub(sub_time: &Duration) -> Option<DurationSinceUnixEpoch> {
37+
Self::now().checked_sub(*sub_time)
38+
}
39+
}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use std::any::TypeId;
44+
use std::time::Duration;
45+
46+
use crate::clock::{self, Stopped, Time, Working};
47+
use crate::CurrentClock;
48+
49+
#[test]
50+
fn it_should_be_the_stopped_clock_as_default_when_testing() {
51+
// We are testing, so we should default to the fixed time.
52+
assert_eq!(TypeId::of::<Stopped>(), TypeId::of::<CurrentClock>());
53+
assert_eq!(Stopped::now(), CurrentClock::now());
54+
}
55+
56+
#[test]
57+
fn it_should_have_different_times() {
58+
assert_ne!(TypeId::of::<clock::Stopped>(), TypeId::of::<Working>());
59+
assert_ne!(Stopped::now(), Working::now());
60+
}
61+
62+
#[test]
63+
fn it_should_use_stopped_time_for_testing() {
64+
assert_eq!(CurrentClock::dbg_clock_type(), "Stopped".to_owned());
65+
66+
let time = CurrentClock::now();
67+
std::thread::sleep(Duration::from_millis(50));
68+
let time_2 = CurrentClock::now();
69+
70+
assert_eq!(time, time_2);
71+
}
72+
}

0 commit comments

Comments
 (0)