Skip to content

Commit

Permalink
dev: extract clock to new package
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Mar 25, 2024
1 parent 03883c0 commit 3e0745b
Show file tree
Hide file tree
Showing 38 changed files with 1,050 additions and 790 deletions.
54 changes: 53 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ serde_repr = "0"
thiserror = "1"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "packages/configuration" }
torrust-tracker-clock = { version = "3.0.0-alpha.12-develop", path = "packages/clock" }
torrust-tracker-contrib-bencode = { version = "3.0.0-alpha.12-develop", path = "contrib/bencode" }
torrust-tracker-located-error = { version = "3.0.0-alpha.12-develop", path = "packages/located-error" }
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "packages/primitives" }
Expand Down
2 changes: 2 additions & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"completei",
"connectionless",
"Containerfile",
"conv",
"curr",
"Cyberneering",
"dashmap",
Expand Down Expand Up @@ -116,6 +117,7 @@
"rngs",
"rosegment",
"routable",
"rstest",
"rusqlite",
"RUSTDOCFLAGS",
"RUSTFLAGS",
Expand Down
24 changes: 24 additions & 0 deletions packages/clock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
description = "A library to a clock for the torrust tracker."
keywords = ["library", "clock", "torrents"]
name = "torrust-tracker-clock"
readme = "README.md"

authors.workspace = true
categories.workspace = true
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
publish.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
lazy_static = "1"
chrono = { version = "0", default-features = false, features = ["clock"] }

torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }

[dev-dependencies]
11 changes: 11 additions & 0 deletions packages/clock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Torrust Tracker Clock

A library to provide a working and mockable clock for the [Torrust Tracker](https://github.com/torrust/torrust-tracker).

## Documentation

[Crate documentation](https://docs.rs/torrust-tracker-torrent-clock).

## License

The project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE](./LICENSE).
72 changes: 72 additions & 0 deletions packages/clock/src/clock/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::time::Duration;

use torrust_tracker_primitives::DurationSinceUnixEpoch;

use self::stopped::StoppedClock;
use self::working::WorkingClock;

pub mod stopped;
pub mod working;

/// A generic structure that represents a clock.
///
/// It can be either the working clock (production) or the stopped clock
/// (testing). It implements the `Time` trait, which gives you the current time.
#[derive(Debug)]
pub struct Clock<T> {
clock: std::marker::PhantomData<T>,
}

/// The working clock. It returns the current time.
pub type Working = Clock<WorkingClock>;
/// The stopped clock. It returns always the same fixed time.
pub type Stopped = Clock<StoppedClock>;

/// Trait for types that can be used as a timestamp clock.
pub trait Time: Sized {
fn now() -> DurationSinceUnixEpoch;

fn dbg_clock_type() -> String;

#[must_use]
fn now_add(add_time: &Duration) -> Option<DurationSinceUnixEpoch> {
Self::now().checked_add(*add_time)
}
#[must_use]
fn now_sub(sub_time: &Duration) -> Option<DurationSinceUnixEpoch> {
Self::now().checked_sub(*sub_time)
}
}

#[cfg(test)]
mod tests {
use std::any::TypeId;
use std::time::Duration;

use crate::clock::{self, Stopped, Time, Working};
use crate::CurrentClock;

#[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::<Stopped>(), TypeId::of::<CurrentClock>());
assert_eq!(Stopped::now(), CurrentClock::now());
}

#[test]
fn it_should_have_different_times() {
assert_ne!(TypeId::of::<clock::Stopped>(), TypeId::of::<Working>());
assert_ne!(Stopped::now(), Working::now());
}

#[test]
fn it_should_use_stopped_time_for_testing() {
assert_eq!(CurrentClock::dbg_clock_type(), "Stopped".to_owned());

let time = CurrentClock::now();
std::thread::sleep(Duration::from_millis(50));
let time_2 = CurrentClock::now();

assert_eq!(time, time_2);
}
}
Loading

0 comments on commit 3e0745b

Please sign in to comment.