Skip to content

Commit

Permalink
test: [#143] add tests for extracted functions
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 3, 2023
1 parent 0f99f7b commit 1c6db6e
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/api/resource/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,57 @@ impl From<TrackerMetrics> for Stats {
}
}
}

#[cfg(test)]
mod tests {
use super::Stats;
use crate::tracker::services::statistics::TrackerMetrics;
use crate::tracker::statistics::Metrics;
use crate::tracker::TorrentsMetrics;

#[test]
fn stats_resource_should_be_converted_from_tracker_metrics() {
assert_eq!(
Stats::from(TrackerMetrics {
torrents_metrics: TorrentsMetrics {
seeders: 1,
completed: 2,
leechers: 3,
torrents: 4
},
protocol_metrics: Metrics {
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_connections_handled: 11,
udp4_announces_handled: 12,
udp4_scrapes_handled: 13,
udp6_connections_handled: 14,
udp6_announces_handled: 15,
udp6_scrapes_handled: 16
}
}),
Stats {
torrents: 4,
seeders: 1,
completed: 2,
leechers: 3,
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_connections_handled: 11,
udp4_announces_handled: 12,
udp4_scrapes_handled: 13,
udp6_connections_handled: 14,
udp6_announces_handled: 15,
udp6_scrapes_handled: 16
}
);
}
}
48 changes: 48 additions & 0 deletions src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,51 @@ impl Tracker {
}
}
}

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

use super::statistics::Keeper;
use super::{TorrentsMetrics, Tracker};
use crate::config::{ephemeral_configuration, Configuration};

pub fn tracker_configuration() -> Arc<Configuration> {
Arc::new(ephemeral_configuration())
}

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

// Configuration
let configuration = tracker_configuration();

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

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

#[tokio::test]
async fn the_tracker_should_collect_torrent_metrics() {
let tracker = tracker_factory();

let torrents_metrics = tracker.get_torrents_metrics().await;

assert_eq!(
torrents_metrics,
TorrentsMetrics {
seeders: 0,
completed: 0,
leechers: 0,
torrents: 0
}
);
}
}
48 changes: 48 additions & 0 deletions src/tracker/services/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,51 @@ pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
},
}
}

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

use super::Tracker;
use crate::config::{ephemeral_configuration, Configuration};
use crate::tracker;
use crate::tracker::services::statistics::{get_metrics, TrackerMetrics};
use crate::tracker::statistics::Keeper;

pub fn tracker_configuration() -> Arc<Configuration> {
Arc::new(ephemeral_configuration())
}

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

// Configuration
let configuration = tracker_configuration();

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

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

#[tokio::test]
async fn the_statistics_service_should_return_the_tracker_metrics() {
let tracker = Arc::new(tracker_factory());

let tracker_metrics = get_metrics(tracker.clone()).await;

assert_eq!(
tracker_metrics,
TrackerMetrics {
torrents_metrics: tracker::TorrentsMetrics::default(),
protocol_metrics: tracker::statistics::Metrics::default(),
}
);
}
}

0 comments on commit 1c6db6e

Please sign in to comment.