-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,050 additions
and
790 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.