Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/tracker.toml
callgrind.out
codecov.json
integration_tests_sqlite3.db
lcov.info
perf.data*
rustc-ice-*.txt
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ tracing = "0"
tracing-subscriber = { version = "0", features = ["json"] }

[dev-dependencies]
bittorrent-primitives = "0.1.0"
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
local-ip-address = "0"
mockall = "0"
torrust-rest-tracker-api-client = { version = "3.0.0-develop", path = "packages/rest-tracker-api-client" }
Expand Down
17 changes: 15 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,28 @@ use torrust_server_lib::registar::Registar;
use torrust_tracker_configuration::Configuration;
use tracing::instrument;

use crate::bootstrap;
use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
use crate::container::AppContainer;

pub async fn run() -> (Arc<AppContainer>, Vec<JoinHandle<()>>, Registar) {
let (config, app_container) = bootstrap::app::setup();

let app_container = Arc::new(app_container);

let (jobs, registar) = start(&config, &app_container).await;

(app_container, jobs, registar)
}

/// # Panics
///
/// Will panic if:
///
/// - Can't retrieve tracker keys from database.
/// - Can't load whitelist from database.
#[instrument(skip(config, app_container))]
pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) -> Vec<JoinHandle<()>> {
pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) -> (Vec<JoinHandle<()>>, Registar) {
if config.http_api.is_none()
&& (config.udp_trackers.is_none() || config.udp_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
&& (config.http_trackers.is_none() || config.http_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
Expand Down Expand Up @@ -138,8 +149,10 @@ pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) ->
));
}

println!("Registar entries: {:?}", registar.entries());

// Start Health Check API
jobs.push(health_check_api::start_job(&config.health_check_api, registar.entries()).await);

jobs
(jobs, registar)
}
9 changes: 2 additions & 7 deletions src/console/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,11 @@
//! kcachegrind callgrind.out
//! ```
use std::env;
use std::sync::Arc;
use std::time::Duration;

use tokio::time::sleep;

use crate::{app, bootstrap};
use crate::app;

pub async fn run() {
// Parse command line arguments
Expand All @@ -180,11 +179,7 @@ pub async fn run() {
return;
};

let (config, app_container) = bootstrap::app::setup();

let app_container = Arc::new(app_container);

let jobs = app::start(&config, &app_container).await;
let (_app_container, jobs, _registar) = app::run().await;

// Run the tracker for a fixed duration
let run_duration = sleep(Duration::from_secs(duration_secs));
Expand Down
10 changes: 2 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use std::sync::Arc;

use torrust_tracker_lib::{app, bootstrap};
use torrust_tracker_lib::app;

#[tokio::main]
async fn main() {
let (config, app_container) = bootstrap::app::setup();

let app_container = Arc::new(app_container);

let jobs = app::start(&config, &app_container).await;
let (_app_container, jobs, _registar) = app::run().await;

// handle the signals
tokio::select! {
Expand Down
7 changes: 4 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Scaffolding for integration tests.
//!
//! Integration tests are used to test the interaction between multiple modules,
//! multiple running trackers, etc. Tests for one specific module should be in
//! the corresponding package.
//!
//! ```text
//! cargo test --test integration
//! ```
mod servers;

// todo: there is only one test example that was copied from other package.
// We have to add tests for the whole app.

use torrust_tracker_clock::clock;

/// This code needs to be copied into each crate.
Expand Down
1 change: 1 addition & 0 deletions tests/servers/api/contract/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod stats;
94 changes: 94 additions & 0 deletions tests/servers/api/contract/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::env;
use std::str::FromStr as _;

use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_client::http::client::requests::announce::QueryBuilder;
use bittorrent_tracker_client::http::client::Client as HttpTrackerClient;
use reqwest::Url;
use serde::Deserialize;
use tokio::time::Duration;
use torrust_rest_tracker_api_client::connection_info::{ConnectionInfo, Origin};
use torrust_rest_tracker_api_client::v1::client::Client as TrackerApiClient;
use torrust_tracker_lib::app;

#[tokio::test]
async fn the_stats_api_endpoint_should_return_the_global_stats() {
// Logging must be OFF otherwise your will get the following error:
// `Unable to install global subscriber: SetGlobalDefaultError("a global default trace dispatcher has already been set")`
// That's because we can't initialize the logger twice.
// You can enable it if you run only this test.
let config_with_two_http_trackers = r#"
[metadata]
app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"

[logging]
threshold = "off"

[core]
listed = false
private = false

[core.database]
driver = "sqlite3"
path = "./integration_tests_sqlite3.db"

[[http_trackers]]
bind_address = "0.0.0.0:7272"
tracker_usage_statistics = true

[[http_trackers]]
bind_address = "0.0.0.0:7373"
tracker_usage_statistics = true

[http_api]
bind_address = "0.0.0.0:1414"

[http_api.access_tokens]
admin = "MyAccessToken"
"#;

env::set_var("TORRUST_TRACKER_CONFIG_TOML", config_with_two_http_trackers);

let (_app_container, _jobs, _registar) = app::run().await;

announce_to_tracker("http://127.0.0.1:7272").await;
announce_to_tracker("http://127.0.0.1:7373").await;

let global_stats = get_tracker_statistics("http://127.0.0.1:1414", "MyAccessToken").await;

assert_eq!(global_stats.tcp4_announces_handled, 2);
}

/// Make a sample announce request to the tracker.
async fn announce_to_tracker(tracker_url: &str) {
let response = HttpTrackerClient::new(Url::parse(tracker_url).unwrap(), Duration::from_secs(1))
.unwrap()
.announce(
&QueryBuilder::with_default_values()
.with_info_hash(&InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap()) // DevSkim: ignore DS173237
.query(),
)
.await;

assert!(response.is_ok());
}

/// Global statistics with only metrics relevant to the test.
#[derive(Deserialize)]
struct PartialGlobalStatistics {
tcp4_announces_handled: u64,
}

async fn get_tracker_statistics(aip_url: &str, token: &str) -> PartialGlobalStatistics {
let response = TrackerApiClient::new(ConnectionInfo::authenticated(Origin::new(aip_url).unwrap(), token))
.unwrap()
.get_tracker_statistics(None)
.await;

response
.json::<PartialGlobalStatistics>()
.await
.expect("Failed to parse JSON response")
}
1 change: 1 addition & 0 deletions tests/servers/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod contract;
32 changes: 0 additions & 32 deletions tests/servers/health_check_api.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/servers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod health_check_api;
pub mod api;
Loading