Skip to content

Commit

Permalink
refactor: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 9, 2023
1 parent 0097c85 commit 4ad9815
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 158 deletions.
9 changes: 9 additions & 0 deletions packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ pub fn ephemeral_with_reverse_proxy() -> Configuration {
cfg
}

#[must_use]
pub fn ephemeral_without_reverse_proxy() -> Configuration {
let mut cfg = ephemeral();

cfg.on_reverse_proxy = false;

cfg
}

#[must_use]
pub fn ephemeral_mode_public() -> Configuration {
let mut cfg = ephemeral();
Expand Down
42 changes: 8 additions & 34 deletions src/http/axum_implementation/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,56 +138,30 @@ fn map_to_aquatic_event(event: &Option<Event>) -> AnnounceEvent {

#[cfg(test)]
mod tests {
use std::sync::Arc;

use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::TrackerMode;
use torrust_tracker_test_helpers::configuration;

use crate::http::axum_implementation::requests::announce::Announce;
use crate::http::axum_implementation::responses;
use crate::http::axum_implementation::services::peer_ip_resolver::ClientIpSources;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::statistics::Keeper;
use crate::tracker::services::common::tracker_factory;
use crate::tracker::{peer, Tracker};

fn private_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Private;
tracker_factory(configuration)
tracker_factory(configuration::ephemeral_mode_private().into())
}

fn listed_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Listed;
tracker_factory(configuration)
fn whitelisted_tracker() -> Tracker {
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
}

fn tracker_on_reverse_proxy() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.on_reverse_proxy = true;
tracker_factory(configuration)
tracker_factory(configuration::ephemeral_with_reverse_proxy().into())
}

fn tracker_not_on_reverse_proxy() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.on_reverse_proxy = false;
tracker_factory(configuration)
}

fn tracker_factory(configuration: Configuration) -> Tracker {
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.

// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();

// Initialize Torrust tracker
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
Ok(tracker) => tracker,
Err(error) => {
panic!("{}", error)
}
}
tracker_factory(configuration::ephemeral_without_reverse_proxy().into())
}

fn sample_announce_request() -> Announce {
Expand Down Expand Up @@ -263,13 +237,13 @@ mod tests {

use std::sync::Arc;

use super::{listed_tracker, sample_announce_request, sample_client_ip_sources};
use super::{sample_announce_request, sample_client_ip_sources, whitelisted_tracker};
use crate::http::axum_implementation::handlers::announce::handle_announce;
use crate::http::axum_implementation::handlers::announce::tests::assert_error_response;

#[tokio::test]
async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() {
let tracker = Arc::new(listed_tracker());
let tracker = Arc::new(whitelisted_tracker());

let announce_request = sample_announce_request();

Expand Down
42 changes: 8 additions & 34 deletions src/http/axum_implementation/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,56 +96,30 @@ fn build_response(scrape_data: ScrapeData) -> Response {
mod tests {
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;

use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::TrackerMode;
use torrust_tracker_test_helpers::configuration;

use crate::http::axum_implementation::requests::scrape::Scrape;
use crate::http::axum_implementation::responses;
use crate::http::axum_implementation::services::peer_ip_resolver::ClientIpSources;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::statistics::Keeper;
use crate::tracker::services::common::tracker_factory;
use crate::tracker::Tracker;

fn private_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Private;
tracker_factory(configuration)
tracker_factory(configuration::ephemeral_mode_private().into())
}

fn listed_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Listed;
tracker_factory(configuration)
fn whitelisted_tracker() -> Tracker {
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
}

fn tracker_on_reverse_proxy() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.on_reverse_proxy = true;
tracker_factory(configuration)
tracker_factory(configuration::ephemeral_with_reverse_proxy().into())
}

fn tracker_not_on_reverse_proxy() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.on_reverse_proxy = false;
tracker_factory(configuration)
}

fn tracker_factory(configuration: Configuration) -> Tracker {
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.

// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();

// Initialize Torrust tracker
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
Ok(tracker) => tracker,
Err(error) => {
panic!("{}", error)
}
}
tracker_factory(configuration::ephemeral_without_reverse_proxy().into())
}

fn sample_scrape_request() -> Scrape {
Expand Down Expand Up @@ -214,13 +188,13 @@ mod tests {

use std::sync::Arc;

use super::{listed_tracker, sample_client_ip_sources, sample_scrape_request};
use super::{sample_client_ip_sources, sample_scrape_request, whitelisted_tracker};
use crate::http::axum_implementation::handlers::scrape::handle_scrape;
use crate::tracker::ScrapeData;

#[tokio::test]
async fn it_should_return_zeroed_swarm_metadata_when_the_torrent_is_not_whitelisted() {
let tracker = Arc::new(listed_tracker());
let tracker = Arc::new(whitelisted_tracker());

let scrape_request = sample_scrape_request();

Expand Down
24 changes: 2 additions & 22 deletions src/http/axum_implementation/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,17 @@ pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer)
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::Arc;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::TrackerMode;
use torrust_tracker_test_helpers::configuration;

use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::statistics::Keeper;
use crate::tracker::services::common::tracker_factory;
use crate::tracker::{peer, Tracker};

fn public_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Public;
tracker_factory(configuration)
}

fn tracker_factory(configuration: Configuration) -> Tracker {
// code-review: the tracker initialization is duplicated in many places. Consider make this function public.

// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();

// Initialize Torrust tracker
match Tracker::new(Arc::new(configuration), Some(stats_event_sender), stats_repository) {
Ok(tracker) => tracker,
Err(error) => {
panic!("{}", error)
}
}
tracker_factory(configuration::ephemeral_mode_public().into())
}

fn sample_info_hash() -> InfoHash {
Expand Down
19 changes: 6 additions & 13 deletions src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ mod tests {
use std::sync::Arc;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use torrust_tracker_primitives::TrackerMode;
use torrust_tracker_test_helpers::configuration;

use crate::protocol::clock::DurationSinceUnixEpoch;
Expand All @@ -564,22 +563,16 @@ mod tests {
use crate::tracker::services::common::tracker_factory;
use crate::tracker::{TorrentsMetrics, Tracker};

pub fn public_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Public;
tracker_factory(Arc::new(configuration))
fn public_tracker() -> Tracker {
tracker_factory(configuration::ephemeral_mode_public().into())
}

pub fn private_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Private;
tracker_factory(Arc::new(configuration))
fn private_tracker() -> Tracker {
tracker_factory(configuration::ephemeral_mode_private().into())
}

pub fn whitelisted_tracker() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.mode = TrackerMode::Listed;
tracker_factory(Arc::new(configuration))
fn whitelisted_tracker() -> Tracker {
tracker_factory(configuration::ephemeral_mode_whitelisted().into())
}

pub fn tracker_persisting_torrents_in_database() -> Tracker {
Expand Down
Loading

0 comments on commit 4ad9815

Please sign in to comment.