Skip to content

Commit

Permalink
refactor: [#1215] instantiate only needed services
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 28, 2025
1 parent 74b04c8 commit f208876
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 294 deletions.
14 changes: 2 additions & 12 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ mod tests {
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_test_helpers::configuration;

use crate::app_test::initialize_tracker_dependencies;
use crate::core::services::statistics::{self, get_metrics, TrackerMetrics};
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::core::{self};
use crate::servers::udp::server::banning::BanService;
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
Expand All @@ -131,19 +131,9 @@ mod tests {
async fn the_statistics_service_should_return_the_tracker_metrics() {
let config = tracker_configuration();

let (
_database,
_in_memory_whitelist,
_whitelist_authorization,
_authentication_service,
in_memory_torrent_repository,
_db_torrent_repository,
_torrents_manager,
) = initialize_tracker_dependencies(&config);

let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
let stats_repository = Arc::new(stats_repository);

let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

let tracker_metrics = get_metrics(
Expand Down
55 changes: 7 additions & 48 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,10 @@ pub async fn get_torrents(
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};

use crate::app_test::initialize_tracker_dependencies;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;

fn initialize_in_memory_torrent_repository(config: &Configuration) -> Arc<InMemoryTorrentRepository> {
let (
_database,
_in_memory_whitelist,
_whitelist_authorization,
_authentication_service,
in_memory_torrent_repository,
_db_torrent_repository,
_torrents_manager,
) = initialize_tracker_dependencies(config);

in_memory_torrent_repository
}

fn sample_peer() -> peer::Peer {
peer::Peer {
peer_id: PeerId(*b"-qB00000000000000000"),
Expand All @@ -153,17 +134,11 @@ mod tests {
use std::sync::Arc;

use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer};
use crate::core::services::torrent::tests::sample_peer;
use crate::core::services::torrent::{get_torrent_info, Info};
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;

pub fn tracker_configuration() -> Configuration {
configuration::ephemeral()
}

#[tokio::test]
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
Expand All @@ -179,9 +154,7 @@ mod tests {

#[tokio::test]
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
let config = tracker_configuration();

let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand Down Expand Up @@ -210,17 +183,11 @@ mod tests {
use std::sync::Arc;

use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer};
use crate::core::services::torrent::tests::sample_peer;
use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination};
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;

pub fn tracker_configuration() -> Configuration {
configuration::ephemeral()
}

#[tokio::test]
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
Expand All @@ -232,9 +199,7 @@ mod tests {

#[tokio::test]
async fn should_return_a_summarized_info_for_all_torrents() {
let config = tracker_configuration();

let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand All @@ -256,9 +221,7 @@ mod tests {

#[tokio::test]
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
let config = tracker_configuration();

let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand All @@ -279,9 +242,7 @@ mod tests {

#[tokio::test]
async fn should_allow_using_pagination_in_the_result() {
let config = tracker_configuration();

let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down Expand Up @@ -311,9 +272,7 @@ mod tests {

#[tokio::test]
async fn should_return_torrents_ordered_by_info_hash() {
let config = tracker_configuration();

let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down
Loading

0 comments on commit f208876

Please sign in to comment.