Skip to content

Commit

Permalink
refactor: move static vars
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 17, 2023
1 parent 9ffcd6b commit 2d99866
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use std::sync::Arc;
use torrust_tracker_configuration::Configuration;

use crate::bootstrap::stats;
use crate::shared::clock::static_time;
use crate::shared::crypto::ephemeral_instance_keys;
use crate::tracker::Tracker;
use crate::{bootstrap, ephemeral_instance_keys, static_time, tracker};
use crate::{bootstrap, tracker};

/// # Panics
///
Expand Down
19 changes: 0 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,3 @@ pub mod tracker;

#[macro_use]
extern crate lazy_static;

pub mod static_time {
use std::time::SystemTime;

lazy_static! {
pub static ref TIME_AT_APP_START: SystemTime = SystemTime::now();
}
}

pub mod ephemeral_instance_keys {
use rand::rngs::ThreadRng;
use rand::Rng;

pub type Seed = [u8; 32];

lazy_static! {
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
}
}
4 changes: 2 additions & 2 deletions src/shared/clock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod static_time;
pub mod time_extent;
pub mod utils;

Expand Down Expand Up @@ -289,8 +290,7 @@ mod stopped_clock {
use std::cell::RefCell;
use std::time::SystemTime;

use crate::shared::clock::DurationSinceUnixEpoch;
use crate::static_time;
use crate::shared::clock::{static_time, DurationSinceUnixEpoch};

pub fn get_app_start_time() -> DurationSinceUnixEpoch {
(*static_time::TIME_AT_APP_START)
Expand Down
5 changes: 5 additions & 0 deletions src/shared/clock/static_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::time::SystemTime;

lazy_static! {
pub static ref TIME_AT_APP_START: SystemTime = SystemTime::now();
}
21 changes: 16 additions & 5 deletions src/shared/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
pub mod ephemeral_instance_keys {
use rand::rngs::ThreadRng;
use rand::Rng;

pub type Seed = [u8; 32];

lazy_static! {
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
}
}

pub mod keys {

pub mod seeds {
use self::detail::CURRENT_SEED;
use crate::ephemeral_instance_keys::{Seed, RANDOM_SEED};
use crate::shared::crypto::ephemeral_instance_keys::{Seed, RANDOM_SEED};

pub trait Keeper {
type Seed: Sized + Default + AsMut<[u8]>;
Expand Down Expand Up @@ -33,7 +44,7 @@ pub mod keys {
mod tests {
use super::detail::ZEROED_TEST_SEED;
use super::{Current, Instance, Keeper};
use crate::ephemeral_instance_keys::Seed;
use crate::shared::crypto::ephemeral_instance_keys::Seed;

pub struct ZeroedTestSeed;

Expand All @@ -58,7 +69,7 @@ pub mod keys {
}

mod detail {
use crate::ephemeral_instance_keys::Seed;
use crate::shared::crypto::ephemeral_instance_keys::Seed;

#[allow(dead_code)]
pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32];
Expand All @@ -67,13 +78,13 @@ pub mod keys {
pub use ZEROED_TEST_SEED as CURRENT_SEED;

#[cfg(not(test))]
pub use crate::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED;
pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED;

#[cfg(test)]
mod tests {
use std::convert::TryInto;

use crate::ephemeral_instance_keys::RANDOM_SEED;
use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED;
use crate::shared::crypto::keys::seeds::detail::ZEROED_TEST_SEED;
use crate::shared::crypto::keys::seeds::CURRENT_SEED;

Expand Down
4 changes: 3 additions & 1 deletion tests/common/tracker.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;

use torrust_tracker::bootstrap;
use torrust_tracker::shared::clock::static_time;
use torrust_tracker::shared::crypto::ephemeral_instance_keys;
use torrust_tracker::tracker::services::common::tracker_factory;
use torrust_tracker::tracker::Tracker;
use torrust_tracker::{bootstrap, ephemeral_instance_keys, static_time};

// TODO: Move to test-helpers crate once `Tracker` is isolated.
#[allow(clippy::module_name_repetitions)]
Expand Down

0 comments on commit 2d99866

Please sign in to comment.